利用Maven插件将依赖包、jar/war包及配置文件输出到指定目录
写在前面
最近遇到一个朋友遇到一个项目需要将maven的依赖包和配置文件分开打包然后用脚本执行程序。这样的好处在于可以随时修改配置文件内容及查看jar包。如果将所有打成一个jar包就会有个问题(例如:修改数据库连接位置需要重新打包这样就失去了使用配置文件的有优点)
1. 使用maven-dependency-plugin 插件将依赖包导出到指定文件夹
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!--${project.build.directory} class的输出目录不做设置的话默认代表项目根目录的target目录;也可以使用“自定义文件夹/自定义文件夹 例如:a/b”--> <outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2. 使用maven-jar-plugin插件移除配置文件将其不打包进jar包中,并可以将打包后的jar包输出到指定路径
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 指定打包的jar包输出路径-->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
<!--不打入jar包的文件类型或者路径-->
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.yml</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
</excludes>
</configuration>
</plugin>
3. 使用maven-resources-plugin插件将需要复制的文件复制到指定路径(例:将配置文件提取到指定路径)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<!--打成jar包后复制到的路径-->
<outputDirectory>
${project.build.directory}/conf
</outputDirectory>
<resources>
<resource>
<!--项目中的路径-->
<directory>src/main/resources/</directory>
</resource>
</resources>
</configuration>
</execution>
<!--可配置多个提取复制路径只需要 “<id>”名字不一样即可-->
<execution>
<id>copy-bulid</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>
${project.build.directory}/bin
</outputDirectory>
<resources>
<resource>
<directory>build/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
使用maven命令执行打包命令
mvn clean package