Java 强制 Maven 将依赖项复制到 target/lib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/97640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
force Maven to copy dependencies into target/lib
提问by Michael
How do I get my project's runtime dependencies copied into the target/lib
folder?
如何将项目的运行时依赖项复制到target/lib
文件夹中?
As it is right now, after mvn clean install
the target
folder contains only my project's jar, but none of the runtime dependencies.
因为它是现在,后mvn clean install
的target
文件夹仅包含我的项目的罐子,但没有运行时依赖的。
回答by Eduard Wirch
If you make your project a war or ear type maven will copy the dependencies.
如果您将项目设为 war 或 ear 类型,maven 将复制依赖项。
回答by Brian Matthews
You can use the the Shade Pluginto create an uber jar in which you can bundle all your 3rd party dependencies.
您可以使用Shade 插件创建一个 uber jar,您可以在其中捆绑所有 3rd 方依赖项。
回答by Travis B. Hartwell
Take a look at the Maven dependency plugin, specifically, the dependency:copy-dependencies goal. Take a look at the exampleunder the heading The dependency:copy-dependencies mojo. Set the outputDirectoryconfiguration property to ${basedir}/target/lib (I believe, you'll have to test).
看看Maven 依赖插件,特别是dependency:copy-dependencies 目标。查看标题The dependency:copy-dependencies mojo下的示例。将outputDirectory配置属性设置为 ${basedir}/target/lib (我相信,您必须进行测试)。
Hope this helps.
希望这可以帮助。
回答by John Stauffer
The best approach depends on what you want to do:
最好的方法取决于你想做什么:
- If you want to bundle your dependencies into a WAR or EAR file, then simply set the packaging type of your project to EAR or WAR. Maven will bundle the dependencies into the right location.
- If you want to create a JAR file that includes your code along with all your dependencies, then use the assemblyplugin with the jar-with-dependenciesdescriptor. Maven will generate a complete JAR file with all your classes plus the classes from any dependencies.
- If you want to simply pull your dependencies into the target directory interactively, then use the dependencyplugin to copy your files in.
- If you want to pull in the dependencies for some other type of processing, then you will probably need to generate your own plugin. There are APIs to get the list of dependencies, and their location on disk. You will have to take it from there...
回答by Georgy Bolyuba
This works for me:
这对我有用:
<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>
回答by Rich Seller
If you want to deliver a bundle of your application jar, together with all its dependencies and some scripts to invoke the MainClass, look at the appassembler-maven-plugin.
如果您想提供应用程序 jar 包及其所有依赖项和一些调用 MainClass 的脚本,请查看appassembler-maven-plugin。
The following configuration will generate scripts for Window and Linux to launch the application (with a generated path referencing all the dependency jars, download all dependencies (into a lib folder below target/appassembler). The assembly plugincan then be used to package the whole appassembler directory to a zip which is installed/deployed along with the jar to the repository.
下面的配置会生成 Window 和 Linux 启动应用程序的脚本(生成的路径引用所有依赖 jar,下载所有依赖(到 target/appassembler 下的 lib 文件夹中)。然后可以使用组装插件打包整个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>
The assembly descriptor (in src/main/assembly) to package the direcotry as a zip would be:
将目录打包为 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>
回答by OleVV
Just to spell out what has already been said in brief. I wanted to create an executable JAR file that included my dependencies along with my code. This worked for me:
只是为了简要说明已经说过的内容。我想创建一个可执行的 JAR 文件,其中包含我的依赖项和我的代码。这对我有用:
(1) In the pom, under <build><plugins>, I included:
(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) Running mvn compile assembly:assembly produced the desired my-project-0.1-SNAPSHOT-jar-with-dependencies.jar in the project's target directory.
(2) 运行 mvn compile assembly:assembly 在项目的目标目录中生成了所需的 my-project-0.1-SNAPSHOT-jar-with-dependencies.jar。
(3) I ran the JAR with java -jar my-project-0.1-SNAPSHOT-jar-with-dependencies.jar
(3) 我用 java -jar my-project-0.1-SNAPSHOT-jar-with-dependencies.jar 运行了 JAR
回答by hemetsu
If you're having problems related to dependencies not appearing in the WEB-INF/lib file when running on a Tomcat server in Eclipse, take a look at this:
如果您在 Eclipse 中的 Tomcat 服务器上运行时遇到与依赖项未出现在 WEB-INF/lib 文件中相关的问题,请查看以下内容:
启动 Tomcat 时出现 ClassNotFoundException DispatcherServlet(Maven 依赖项未复制到 wtpwebapps)
You simply had to add the Maven Dependencies in Project Properties > Deployment Assembly.
您只需在项目属性 > 部署程序集中添加 Maven 依赖项。
回答by ruhsuzbaykus
A simple and elegant solution for the case where one needs to copy the dependencies to a target directory without using any other phases of maven (I found this very useful when working with Vaadin).
对于需要将依赖项复制到目标目录而不使用 Maven 的任何其他阶段的情况的简单而优雅的解决方案(我发现这在使用 Vaadin 时非常有用)。
Complete pom example:
完整的 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>
Then run mvn process-sources
然后运行 mvn process-sources
The jar file dependencies can be found in /target/dependency
jar 文件依赖项可以在 /target/dependency
回答by RubyTuesdayDONO
It's a heavy solution for embedding heavy dependencies, but Maven's Assembly Plugindoes the trick for me.
这是嵌入大量依赖项的重要解决方案,但 Maven 的Assembly Plugin为我解决了这个问题。
@Rich Seller's answershould work, although for simpler cases you should only need this excerpt from the usage guide:
@ 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>