Thundercleez
Thundercleez

Reputation: 401

Why does exec-maven-plugin run all phases twice?

When I run a build with maven using the exec-maven-plugin, it runs everything twice for some reason. Is there a way to fix this so it only runs once? I've tried setting my phase in the pom.xml to compile and package and either way, it runs twice. My pom looks like

<build>
  <plugins>
    <plugin>
      <artifactId>exec-maven-plugin</artifactId>
      <groupId>org.codehaus.mojo</groupId>
      <version>1.0</version>
      <executions>
        <execution>
          <id>foo</id>
          <phase>compile</phase>
          <goals>
            <goal>exec</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <executable>bash</executable>
        <commandlineArgs>myscript.sh</commandlineArgs>
      </configuration>
     </plugin>
    </plugins>
  </build>

Upvotes: 3

Views: 2359

Answers (4)

Logan Waggoner
Logan Waggoner

Reputation: 71

If you need to run this early in the build, excluding the phase isn't an option.

You can do something like this instead in the plugin config:

<executions>
    <execution>
        <id>default</id>
        <phase>none</phase> <!-- disable the default execution in validate phase -->
    </execution>
    <execution>
        <id>exec-do-something</id>
        <goals>
            <goal>java</goal>
        </goals>
        <phase>generate-sources</phase><!-- now it will run once but in an earlier phase -->
    </execution>
</executions>

Upvotes: 1

Sean
Sean

Reputation: 2475

I saw this happening due to the inclusion of:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

This seems to be that the maven-source-plugin causes a re-execution of the generate-sources phase. See https://maven.apache.org/plugins/maven-source-plugin/jar-mojo.html

Invokes the execution of the lifecycle phase generate-sources prior to executing itself.

If I removed this plugin, the exec goal only executed once.

Upvotes: 0

Karthikeyan
Karthikeyan

Reputation: 414

Instead of removing the phase.. you can pick a correct phase from https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

I move my execution part to compile pase... which got executed only during install phase.. if you want to run your task in compile phase.. below linke has the list https://books.sonatype.com/mvnref-book/reference/lifecycle-sect-structure.html

Upvotes: 0

Thundercleez
Thundercleez

Reputation: 401

It turned out that adding the phase tag caused the command to get executed twice. Leaving that out, it is now getting run once as expected. I guess it doesn't matter what phase I give it now, it'll always run the goal, which works for me.

Upvotes: 1

Related Questions