Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Maven3 exec plugin with no inheritance

Tags:

java

maven

exec

I have a multi module maven java project where I want to execute the exec plugin in order to execute a custom command after the Jars has been created.

I call maven package assembly:assembly exec:exec on the parent POM to create the project output.

I used the following in the parent POM:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <configuration>
            <executable>myExecutable.exe</executable>
            <workingDirectory>${basedir}</workingDirectory>
            <arguments>....</arguments>
        </configuration>
    </plugin>

When doing so, my executable got executed correctly but was executed for every child module as well.

In order to try and fix it, I've set the plugin to not get inherit to the child modules:

<inherited>false</inherited>

But now the exec plugin fails with a The parameters 'executable' for goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec are missing or invalid error.

I've tried setting the plugin to run under the "executions" element and assign it to a maven life cycle phase. This caused the executable to run successfully, but I am unable to do this since I need to execute other plugins (assembly plugin) before I execute this exec plugin.

How can I run this (exec) plugin just once after the package phase has completed and the other plugin (assembly) has been finished as well?

In other words, I want to execute 'package' for all of my child modules and then execute the exec plugin just once from the parent.

I will appreciate any help.

like image 628
Omri Avatar asked Nov 13 '22 04:11

Omri


1 Answers

When you run a command at the parent POM level, you are telling Maven to invoke that part of the lifecycle for every module.

If you only want to invoke exec:exec for the child module, you should only declare that plugin for the child module (as it has no meaning for other modules that inherit the parent) and invoke Maven with the --projects or -pl argument:

mvn -pl child package assembly:assembly exec:exec

This command, when executed from the parent project, executes package assembly:assembly exec:exec for only the child project.

However, if what you really are trying to accomplish is that you can package all of the modules in a single command, and you want the assembly:assembly and exec:exec goals to be executed for that child module during the package phase, then you want to bind these plugins to that phase. For example:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
        <executable>myExecutable.exe</executable>
        <workingDirectory>${basedir}</workingDirectory>
        <arguments>....</arguments>
    </configuration>
    <executions>
        <!-- run the exec goal of this plugin every time this module is packaged -->
        <execution>
            <phase>package</phase>
            <goals><goal>exec</goal></goals>
        </execution>
    </executions>
</plugin>
like image 144
matt b Avatar answered Nov 14 '22 22:11

matt b