强制Maven将依赖项复制到目标/库中
如何将项目的运行时依赖项复制到" target / lib"文件夹中?
现在,在执行" mvn clean install"之后," target"文件夹仅包含我项目的jar,但不包含任何运行时依赖项。
解决方案
如果我们使项目成为战争或者战争类型,那么maven将复制依赖关系。
我们可以使用Shade插件创建一个uber jar,在其中可以捆绑所有第3方依赖性。
看一下Maven依赖插件,特别是dependency:copy-dependencies目标。看一下标题" dependency:copy-dependencies mojo"下的示例。将outputDirectory配置属性设置为$ {basedir} / target / lib(我相信,我们必须进行测试)。
希望这可以帮助。
最佳方法取决于我们要执行的操作:
- 如果要将依赖项捆绑到WAR或者EAR文件中,则只需将项目的打包类型设置为EAR或者WAR。 Maven会将依赖项捆绑到正确的位置。
- 如果要创建一个包含代码以及所有依赖项的JAR文件,请使用带有jar-with-dependencies描述符的程序集插件。 Maven将使用所有类以及来自任何依赖项的类生成一个完整的JAR文件。
- 如果我们想简单地以交互方式将依赖项拉入目标目录,请使用依赖项插件将文件复制到其中。
- 如果我们想为其他类型的处理引入依赖关系,则可能需要生成自己的插件。有一些API可以获取依赖项列表及其在磁盘上的位置。我们将不得不从那里拿走...
这对我有用:
<project> ... <profiles> <profile> <id>qa</id> <build> <plugins> <plugin> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
如果要交付捆绑的应用程序jar,所有依赖关系以及一些调用MainClass的脚本,请查看appassembler-maven-plugin。
以下配置将为Window和Linux生成脚本以启动应用程序(生成的路径引用所有依赖项jar,下载所有依赖项(到target / appassembler下的lib文件夹中),然后可以使用Assembly插件来打包整个appassembler目录到一个zip文件,该zip文件与jar一起安装/部署到存储库。
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>appassembler-maven-plugin</artifactId> <version>1.0</version> <executions> <execution> <id>generate-jsw-scripts</id> <phase>package</phase> <goals> <goal>generate-daemons</goal> </goals> <configuration> <!--declare the JSW config --> <daemons> <daemon> <id>myApp</id> <mainClass>name.seller.rich.MyMainClass</mainClass> <commandLineArguments> <commandLineArgument>start</commandLineArgument> </commandLineArguments> <platforms> <platform>jsw</platform> </platforms> </daemon> </daemons> <target>${project.build.directory}/appassembler</target> </configuration> </execution> <execution> <id>assemble-standalone</id> <phase>integration-test</phase> <goals> <goal>assemble</goal> </goals> <configuration> <programs> <program> <mainClass>name.seller.rich.MyMainClass</mainClass> <!-- the name of the bat/sh files to be generated --> <name>mymain</name> </program> </programs> <platforms> <platform>windows</platform> <platform>unix</platform> </platforms> <repositoryLayout>flat</repositoryLayout> <repositoryName>lib</repositoryName> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-4</version> <executions> <execution> <phase>integration-test</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>src/main/assembly/archive.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>
将目录打包为zip的程序集描述符(在src / main / assembly中)为:
<assembly> <id>archive</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.build.directory}/appassembler</directory> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> </assembly>
只是为了阐明已经简短说过的话。我想创建一个可执行的JAR文件,其中包含我的依赖关系以及我的代码。这为我工作:
(1)在pom的<build> <plugins>下,我包括:
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <archive> <manifest> <mainClass>dk.certifikat.oces2.some.package.MyMainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
(2)运行mvn compile assembly:assembly在项目的目标目录中生成了所需的my-project-0.1-SNAPSHOT-jar-with-dependencies.jar。
(3)我使用java -jar my-project-0.1-SNAPSHOT-jar-with-dependencies.jar运行了JAR