简体   繁体   English

maven-surefire-plugin忽略pom.xml中的插件顺序

[英]maven-surefire-plugin ignores plugins order in pom.xml

I have multimodule maven project. 我有多模块maven项目。 This project contains self written maven plugin and test project for this plugin. 这个项目包含自编写的maven插件和这个插件的测试项目。 I want to run my plugin in test phase before maven-surefire-plugin I put maven-surefire-plugin after my plugin in pom.xml 我想在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>

But maven-surefire-plugin still runs first. 但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] ------------------------------------------------------------------------

Is it expected behavior for maven-surefire-plugin? 这是maven-surefire-plugin的预期行为吗? How to change order of plugins execution? 如何更改插件执行的顺序?

try this one: 试试这个:

<?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.stackoverflow</groupId>
    <artifactId>stackoverflow-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>

and the console output: 和控制台输出:

cd /Users/stefanheimberg/git/stackoverflow-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.stackoverflow:stackoverflow-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 stackoverflow-31225404 1.0-SNAPSHOT
------------------------------------------------------------------------

--- maven-clean-plugin:2.4.1:clean (default-clean) @ stackoverflow-31225404 ---
Deleting /Users/stefanheimberg/git/stackoverflow-31225404/target

--- maven-resources-plugin:2.5:resources (default-resources) @ stackoverflow-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/stackoverflow-31225404/src/main/resources

--- maven-compiler-plugin:2.3.2:compile (default-compile) @ stackoverflow-31225404 ---
No sources to compile

--- maven-resources-plugin:2.5:testResources (default-testResources) @ stackoverflow-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/stackoverflow-31225404/src/test/resources

--- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ stackoverflow-31225404 ---
No sources to compile

--- maven-antrun-plugin:1.3:run (run-before-surfire) @ stackoverflow-31225404 ---
Executing tasks
     [echo]  !!!! THIS SHOULD RUN BEFORE SUREFIRE !!!! 
Executed tasks

--- maven-surefire-plugin:2.10:test (run-after-antrun) @ stackoverflow-31225404 ---
No tests to run.
Surefire report directory: /Users/stefanheimberg/git/stackoverflow-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) @ stackoverflow-31225404 ---
JAR will be empty - no content was marked for inclusion!
Building jar: /Users/stefanheimberg/git/stackoverflow-31225404/target/stackoverflow-31225404-1.0-SNAPSHOT.jar

--- maven-install-plugin:2.3.1:install (default-install) @ stackoverflow-31225404 ---
Installing /Users/stefanheimberg/git/stackoverflow-31225404/target/stackoverflow-31225404-1.0-SNAPSHOT.jar to /Users/stefanheimberg/.m2/repository/ch/stefanheimberg/stackoverflow/stackoverflow-31225404/1.0-SNAPSHOT/stackoverflow-31225404-1.0-SNAPSHOT.jar
Installing /Users/stefanheimberg/git/stackoverflow-31225404/pom.xml to /Users/stefanheimberg/.m2/repository/ch/stefanheimberg/stackoverflow/stackoverflow-31225404/1.0-SNAPSHOT/stackoverflow-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
------------------------------------------------------------------------

the trick here is, to first bind your plugin (in my antrun) to the test phase, and after this, unbind the "default-test" phase. 这里的技巧是,首先将你的插件(在我的antrun中)绑定到测试阶段,然后取消绑定“默认测试”阶段。 after this you can bind the maven-surfire to the test phase again. 在此之后,您可以再次将maven-surfire绑定到测试阶段。

In the same phase, maven should execute plugins in the same order as they are listed in the pom.xml. 在同一阶段,maven应按照pom.xml中列出的顺序执行插件。 Update your maven version : 更新您的maven版本:

For maven 3, it is fixed since version 3.0.3. 对于maven 3,它从版本3.0.3开始修复

For maven 2, it is fixed since versions 2.0.11 and 2.1.0 对于maven 2,它从版本2.0.11和2.1.0开始修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 eclipse (pom.xml) 中的 Maven 错误:无法传输 org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 - Maven error in eclipse (pom.xml) : Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 在 pom.xml 中,我们可以将 maven-compiler-plugin 和 maven-surefire-plugin 放在依赖标签而不是插件标签下吗? - In pom.xml can we keep maven-compiler-plugin and maven-surefire-plugin under a dependency tag instead of plugin tag? 是否需要在pom.xml中指定Maven Surefire Plugin? - Is it required to specify Maven Surefire Plugin in pom.xml? 无法在现有 pom.xml 中添加 maven 编译器和肯定插件 - unable to add maven compiler and surefire plugin in the existing pom.xml Maven 和 POM.xml 中的插件 - Plugins in Maven and POM.xml Maven 忽略来自 pom.xml 的插件配置 - Maven ignores plugin configuration from pom.xml 无法从命令行覆盖pom maven-surefire-plugin - Cannot overwrite pom maven-surefire-plugin from command line Nexus存储库中缺少maven-surefire-plugin:pom:2.9 - maven-surefire-plugin:pom:2.9 missing from Nexus repository Maven Cargo 忽略 pom.xml 中的属性 - Maven Cargo ignores properties in pom.xml 无法将工件 org.apache.maven.plugins:maven-surefire-plugin:pom:2.7.1 从/到中央 (http://repo1.Z402C5D9AF6B270711EAZD70BEE574) - Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.7.1 from/to central (http://repo1.maven.org/maven2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM