Maven:将依赖项与项目 JAR 一起打包? [英] Maven: Packaging dependencies alongside project JAR?
问题描述
我希望 Maven 将项目与其运行时依赖项一起打包.我希望它创建一个包含以下清单的 JAR 文件:
<代码>.....主类:com.acme.MainClass类路径:lib/dependency1.jar lib/dependency2.jar......
并创建以下目录结构:
目标|-- ....|-- 我的项目.jar|-- 库|-- 依赖1.jar|-- 依赖2.jar
意思是,我希望主 JAR 排除任何依赖项,并且希望将所有传递依赖项复制到lib"子目录中.有什么想法吗?
我喜欢 Maven 打包一个带有运行时依赖项的项目.
这部分不清楚(这与您刚刚描述的不完全一样).我的回答涵盖了您所描述的内容.
<块引用>我希望它创建一个带有以下清单 (...) 的 JAR 文件
配置 Maven Jar 插件 这样做(或更准确地说,Maven 归档器):
<代码><项目>...<构建><插件><插件><artifactId>maven-jar-plugin</artifactId><配置><存档><清单><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><mainClass>com.acme.MainClass</mainClass></清单></存档></配置></插件></插件></构建>...<依赖关系><依赖性><groupId>dependency1</groupId><artifactId>dependency1</artifactId><版本>X.Y</版本></依赖><依赖性><groupId>dependency2</groupId><artifactId>dependency2</artifactId><版本>W.Z</版本></依赖></依赖关系>...</项目>
这将产生一个包含以下条目的 MANIFEST.MF:
<代码>...主类:fully.qualified.MainClass类路径:lib/dependency1-X.Y.jar lib/dependency2-W.Z.jar...
<块引用>
并创建以下目录结构(...)
这可以使用 Maven 依赖插件 和 dependency:copy-dependencies
目标.来自文档:
dependency:copy-dependencies
获取项目直接依赖项和可选传递依赖项的列表,并将它们复制到指定位置,如果需要,剥离版本.这个目标也可以从命令行运行.
你可以在 package
阶段绑定它:
<代码><项目>[...]<构建><插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><版本>2.1</版本><执行><执行><id>复制依赖项</id><阶段>包</阶段><目标><目标>复制依赖</目标></目标><配置><outputDirectory>${project.build.directory}/lib</outputDirectory><overWriteReleases>false</overWriteReleases><overWriteSnapshots>假</overWriteSnapshots><overWriteIfNewer>true</overWriteIfNewer></配置></执行></执行></插件></插件></构建>[...]</项目>
I'd like Maven to package a project alongside its run-time dependencies. I expect it to create a JAR file with the following manifest:
.....
Main-Class : com.acme.MainClass
Class-Path : lib/dependency1.jar lib/dependency2.jar
.....
and create the following directory structure:
target
|-- ....
|-- my-project.jar
|-- lib
|-- dependency1.jar
|-- dependency2.jar
Meaning, I want the main JAR to exclude any dependencies and I want all transitive dependencies to get copied into a "lib" sub-directory. Any ideas?
I've like Maven to package a project with run-time dependencies.
This part is unclear (it's not exactly what you describe just after). My answer covers what you described.
I expect it to create a JAR file with the following manifest (...)
Configure the Maven Jar Plugin to do so (or more precisely, the 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>X.Y</version>
</dependency>
<dependency>
<groupId>dependency2</groupId>
<artifactId>dependency2</artifactId>
<version>W.Z</version>
</dependency>
</dependencies>
...
</project>
And this will produce a MANIFEST.MF with the following entries:
...
Main-Class: fully.qualified.MainClass
Class-Path: lib/dependency1-X.Y.jar lib/dependency2-W.Z.jar
...
and create the following directory structure (...)
This is doable using the Maven Dependency Plugin and the dependency:copy-dependencies
goal. From the documentation:
dependency:copy-dependencies
takes the list of project direct dependencies and optionally transitive dependencies and copies them to a specified location, stripping the version if desired. This goal can also be run from the command line.
You could bind it on the package
phase:
<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>
这篇关于Maven:将依赖项与项目 JAR 一起打包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!