5

Follow-up to a previous question: Maven run class before test phase: exec-maven-plugin exec:java not executing class.

I am running jUnit4 tests, built with maven, on a jenkins box. I need to run a specific main-method java program before before the test phase of my build. The intent is to restore a test database before the tests run.

If I run the exact phase this exec is assigned to, my class executes as expected; but when I run the whole build, my class does not execute:

Specifically, it runs with:
mvn -X exec:java generate-test-resources

But does not run with:
mvn -X -e install
-- or --
mvn -X -e clean install

pom.xml: My pom.xml file includes:

<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>

Lifecycle default: I have not futzed with maven's lifecycle. The log reports it as:

[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
]
Community
  • 1
  • 1
bobanahalf
  • 829
  • 1
  • 11
  • 23

2 Answers2

8

With your plugin defined under the <pluginManagement>, you are actually telling maven which version of the plugin you will use throughout your project when you will invoke the plugin. I would normally expect the <pluginManagement> tag to be present in the parent pom.

To invoke the plugin - simply put the <plugins/> element. It may or may not be inherited from a

Hence to use the plugin, you just need to invoke the plugin by putting

<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>

without any <pluginManagement> tag

Ashish
  • 510
  • 3
  • 14
5

Ashish was on to it: <pluginManagement> was killing it.

I thought I needed <pluginManagement> because of problems in Eclipse and m2e. Turns out the m2e guys had a solution (a very complicated solution).

See:

How to solve "Plugin execution not covered by lifecycle configuration" for Spring Data Maven Builds

-- and --

http://wiki.eclipse.org/M2E_plugin_execution_not_covered

Good luck, if you run into this!

Community
  • 1
  • 1
bobanahalf
  • 829
  • 1
  • 11
  • 23
  • Wonder why this was downvoted. I don't care about the rep point (whatever), but I could be a better citizen if I knew what was wrong with this post. – bobanahalf Dec 18 '17 at 16:58
  • 1
    I agree, I'm not sure why someone would downvote and not give you an idea of how it could be improved. I've upvoted to get your answer at least out of the negative (for now) because I see your answer as being thorough and helpful. Thanks! – Danny Bullis May 31 '18 at 23:00