使用 maven 构建一个胖罐子 [英] Building a fat jar using maven
问题描述
我有一个代码库,我想将其作为 jar 分发.它还依赖于外部 jar,我想将其捆绑在最终 jar 中.
I have a code base which I want to distribute as jar. It also have dependency on external jars, which I want to bundle in the final jar.
我听说这可以使用 maven-assembly-plug-in
来完成,但我不明白怎么做.有人可以给我指出一些例子.
I heard that this can be done using maven-assembly-plug-in
, but I don't understand how. Could someone point me to some examples.
现在,我正在使用 fat jar 来打包最终的 jar.我想使用 maven 实现同样的目的.
Right now, I'm using fat jar to bundle the final jar. I want to achieve the same thing using maven.
推荐答案
注意:如果你是 spring-boot 应用,请阅读答案结尾
将以下插件添加到您的 pom.xml
最新版本可以在在
Add following plugin to your pom.xml
The latest version can be found at
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>CHOOSE LATEST VERSION HERE</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
配置好这个插件后,运行mvn package
会产生两个jar:一个只包含项目类,第二个包含所有依赖的fat jar,后缀为-jar-with-dependencies"".
After configuring this plug-in, running mvn package
will produce two jars: one containing just the project classes, and a second fat jar with all dependencies with the suffix "-jar-with-dependencies".
如果你想在运行时正确设置 classpath
然后添加以下插件
if you want correct classpath
setup at runtime then also add following plugin
<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>
<小时>
对于 spring boot 应用程序只使用以下插件(选择合适的版本)
For spring boot application use just following plugin (choose appropriate version of it)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>${start-class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
这篇关于使用 maven 构建一个胖罐子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!