使用 Maven,如何构建一个包含项目 jar 和所有依赖 jar 的可分发组件?
我有一个项目(类型为“jar”),(显然)构建了一个jar。但该项目有许多依赖项。我希望Maven构建一个“包”或“程序集”,其中包含我的jar,所有依赖jar和一些脚本(用于启动应用程序等)。
最好的方法是什么?具体来说,让依赖项进入程序集的最佳方法是什么?
我有一个项目(类型为“jar”),(显然)构建了一个jar。但该项目有许多依赖项。我希望Maven构建一个“包”或“程序集”,其中包含我的jar,所有依赖jar和一些脚本(用于启动应用程序等)。
最好的方法是什么?具体来说,让依赖项进入程序集的最佳方法是什么?
对于单个模块,我将使用如下所示的程序集():src/assembly/bin.xml
<assembly>
<id>bin</id>
<formats>
<format>tar.gz</format>
<format>tar.bz2</format>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/command</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>*.sh</include>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
若要使用此程序集,请将以下配置添加到 pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
在此示例中,位于 下且捆绑到 的启动/停止脚本,依赖项捆绑到 中。自定义它以满足您的需求。src/main/command
bin
lib
这是我的解决方案,用于创建一个可分发的(或 /),包括文件夹中的所有依赖项。它将:.zip
.tar.gz
.tar.bz2
lib
jar
lib
jar
target/lib
摘录:pom.xml
<!-- create distributable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>full.path.to.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/resources/dist.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
dist.xml
:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<formats>
<format>zip</format>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/site</directory>
<outputDirectory>docs</outputDirectory>
</fileSet>
</fileSets>
</assembly>
是从此处的描述符格式派生而来的:http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#bindist.xml
bin