我正在使用 maven-assemble-plugin
从我的 Maven 项目创建一个输出 jar。我的 pom.xml
的相关部分如下所示:
.
.
.
<dependencies>
<dependency>
.
.
.
</dependency>
.
.
.
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
.
.
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>my.package.TheApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
.
.
.
</plugins>
</build>
.
.
.
当我运行“mvn clean install assembly:single”时,生成的 jar 文件确实包含依赖项,但它不包含我的项目文件。使用
运行 jarjava -jar TheApp-0.1.2-SNAPSHOT-jar-with-dependencies.jar
我收到消息错误:无法找到或加载主类 my.package.TheApp
。 那么如何包含我的项目文件?(我想过设置 useProjectArtifact
,但 according to the maven-assembly-plugin doco 此属性默认为 true
。)
最佳答案
通过查阅 Stack Overflow I found the solution :
运行mvn cleancompile assembly:single
而不是mvn clean install assembly:single
。 :-)
或者更好的是在pom.xml
中将程序集绑定(bind)到包阶段(感谢@khmarbaise!):
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
.
.
.
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
然后运行mvn clean package
(如果需要,也可以运行mvn clean install
)。
关于java - 使用 `maven-assembly-plugin` 时,如何使用 Maven 将所有依赖项和我的 Java 项目文件捆绑到输出 jar 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60843834/