首页 > 解决方案 > Maven 安装和运行 JAR 文件会产生各种编译错误

问题描述

使用创建 jar 文件mvn install并使用该 jar 运行时,java -jar App.jar我在运行时遇到各种不同的编译错误。每次安装后,这些错误都会发生变化。一些例子:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
        ByteOrder cannot be resolved
        ByteOrder cannot be resolved
        ByteOrder cannot be resolved
        ByteOrder cannot be resolved

        at com.aidan.core.requestreply.ByteRequest.generateUniqueID(ByteRequest.java:49)
        at com.aidan.core.requestreply.ByteRequest.fire(ByteRequest.java:105)
        at com.aidan.core.requestreply.Request.fire(Request.java:22)
        at com.aidan.core.util.DistCall.getSecretKey(DistCall.java:27)
        at com.aidan.core.App.main(App.java:13)
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        StringUtils cannot be resolved

        at com.aidan.core.App.main(App.java:19)

我不明白为什么这些编译错误不会使mvn install命令失败,也不明白为什么每次安装时错误消息都不同。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.aidan.core</groupId>
  <artifactId>App</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>App</name>
  <url>http://maven.apache.org</url>
  <repositories>
    <repository>
      <id>maven-repository</id>
      <url>https://mvnrepository.com/</url>
    </repository>
  </repositories>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- protobuf paths -->
    <protobuf.input.directory>${project.basedir}/src/main/proto</protobuf.input.directory>
    <protobuf.output.directory>${project.basedir}/src/main/java</protobuf.output.directory>

    <!-- library versions -->
    <build-helper-maven-plugin.version>1.9.1</build-helper-maven-plugin.version>
    <maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
    <maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
    <maven-shade-plugin.version>2.4.2</maven-shade-plugin.version>
    <os-maven-plugin.version>1.4.1.Final</os-maven-plugin.version>
    <protobuf.version>2.6.1</protobuf.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>${protobuf.version}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>App</finalName>
    <extensions>
      <!-- provides os.detected.classifier (i.e. linux-x86_64, osx-x86_64) property -->
      <extension>
        <groupId>kr.motd.maven</groupId>
        <artifactId>os-maven-plugin</artifactId>
        <version>${os-maven-plugin.version}</version>
      </extension>
    </extensions>
    <plugins>

      <!-- Set a JDK compiler level -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
        </configuration>
      </plugin>

      <!-- Make this jar executable -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
              <!-- Jar file entry point -->
              <mainClass>com.aidan.core.App</mainClass>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>${maven-dependency-plugin.version}</version>
        <executions>
          <execution>
            <phase>install</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>lib</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-protoc</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>com.google.protobuf</groupId>
                  <artifactId>protoc</artifactId>
                  <version>${protobuf.version}</version>
                  <classifier>${os.detected.classifier}</classifier>
                  <type>exe</type>
                  <overWrite>true</overWrite>
                  <outputDirectory>${project.build.directory}</outputDirectory>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- compile proto buffer files using copied protoc binary -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>${maven-antrun-plugin.version}</version>
        <executions>
          <execution>
            <id>exec-protoc</id>
            <phase>generate-sources</phase>
            <configuration>
              <target>
                <property name="protoc.filename" value="protoc-${protobuf.version}-${os.detected.classifier}.exe" />
                <property name="protoc.filepath" value="${project.build.directory}/${protoc.filename}" />
                <chmod file="${protoc.filepath}" perm="ugo+rx" />
                <mkdir dir="${protobuf.output.directory}" />
                <path id="protobuf.input.filepaths.path">
                  <fileset dir="${protobuf.input.directory}">
                    <include name="**/*.proto" />
                  </fileset>
                </path>
                <pathconvert pathsep=" " property="protobuf.input.filepaths" refid="protobuf.input.filepaths.path" />
                <exec executable="${protoc.filepath}" failonerror="true">
                  <arg value="-I" />
                  <arg value="${protobuf.input.directory}" />
                  <arg value="--java_out" />
                  <arg value="${protobuf.output.directory}" />
                  <arg line="${protobuf.input.filepaths}" />
                </exec>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- add generated proto buffer classes into the package -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>${build-helper-maven-plugin.version}</version>
        <executions>
          <execution>
            <id>add-classes</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>${protobuf.output.directory}</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>
</project>

生成文件

clean:
    rm -rf target
    rm -rf lib
    rm App.jar
    mvn clean

jar:
    mvn clean install dependency:copy-dependencies -DskipTests
    cp target/App.jar App.jar

test:
    mvn clean test -q

run:
    java -jar App.jar

go:
    echo "[COMPILING]"
    $(MAKE) jar
    echo "Done!\n\n[RUNNING TESTS]"
    $(MAKE) test
    echo "Done!\n\n[RUNNING PROGRAM]"
    $(MAKE) run

我正在运行make go进行完整编译并运行 jar 文件。

标签: javamaven

解决方案


> Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        StringUtils cannot be resolved
        at com.aidan.core.App.main(App.java:19

StringUtils类无法解决。它是第三方库还是您自己的自定义类?

您需要确保在您的POM


推荐阅读