0

I have implemented a custom maven mojo to perform some operation on all modules mentioned in the input POM. The mojo works as expected when called from command line like:

mvn plugin_group_id:artifact_id:version:mojo_goal_name

but it doesn't work the same when i make it part of a phase and call the phase like:

mvn phase_having_mojo_as_goal

In this case it does not utilize the reactor and ignores modules.

The plugin POM is:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aaa.bbb.ccc</groupId>
<artifactId>test-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>release-maven-plugin</name>
<description>Test Plugin</description>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
    <dependency>
        <groupId>org.eclipse.jgit</groupId>
        <artifactId>org.eclipse.jgit</artifactId>
        <version>5.7.0.202003110725-r</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.maven.shared/maven-invoker -->
    <dependency>
        <groupId>org.apache.maven.shared</groupId>
        <artifactId>maven-invoker</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.maven/maven-artifact -->
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-artifact</artifactId>
        <version>3.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.sisu</groupId>
        <artifactId>org.eclipse.sisu.plexus</artifactId>
        <version>0.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-testing</groupId>
        <artifactId>maven-plugin-testing-harness</artifactId>
        <version>3.3.0</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-container-default</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>2.2.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-container-default</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.twdata.maven</groupId>
        <artifactId>mojo-executor</artifactId>
        <version>2.3.0</version>
    </dependency>

    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-model</artifactId>
        <version>3.2.5</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-compat</artifactId>
        <version>3.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-aether-provider</artifactId>
        <version>3.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-utils</artifactId>
        <version>3.0.15</version>
    </dependency>
</dependencies>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

vivek
  • 386
  • 3
  • 12
  • Please add the parts of the POM that contain the plugin definition. I assume all modules have the main POM (aggregator POM) as parent? – J Fabian Meier Apr 15 '20 at 09:58
  • Added the plugin POM. No, the aggregator POM is not the parent of all modules. All modules have a common parent that is also mentioned as one of the modules in the aggregator POM. In fact, the aggregator POM is a newly defined POM that is only supposed to act as an input POM for the plugin. It contains the modules and other user defined parameters being used by the MOJOs. – vivek Apr 15 '20 at 10:12
  • So where did you add the plugin to the phase? You need to do it in the parent POM of the modules. – J Fabian Meier Apr 15 '20 at 10:28
  • I did it in the aggregator POM. – vivek Apr 15 '20 at 10:35
  • Then it only runs in the aggregator POM because the modules don't inherit it if their parent POM is not the aggregator. – J Fabian Meier Apr 15 '20 at 10:40
  • but how come it works when i run the goal itself ? The behavior you mentioned is observed only when i run it through phase. I mean as a goal it works over all the modules, as a phase it works on the aggregator POM only – vivek Apr 15 '20 at 11:41

1 Answers1

1

When you run a goal or phase on an aggregator, it is run on every module.

For standalone goals, this means that the goal is just run on each of the modules.

For a phase, this means that the phase is run on each of the modules. Whether the phase contains the plugin definition is a totally different story.

Your run of the phase happens on every module, but only the aggregator has the definition of the plugin for the phase, the other modules don't have that definition.

You will see that it works if you put the plugin definition in each of the modules or in their parent POM.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • i get it now. 1. So if i run the goal from the aggregator pom location it gets all modules from the reactor and runs the goal on each. When i run the phase, even though it gets all modules via reactor and runs the phase, nothing happens as the modules dont know what to do. Right? 2. i cannot change the modules. I am not supposed to change those, only read them and process the way i want. – vivek Apr 15 '20 at 13:14