Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java maven exec-maven-plugin not executing on mvn clean install

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
]
like image 619
bobanahalf Avatar asked Dec 19 '22 14:12

bobanahalf


2 Answers

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

like image 182
Ashish Avatar answered May 01 '23 12:05

Ashish


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!

like image 30
bobanahalf Avatar answered May 01 '23 11:05

bobanahalf