建立使用maven脂肪罐子 [英] Building a fat jar using maven
问题描述
我有我要分发的罐子code基地。它也有依赖于外部的罐子,这是我想在最后罐子捆绑。
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的组装插件
,但我不知道怎么样。可能有人点我的一些例子。
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.
现在,我使用的脂肪罐捆绑最后的罐子。我想使用maven来实现同样的事情。
Right now, I'm using fat jar to bundle the final jar. I want to achieve the same thing using maven.
推荐答案
添加下面的插件,你的的pom.xml
Add following plugin to your pom.xml
...
<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包
将产生两个jar:一是只包含项目类,第二发罐后缀为所有依赖罐子,有依赖性。
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>
这篇关于建立使用maven脂肪罐子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!