Maven插件goal绑定test阶段后执行mvn test未触发如何解决
问题原因
你将maven-antrun-plugin的执行配置放在了<pluginManagement>节点下,该节点的作用是统一管理插件的版本、配置模板,供子模块继承使用,本身不会激活当前项目的插件生命周期绑定,因此你配置的test阶段绑定不会自动触发。而直接执行mvn antrun:run@ant-echo时,Maven可以直接匹配到pluginManagement中的对应执行配置,因此可以正常运行。
解决方法
有两种常用修正方案:
方案1:将插件配置移出pluginManagement,直接放在<build><plugins>节点下
修改后的build节点配置如下:
<build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> </plugins> </pluginManagement> <!-- 新增外层plugins节点,放置需要当前项目生效的插件配置 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>ant-echo</id> <phase>test</phase> <configuration> <target name="app2"> <echo level="info">This is how you echo information into the console.</echo> <echo level="info">Echo levels defined in Ant are the following </echo> <echo level="info">error, warning, info, verbose, debug</echo> <echo level="info"></echo> <echo level="info">The following is the way to echo property values.</echo> <echo level="info">${project.var1}</echo> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
方案2:保留pluginManagement中的插件配置,在外层plugins中声明引用
如果需要统一管理插件版本,也可以将插件版本和配置放在pluginManagement中,外层仅声明插件即可自动继承配置:
<build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>ant-echo</id> <phase>test</phase> <configuration> <target name="app2"> <echo level="info">This is how you echo information into the console.</echo> <echo level="info">Echo levels defined in Ant are the following </echo> <echo level="info">error, warning, info, verbose, debug</echo> <echo level="info"></echo> <echo level="info">The following is the way to echo property values.</echo> <echo level="info">${project.var1}</echo> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> <!-- 外层声明插件,激活配置 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> </plugin> </plugins> </build>
修正配置后执行mvn test即可看到ant任务的输出内容。
内容的提问来源于stack exchange,提问作者Rahul Khimasia
相关产品推荐
相关产品推荐

