'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>


Solution 1:[1]

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>

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Logan Waggoner
Solution 2 Sean