4

上一个问题的后续: Maven run class before test phase: exec-maven-plugin exec:java not execution class

我在詹金斯盒子上运行 jUnit4 测试,用 maven 构建。在构建的测试阶段之前,我需要运行一个特定的 main-method java 程序。目的是在测试运行之前恢复测试数据库。

如果我运行分配给这个 exec 的确切阶段,我的类将按预期执行;但是当我运行整个构建时,我的类没有执行:

具体来说,它运行:
mvn -X exec:java generate-test-resources

但不运行:
mvn -X -e install
--或--
mvn -X -e clean install

pom.xml: 我的 pom.xml 文件包括:

<pluginManagement>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>            
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>build-test-environment</id>
                    <phase>generate-test-resources</phase>          
                    <goals>
                        <goal>java</goal>           
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>main.java._tools.BuildTestEnvironment</mainClass>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

生命周期默认值: 我没有对 maven 的生命周期感到厌烦。日志将其报告为:

[DEBUG] Lifecycle default -> [
    validate,
    initialize,
    generate-sources,
    process-sources,
    generate-resources,
    process-resources,
    compile,
    process-classes,
    generate-test-sources,
    process-test-sources,
    generate-test-resources,
    process-test-resources,
    test-compile,
    process-test-classes,
    test,
    prepare-package,
    package,
    pre-integration-test,
    integration-test,
    post-integration-test,
    verify,
    install,
    deploy
]
4

2 回答 2

8

使用在 下定义的插件<pluginManagement>,您实际上是在告诉 maven 当您调用插件时,您将在整个项目中使用哪个版本的插件。我通常希望<pluginManagement>标签出现在父 pom.xml 中。

要调用插件 - 只需放置<plugins/>元素。它可能继承也可能不继承自

因此,要使用插件,您只需要通过放置来调用插件

<plugins>
        <plugin>            
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>build-test-environment</id>
                    <phase>generate-test-resources</phase>          
                    <goals>
                        <goal>java</goal>           
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>main.java._tools.BuildTestEnvironment</mainClass>
            </configuration>
        </plugin>
    ...AnyOtherPlugin
    <plugins>

没有任何<pluginManagement>标签

于 2014-05-15T20:13:46.533 回答
4

Ashish 正在处理它:<pluginManagement>正在杀死它。

我认为我需要<pluginManagement>因为 Eclipse 和 m2e 中的问题。原来 m2e 的人有一个解决方案(一个非常复杂的解决方案)。

看:

如何解决 Spring Data Maven Builds 的“生命周期配置未涵盖的插件执行”

- 和 -

http://wiki.eclipse.org/M2E_plugin_execution_not_covered

祝你好运,如果你遇到这个!

于 2014-05-15T20:19:06.857 回答