如何将项目的运行时依赖项复制到target/lib文件夹中?
现在,在mvn clean install之后,target文件夹仅包含我项目的jar,而没有任何运行时依赖项。
这对我有用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <project>
...
<profiles>
<profile>
<id>qa</id>
<build>
<plugins>
<plugin>
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> |
最佳方法取决于您想做什么:
-
如果要将依赖项捆绑到WAR或EAR文件中,则只需将项目的打包类型设置为EAR或WAR。 Maven会将依赖项捆绑到正确的位置。
-
如果要创建一个包含代码以及所有依赖项的JAR文件,请使用带有jar-with-dependencies描述符的程序集插件。 Maven将使用您的所有类以及来自任何依赖项的类生成一个完整的JAR文件。
-
如果您想简单地以交互方式将依赖项拉入目标目录,请使用依赖项插件将文件复制到其中。
-
如果您想为其他类型的处理引入依赖关系,则可能需要生成自己的插件。有一些API可以获取依赖项列表及其在磁盘上的位置。您将不得不从那里拿走...
1
| mvn install dependency:copy-dependencies |
对我有用,它在目标文件夹中创建了依赖项目录。喜欢它!
看一下Maven依赖插件,特别是dependency:copy-dependencies目标。看一下标题" dependency:copy-dependencies mojo"下的示例。将outputDirectory配置属性设置为$ {basedir} / target / lib(我相信,您必须进行测试)。
希望这可以帮助。
一个简单而优雅的解决方案,适用于需要将依赖项复制到目标目录而无需使用其他任何Maven阶段的情况(我发现使用Vaadin时这非常有用)。
完整的pom示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
artifactId</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${targetdirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> |
然后运行mvn process-sources
jar文件的依赖关系可以在/target/dependency中找到
尝试这样的事情:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <plugin>
<groupId>org.apache.maven.plugins</groupId>
maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<manifest>
true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin> |
如果您想偶尔执行此操作(因此不想更改您的POM),请尝试以下命令行:
1
| mvn dependency:copy-dependencies -DoutputDirectory=${project.build.directory}/lib |
如果省略最后一个参数,则相关性将放置在target/dependencies中。
您所需要做的就是pom.xml build/plugins中的以下代码段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <plugin>
maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin> |
上面的代码将在package阶段运行
并将相关性复制到代码段中指定的outputDirectory中,在这种情况下为lib。
如果您只想偶尔执行此操作,则无需更改pom.xml。 只需运行以下命令:
1
| mvn clean package dependency:copy-dependencies |
要覆盖默认位置${project.build.directory}/dependencies,请添加一个名为outputDirectory的系统属性,即
1
| -DoutputDirectory=${project.build.directory}/lib |
假如
-
您不想更改pom.xml
-
您不希望测试范围(例如junit.jar)或提供的依赖项(例如wlfullclient.jar)
这对我有用:
1
| mvn install dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=target/lib |
如果要交付捆绑的应用程序jar,所有依赖关系以及一些调用MainClass的脚本,请查看appassembler-maven-plugin。
以下配置将为Window和Linux生成脚本以启动应用程序(生成的路径引用所有依赖项jar,下载所有依赖项(到target / appassembler下的lib文件夹中),然后可以使用Assembly插件来打包整个程序。 appassembler目录到一个zip文件,该zip文件与jar一起安装/部署到存储库。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| <plugin>
<groupId>org.codehaus.mojo</groupId>
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>
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中)为:
1 2 3 4 5 6 7 8 9 10 11
| <id>archive</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/appassembler</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly> |
如果您使项目成为战争或战争类型,那么maven将复制依赖关系。
这是用于嵌入大量依赖项的繁重解决方案,但是Maven的Assembly Plugin为我解决了这个难题。
@Rich Seller's的答案应该有用,尽管在更简单的情况下,您只需要使用指南中的以下摘录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <project>
<build>
<plugins>
<plugin>
maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project> |
只是为了阐明已经简短说过的话。我想创建一个可执行的JAR文件,其中包含我的依赖关系以及我的代码。这对我有用:
(1)在pom的 下,我包括:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <plugin>
maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<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
您可以使用Shade插件创建一个uber jar,在其中可以捆绑所有第3方依赖性。
如果在Eclipse的Tomcat服务器上运行时遇到与依赖关系相关的问题,这些依赖关系未出现在WEB-INF / lib文件中,请查看以下内容:
启动Tomcat时发生ClassNotFoundException DispatcherServlet(Maven依赖关系未复制到wtpwebapps)
您只需要在"项目属性">"部署程序集"中添加Maven依赖项。