maven 工程导出所有 JAR 依赖包
原创
maven
开发时我们也许因为各种情况,需要导出 maven 的所有 dependency 项,如果去 maven 仓库找会显的相当繁琐。
下面是简单的配置可以帮助我们轻松的得到所有的依赖 jar。
这里需要添加 maven-dependency-plugin 这个插件。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
下面主要介绍两种常见使用:<goal> 为 copy,和 copy-dependencies 的情况。
copy
以下代码为 copy 的设置,此时需要自己指定 artifactItem (也就是需要打包的项)。outputDirectory 为 jar 包输出目录。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</artifactItem>
</artifactItems>
<outputDirectory>target/lib</outputDirectory>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
</plugin>
此时运行 maven 打包命令: mvn clean package -Dmaven.test.skip=true。会在 target/lib 目录下生成我们配置的 jar 包。
copy-dependencies
copy-dependencies 是指当前工程依赖包,它已经关联了我们 pom.xml 中所有 dependencies 依赖的 jar。
而我们不需要导出的 jar 包,可以用 excludeArtifactIds 排除,这里我们选择排除测试工具包 junit。然后同样可以指定生成目录。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<excludeArtifactIds>
junit
</excludeArtifactIds>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
如图,工程中的依赖关系和配置:

maven 打包后导出了所有的依赖包:

然后就可以得到我们想要的 jar 包了。是不是很方便了。
maven-dependency-plugin 的更多使用可以查看:http://maven.apache.org/components/plugins/maven-dependency-plugin/usage.html