我如何让我的项目的运行时依赖复制到目标/lib文件夹?
就像现在一样,在mvn清洁安装后,目标文件夹只包含我的项目的jar,但没有运行时依赖项。
我如何让我的项目的运行时依赖复制到目标/lib文件夹?
就像现在一样,在mvn清洁安装后,目标文件夹只包含我的项目的jar,但没有运行时依赖项。
当前回答
试试这样做:
<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 你不想要测试范围(例如junit.jar)或提供的依赖关系(例如wlfullclient.jar)
以下是对我有效的方法:
mvn install dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=target/lib
你可以使用Shade插件创建一个超级罐子,你可以在里面捆绑你所有的第三方依赖。
最好的方法取决于你想做什么:
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 assembly plugin with the jar-with-dependencies descriptor. 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 dependency plugin 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...
试试这样做:
<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),试试这个命令行:
mvn dependency:copy-dependencies -DoutputDirectory=${project.build.directory}/lib
如果省略最后一个参数,依赖项将放在target/dependencies中。