Question

I am currently working on a multi-module project with the following structure.

root
  -module A
  -module B

What I want to do is to execute module B (The main method of the module) after the compiling of module B (Module B depends on module A). But I need to do this with a customized command.

Ex -

mvn runb

I know that the exec maven plugin can be used to run a project using maven. What I don't understand is how to create a custom command (phase) in maven. Is there anyway to achieve this without writing a maven plugin?

I referred various guides such as https://community.jboss.org/wiki/CreatingACustomLifecycleInMaven trying to achieve this. But they need to create components.xml and lifecycle.xml files under src/resources/META-INF. I don't understand how to apply that file structure to my project since it is a multi-module project where each module has seperate src directories.

(I'm using maven 3)

Was it helpful?

Solution

You cannot create a custom lifecycle without writing a Maven plugin.

And without hacking Maven itself, at least as of Maven 3.0.5, it is not possible to add a custom phase to Maven through a plugin. The phases are loaded up by the core of Maven from its configuration before any plugins are processed.

If you really have your heart set on using one command to do what you want, writing a plugin is the only way. With some pluginGroup mappings in your settings.xml, this can be made simpler (you can specify mvn my:plugin rather than mvn com.mygroupid:plugin).

But if you are willing to have a slightly more verbose syntax on the command line, what you want could be achieved through profiles and the exec maven plugin.


Add a profile to module B that uses the exec plugin to run itself.

Something like this:

<project>
    ...
    <profiles>
        <!-- This profile uses local webapp security instead of the BlueCoat device, useful for testing -->
        <profile>
            <id>execb</id>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.2.1</version>

                        <executions>
                            <execution>
                                <id>runb</id>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                                <phase>verify</phase> <!-- Anything after package phase -->
                                <configuration>
                                    <!-- Exec plugin configuration goes here -->
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

You'll need to configure the exec plugin depending on how you run your JAR, more info here.

What this does is run the exec plugin as part of module B's build, but only if the execb profile is activated.


Now, when you just want to build your project (without any exec), build like normal (e.g. mvn install).

When you want to build and run, use the command line:

mvn install -Pexecb

and it will do the exec as part of the build.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top