Maven:在项目JAR旁边封装依赖项?

我希望Maven在运行时依赖包的同时打包一个项目。 我期望它创build一个JAR文件,具有以下清单:

..... Main-Class : com.acme.MainClass Class-Path : lib/dependency1.jar lib/dependency2.jar ..... 

并创build以下目录结构:

 target |-- .... |-- my-project.jar |-- lib |-- dependency1.jar |-- dependency2.jar 

意思是,我想要主JAR排除任何依赖关系,我希望所有的传递依赖被复制到一个“lib”子目录。 有任何想法吗?

我喜欢Maven打包运行时依赖的项目。

这部分不清楚(这不正是你刚才描述的)。 我的回答涵盖你所描述的。

我期望它创build一个JAR文件,具有以下清单(…)

configurationMaven Jar Plugin (或更准确地说, Maven Archiver ):

 <project> ... <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.acme.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> ... <dependencies> <dependency> <groupId>dependency1</groupId> <artifactId>dependency1</artifactId> <version>XY</version> </dependency> <dependency> <groupId>dependency2</groupId> <artifactId>dependency2</artifactId> <version>WZ</version> </dependency> </dependencies> ... </project> 

这将产生一个MANIFEST.MF与以下项目:

 ... Main-Class: fully.qualified.MainClass Class-Path: lib/dependency1-XYjar lib/dependency2-WZjar ... 

并创build以下目录结构(…)

这是可行的使用Maven的依赖插件和dependency:copy-dependencies目标。 从文档:

  • dependency:copy-dependencies获取项目直接依赖关系列表和可选的传递依赖项列表,并将它们复制到指定的位置,如果需要则剥离该版本。 这个目标也可以从命令行运行。

你可以在package阶段绑定它:

 <project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build> [...] </project> 

在pom.xml中添加以下插件。 检查mainClass,classpathPrefix,addClasspath标记的值。

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <mainClass>org.apache.camel.spring.Main</mainClass> <classpathPrefix>lib/</classpathPrefix> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptors> <descriptor>src/assembly/some-assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 

在src / assembly下创build一些-assembly.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>distribution</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <scope>runtime</scope> <outputDirectory>/lib</outputDirectory> <useProjectArtifact>false</useProjectArtifact> <unpack>false</unpack> </dependencySet> </dependencySets> 

注意useProjectArtifact标志为false,解压标志为false。 如果不需要zip文件中的根文件夹,那么可以使includeBaseDirectory为false。

这将创buildname-version-distribution.zip文件。 里面的zip文件,会有文件夹的名称版本。 在这个文件夹中,包含所有依赖关系jar的可执行jar和lib文件夹将会出现。 检查可执行jar的manifest.MF文件。 它包含主类和类path信息。

你可以使用maven jar插件,看看这个页面: http : //maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html