maven-surefire-plugin 忽略 pom.xml 中的插件顺序
Posted
技术标签:
【中文标题】maven-surefire-plugin 忽略 pom.xml 中的插件顺序【英文标题】:maven-surefire-plugin ignores plugins order in pom.xml 【发布时间】:2015-09-22 09:26:08 【问题描述】:我有多模块 Maven 项目。该项目包含自写的 maven 插件和该插件的测试项目。
我想在 maven-surefire-plugin 之前的test
阶段运行我的插件
我在 pom.xml 中的插件之后放置了 maven-surefire-plugin
<build>
<plugins>
<plugin>
<groupId>com.my.group</groupId>
<artifactId>validator-maven-plugin</artifactId>
<version>$project.version</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
但是 maven-surefire-plugin 仍然首先运行。
[INFO] ------------------------------------------------------------------------
[INFO] Building PC_TEST_PROJECT 0.23-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ PC_TEST_PROJECT ---
[INFO] Deleting C:\validator\PC_TEST_PROJECT\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ PC_TEST_PROJECT ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\validator\PC_TEST_PROJECT\src\main\java
[INFO] skip non existing resourceDirectory C:\validator\PC_TEST_PROJECT\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ PC_TEST_PROJECT ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ PC_TEST_PROJECT ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ PC_TEST_PROJECT ---
[INFO] Compiling 1 source file to C:\validator\PC_TEST_PROJECT\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ PC_TEST_PROJECT ---
[INFO] Surefire report directory: C:\validator\PC_TEST_PROJECT\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.my.group.validator.MainTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.657 sec - in com.my.group.validator.MainTest
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] >>> validator-maven-plugin:0.23-SNAPSHOT:validate (default) > test @ PC_TEST_PROJECT >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ PC_TEST_PROJECT ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\validator\PC_TEST_PROJECT\src\main\java
[INFO] skip non existing resourceDirectory C:\validator\PC_TEST_PROJECT\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ PC_TEST_PROJECT ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ PC_TEST_PROJECT ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ PC_TEST_PROJECT ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ PC_TEST_PROJECT ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default) @ PC_TEST_PROJECT ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO]
[INFO] <<< validator-maven-plugin:0.23-SNAPSHOT:validate (default) < test @ PC_TEST_PROJECT <<<
[INFO]
[INFO] --- validator-maven-plugin:0.23-SNAPSHOT:validate (default) @ PC_TEST_PROJECT ---
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default) @ PC_TEST_PROJECT ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] validator-parent ................................... SUCCESS [ 0.175 s]
[INFO] validator .......................................... SUCCESS [ 6.038 s]
[INFO] validator-maven-plugin ............................. SUCCESS [ 2.012 s]
[INFO] PC_TEST_PROJECT .................................... SUCCESS [ 2.762 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.298 s
[INFO] Finished at: 2015-07-05T00:46:59+03:00
[INFO] Final Memory: 21M/51M
[INFO] ------------------------------------------------------------------------
这是 maven-surefire-plugin 的预期行为吗? 如何改变插件的执行顺序?
【问题讨论】:
这里使用默认执行,可能跳过 default-test 执行并添加你的可以工作,或者只是将验证器绑定到 process-test-resources @Tome 如何跳过default-test
?
看看那里:***.com/questions/15249345/…,只需将 default-testResources 更改为 default-test
Maven 插件目标默认绑定到生命周期阶段(作为默认生命周期中test
阶段的test
目标),在通过 pom 声明添加插件目标之前执行,无论什么。 @Tome 是对的,您必须在 test-compile
阶段执行您自己的插件(这将遵循默认绑定到该阶段的编译器插件的 testCompile
目标),以便在surefire插件之前运行它。所以是的,这是预期的 Maven 行为 - 不一定是万能的插件行为。
【参考方案1】:
试试这个:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.stefanheimberg.***</groupId>
<artifactId>***-31225404</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>run-before-surfire</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message=" !!!! THIS SHOULD RUN BEFORE SUREFIRE !!!! " />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<phase>none</phase>
</execution>
<execution>
<id>run-after-antrun</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
和控制台输出:
cd /Users/stefanheimberg/git/***-31225404; JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home "/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/java/maven/bin/mvn" clean install
Scanning for projects...
Some problems were encountered while building the effective model for ch.stefanheimberg.***:***-31225404:jar:1.0-SNAPSHOT
'build.plugins.plugin.version' for org.apache.maven.plugins:maven-surefire-plugin is missing. @ line 31, column 21
It is highly recommended to fix these problems because they threaten the stability of your build.
For this reason, future Maven versions might no longer support building such malformed projects.
------------------------------------------------------------------------
Building ***-31225404 1.0-SNAPSHOT
------------------------------------------------------------------------
--- maven-clean-plugin:2.4.1:clean (default-clean) @ ***-31225404 ---
Deleting /Users/stefanheimberg/git/***-31225404/target
--- maven-resources-plugin:2.5:resources (default-resources) @ ***-31225404 ---
[debug] execute contextualize
Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory /Users/stefanheimberg/git/***-31225404/src/main/resources
--- maven-compiler-plugin:2.3.2:compile (default-compile) @ ***-31225404 ---
No sources to compile
--- maven-resources-plugin:2.5:testResources (default-testResources) @ ***-31225404 ---
[debug] execute contextualize
Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory /Users/stefanheimberg/git/***-31225404/src/test/resources
--- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ ***-31225404 ---
No sources to compile
--- maven-antrun-plugin:1.3:run (run-before-surfire) @ ***-31225404 ---
Executing tasks
[echo] !!!! THIS SHOULD RUN BEFORE SUREFIRE !!!!
Executed tasks
--- maven-surefire-plugin:2.10:test (run-after-antrun) @ ***-31225404 ---
No tests to run.
Surefire report directory: /Users/stefanheimberg/git/***-31225404/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
--- maven-jar-plugin:2.3.2:jar (default-jar) @ ***-31225404 ---
JAR will be empty - no content was marked for inclusion!
Building jar: /Users/stefanheimberg/git/***-31225404/target/***-31225404-1.0-SNAPSHOT.jar
--- maven-install-plugin:2.3.1:install (default-install) @ ***-31225404 ---
Installing /Users/stefanheimberg/git/***-31225404/target/***-31225404-1.0-SNAPSHOT.jar to /Users/stefanheimberg/.m2/repository/ch/stefanheimberg/***/***-31225404/1.0-SNAPSHOT/***-31225404-1.0-SNAPSHOT.jar
Installing /Users/stefanheimberg/git/***-31225404/pom.xml to /Users/stefanheimberg/.m2/repository/ch/stefanheimberg/***/***-31225404/1.0-SNAPSHOT/***-31225404-1.0-SNAPSHOT.pom
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.888s
Finished at: Tue Jul 07 00:41:57 CEST 2015
Final Memory: 8M/155M
------------------------------------------------------------------------
这里的诀窍是,首先将您的插件(在我的 antrun 中)绑定到测试阶段,然后取消绑定“默认测试”阶段。在此之后,您可以再次将 maven-surfire 绑定到测试阶段。
【讨论】:
【参考方案2】:在同一阶段,maven 应该按照 pom.xml 中列出的顺序执行插件。更新您的 Maven 版本:
对于 maven 3,自 3.0.3 版起为 fixed。
对于 maven 2,它是 fixed,因为版本 2.0.11 和 2.1.0
【讨论】:
以上是关于maven-surefire-plugin 忽略 pom.xml 中的插件顺序的主要内容,如果未能解决你的问题,请参考以下文章
maven-surefire-plugin,用于自动化测试和单元测试的
maven-surefire-plugin 包含/排除优先级
无法加载 mojo 'test' - org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M1:test