如何在maven-dependency-plugin:copy-dependencies中按打包类型筛选构件
实现方案
直接使用maven-dependency-plugin的copy-dependencies目标原生提供的includeTypes参数即可实现按打包类型过滤构件,无需逐个指定依赖,新增依赖时只要类型匹配会自动纳入复制范围,可维护性高。
完整配置示例如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.0</version> <executions> <!-- 任务1:仅复制WAR类型依赖到指定目录 --> <execution> <id>copy-war-dependencies</id> <phase>pre-integration-test</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- 替换为你的测试服务WAR包存放目录 --> <outputDirectory>${project.build.directory}/test-server/webapps</outputDirectory> <includeTypes>war</includeTypes> <!-- 按需指定依赖scope,你声明的第三方WAR包为provided scope,可保留该配置过滤其他scope依赖 --> <includeScope>provided</includeScope> </configuration> </execution> <!-- 任务2:仅复制JAR类型依赖到指定目录 --> <execution> <id>copy-jar-dependencies</id> <phase>pre-integration-test</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- 替换为你的测试服务JAR包存放目录 --> <outputDirectory>${project.build.directory}/test-server/lib</outputDirectory> <includeTypes>jar</includeTypes> <includeScope>provided</includeScope> </configuration> </execution> </executions> </plugin>
补充说明
- 若需要同时匹配多个打包类型,可直接用英文逗号分隔,例如
<includeTypes>jar,aar</includeTypes> - 若需要排除少数特定类型,可搭配使用
<excludeTypes>参数,适合保留绝大多数类型仅过滤少量类型的场景 - 可以根据实际部署需求调整执行任务绑定的Maven生命周期阶段,例如
package、install等 includeTypes参数从maven-dependency-plugin 2.0版本开始支持,主流Maven版本均兼容,建议使用3.x版本获得更稳定的过滤效果
内容的提问来源于stack exchange,提问作者Javier Vilarroig
相关产品推荐
相关产品推荐

