猿问
如何使用Maven创建具有依赖关系的可执行JAR?
我想将我的项目打包在一个可执行的JAR中进行分发。
如何将所有依赖JAR的Maven项目打包到我的输出JAR中?
拉丁的传说
浏览 750
回答 3
3回答
心有法竹
采取未答复的答案并重新格式化,我们有:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins></build>接下来,我建议将其作为构建的自然部分,而不是明确调用。要使其成为构建中不可或缺的一部分,请将此插件添加到您的插件pom.xml并将其绑定到package生命周期事件。但是,assembly:single如果将它放在pom.xml中,则需要调用目标,而如果从命令行手动执行它,则调用'assembly:assembly'。<project> [...] <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-my-jar-with-dependencies</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> [...] </plugins> [...] </build></project>
0
0
0
回首忆惘然
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins></build>你运行它mvn clean compile assembly:single应该在汇编之前添加编译目标:单个或者不包括您自己项目中的代码。在评论中查看更多详情。通常,此目标与构建阶段相关联,以自动执行。这可确保在执行mvn install或执行部署/发布时构建JAR 。<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <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>
0
0
0
随时随地看视频
慕课网APP
相关分类
Java
我要回答