Walle
Walle

Reputation: 570

Running a maven plugin only in the parent

I've created a maven plugin to start, clean and stop a database. My project consists of a pom file with 4 modules:

<modules>
    <module>infrastructure</module>
    <module>domain</module>
    <module>application</module>
    <module>presentation</module>
</modules>

The plugin is only specified in this pom, and not in the pom of the modules. When I go to cmd to start a database with:

mvn hsqldb:startdb

Maven tries to create a database for every pom file. It actually starts 5 databases (one for the parent pom, and one for each module). However, I only want one (from the parent pom). In my parent pom file, the plugin is declared like this:

<plugin>
    <groupId>sample.plugin</groupId>
    <artifactId>hsqldb-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <inherited>false</inherited>
    <dependencies>
        <dependency>
            ...
        </dependency>
    </dependencies>
    <executions>
        ...
    </executions>
    <configuration>
        ...
    </configuration>
</plugin>

Any solutions for my problem?

Kind regards,

Walle

Upvotes: 17

Views: 8169

Answers (2)

Jocker
Jocker

Reputation: 358

For the next one to go there, their is a solution at pom level like said in this stackoverflow proposition : https://stackoverflow.com/a/46357305/2190096

You can use <inherited>false</inherited> in the first plugin configuration. So it will only run in parent pom execution.

<build>
    <plugins>
        <plugin>
            <groupId>com.test.plugin</groupId>
            <artifactId>first-maven-plugin</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <inherited>false</inherited>
            <execution>
                <id>execution1</id>
                <phase>initialize</phase>
                <goals>
                    <goal>doit</goal>
                </goals>
            </execution>
        </plugin>
        <plugin>
            <groupId>com.test.plugin2</groupId>
            <artifactId>second-maven-plugin</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <execution>
                <id>another</id>
                <phase>package</phase>
                <goals>
                    <goal>goforit</goal>
                </goals>
            </execution>
        </plugin>
    </plugins>
</build>

Upvotes: 5

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298818

Two ways:

  • On the command line, do mvn hsqldb:startdb -N

    -N,--non-recursive
    Do not recurse into sub-projects

    (Source) or

  • annotate your plugin with @aggregator

    Flags this Mojo to run it in a multi module way, i.e. aggregate the build with the set of projects listed as modules.

    (Source)

    While it's not explicitly said, this means that your plugin is put in charge of building the child modules, i.e. the child modules won't be built automatically.

Either way, it will only build the top project and not descend into modules. There is no way I know of that you can configure this in a pom.

Upvotes: 26

Related Questions