如何将项目的运行时依赖项复制到target/lib
文件夹中?
现在,mvn clean install
该target
文件夹仅包含我项目的jar,但没有包含任何运行时依赖项。
这对我有用:
<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>
最佳方法取决于您想做什么:
- 如果要将依赖项捆绑到WAR或EAR文件中,则只需将项目的打包类型设置为EAR或WAR。Maven会将依赖项捆绑到正确的位置。
- 如果要创建一个包含代码和所有依赖项的JAR文件,请使用带有jar-with-dependencies描述符的程序集插件。Maven将使用您的所有类以及来自任何依赖项的类生成一个完整的JAR文件。
- 如果您只是想简单地以交互方式将依赖项拉到目标目录中,请使用依赖项插件将文件复制到其中。
- 如果您想为其他类型的处理引入依赖关系,则可能需要生成自己的插件。有一些API可以获取依赖项列表及其在磁盘上的位置。您将不得不从那里拿走...
mvn install dependency:copy-dependencies
对我有用,在目标文件夹中创建了依赖项目录。喜欢它!
看一看Maven依赖插件,特别是dependency:copy-dependencies目标。看一下标题“ dependency:copy-dependencies mojo”下的示例。将outputDirectory配置属性设置为$ {basedir} / target / lib(我相信,您必须进行测试)。
希望这可以帮助。
一个简单而优雅的解决方案,适用于需要将依赖项复制到目标目录而无需使用其他任何Maven阶段的情况(我发现使用Vaadin时这非常有用)。
完整的pom示例:
<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</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>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
如果您希望偶尔执行此操作(因此不想更改您的POM),请尝试以下命令行:
mvn依赖项:复制依赖项-DoutputDirectory = $ {project.build.directory} / lib
如果省略最后一个参数,则将依赖项放在中target/dependencies
。
尝试这样的事情:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>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.xml中的以下代码段build/plugins
:
<plugin>
<artifactId>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
您运行时分阶段运行
mvn clean package
然后,依赖关系将复制到代码段中指定的outputDirectory中,lib
在这种情况下。
如果您只想偶尔执行此操作,则无需更改pom.xml。只需运行以下命令:
mvn clean package dependency:copy-dependencies
要覆盖默认位置,请${project.build.directory}/dependencies
添加一个名为的系统属性outputDirectory
,即
-DoutputDirectory=${project.build.directory}/lib
假如
- 您不想更改pom.xml
- 您不希望测试作用域(例如junit.jar)或提供的依赖项(例如wlfullclient.jar)
这对我有用:
mvn安装依赖项:复制依赖项-DincludeScope =运行时-DoutputDirectory = target / lib
如果要交付捆绑的应用程序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>
如果您使项目成为战争或战争类型,那么maven将复制依赖关系。
您可以使用Shade插件创建一个uber jar,在其中可以捆绑所有第3方依赖性。
只是为了阐明已经简短说过的话。我想创建一个可执行的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
这是嵌入大量依赖项的繁重解决方案,但是Maven的Assembly Plugin为我解决了这个难题。
@ Rich Seller的答案应该有用,尽管对于更简单的情况,您只需要使用指南中的以下摘录:
<project>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
如果在Eclipse的Tomcat服务器上运行时遇到与依赖关系相关的问题,这些依赖关系未出现在WEB-INF / lib文件中,请查看以下内容:
启动Tomcat时发生ClassNotFoundException DispatcherServlet(Maven依赖关系未复制到wtpwebapps)
您只需要在“项目属性”>“部署程序集”中添加Maven依赖项。
文章标签:dependencies , java , maven , maven-2 , maven-3
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!