You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by or...@io7m.com on 2015/01/15 17:24:04 UTC

source-plugin vs, the initialize phase?

Hi.

I have a custom plugin that inserts some extra files into the jars
produced by the jar plugin. Because of the way the plugin works, it
will fail if it tries to make the modifications to a jar file that has
already had the modifications made. For reasons that aren't really
relevant here, modifying the plugin to cope with jars that have been 
modified isn't feasible.

The simplest way to resolve this issue seems to be to have the clean 
plugin delete any existing jar file unconditionally (so if the user 
doesn't specify "clean" on the command line, the module is cleaned 
anyway and the plugin doesn't have to receive an already-edited jar).

Anyway, the following pom file unconditonally runs the clean plugin
in the initialize phase:

<?xml version="1.0" encoding="UTF-8"?>
<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.io7m.example</groupId>
  <artifactId>init-test</artifactId>
  <version>0.1.0</version>
  <packaging>jar</packaging>

  <prerequisites>
    <maven>3.0.3</maven>
  </prerequisites>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <dependencies>
  </dependencies>

  <build>
    <plugins>
      <!-- Clean all artifacts automatically at the start of the build. -->
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>auto-clean</id>
            <phase>initialize</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- Create source jar -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

This would be fine, except that if you attempt to run the above pom with "mvn verify",
the maven-source-plugin will cause the maven-clean-plugin to be executed twice:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building init-test 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ init-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /tmp/sandbox/init-test/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ init-test ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ init-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /tmp/sandbox/init-test/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ init-test ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ init-test ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ init-test ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /tmp/sandbox/init-test/target/init-test-0.1.0.jar
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:jar (default) > generate-sources @ init-test >>>
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
[INFO] Deleting /tmp/sandbox/init-test/target
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:jar (default) < generate-sources @ init-test <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ init-test ---
[INFO] No sources in project. Archive not created.
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:test-jar (default) > generate-sources @ init-test >>>
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:test-jar (default) < generate-sources @ init-test <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:test-jar (default) @ init-test ---
[INFO] No sources in project. Archive not created.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.925 s
[INFO] Finished at: 2015-01-15T16:05:19+00:00
[INFO] Final Memory: 10M/228M
[INFO] --------------------------------------------------------

Note how auto-clean appears three times!

In practice, this means that auto-clean runs at the start of the build, and then
is usually run again at some point after the maven-jar-plugin is executed, deleting
the jar file.

Is there any way to stop this from occurring?

M

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: source-plugin vs, the initialize phase?

Posted by Stuart McCulloch <mc...@gmail.com>.
Use the jar-no-fork and test-jar-no-fork goals of the maven-source-plugin - these won’t fork the build (which is what is causing initialize to run multiple times)

On Thursday, 15 January 2015 at 16:24, org.apache.maven.user@io7m.com wrote:

> Hi.
>  
> I have a custom plugin that inserts some extra files into the jars
> produced by the jar plugin. Because of the way the plugin works, it
> will fail if it tries to make the modifications to a jar file that has
> already had the modifications made. For reasons that aren't really
> relevant here, modifying the plugin to cope with jars that have been  
> modified isn't feasible.
>  
> The simplest way to resolve this issue seems to be to have the clean  
> plugin delete any existing jar file unconditionally (so if the user  
> doesn't specify "clean" on the command line, the module is cleaned  
> anyway and the plugin doesn't have to receive an already-edited jar).
>  
> Anyway, the following pom file unconditonally runs the clean plugin
> in the initialize phase:
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <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.io7m.example</groupId>
> <artifactId>init-test</artifactId>
> <version>0.1.0</version>
> <packaging>jar</packaging>
>  
> <prerequisites>
> <maven>3.0.3</maven>
> </prerequisites>
>  
> <properties>
> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
> </properties>
>  
> <dependencies>
> </dependencies>
>  
> <build>
> <plugins>
> <!-- Clean all artifacts automatically at the start of the build. -->
> <plugin>
> <artifactId>maven-clean-plugin</artifactId>
> <version>2.5</version>
> <executions>
> <execution>
> <id>auto-clean</id>
> <phase>initialize</phase>
> <goals>
> <goal>clean</goal>
> </goals>
> </execution>
> </executions>
> </plugin>
>  
> <!-- Create source jar -->
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-source-plugin</artifactId>
> <version>2.2.1</version>
> <executions>
> <execution>
> <phase>package</phase>
> <goals>
> <goal>jar</goal>
> <goal>test-jar</goal>
> </goals>
> </execution>
> </executions>
> </plugin>
> </plugins>
> </build>
> </project>
>  
> This would be fine, except that if you attempt to run the above pom with "mvn verify",
> the maven-source-plugin will cause the maven-clean-plugin to be executed twice:
>  
> [INFO] Scanning for projects...
> [INFO]  
> [INFO] ------------------------------------------------------------------------
> [INFO] Building init-test 0.1.0
> [INFO] ------------------------------------------------------------------------
> [INFO]  
> [INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
> [INFO]  
> [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ init-test ---
> [INFO] Using 'UTF-8' encoding to copy filtered resources.
> [INFO] skip non existing resourceDirectory /tmp/sandbox/init-test/src/main/resources
> [INFO]  
> [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ init-test ---
> [INFO] No sources to compile
> [INFO]  
> [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ init-test ---
> [INFO] Using 'UTF-8' encoding to copy filtered resources.
> [INFO] skip non existing resourceDirectory /tmp/sandbox/init-test/src/test/resources
> [INFO]  
> [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ init-test ---
> [INFO] No sources to compile
> [INFO]  
> [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ init-test ---
> [INFO] No tests to run.
> [INFO]  
> [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ init-test ---
> [WARNING] JAR will be empty - no content was marked for inclusion!
> [INFO] Building jar: /tmp/sandbox/init-test/target/init-test-0.1.0.jar
> [INFO]  
> [INFO] >>> maven-source-plugin:2.2.1:jar (default) > generate-sources @ init-test >>>
> [INFO]  
> [INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
> [INFO] Deleting /tmp/sandbox/init-test/target
> [INFO]  
> [INFO] <<< maven-source-plugin:2.2.1:jar (default) < generate-sources @ init-test <<<
> [INFO]  
> [INFO] --- maven-source-plugin:2.2.1:jar (default) @ init-test ---
> [INFO] No sources in project. Archive not created.
> [INFO]  
> [INFO] >>> maven-source-plugin:2.2.1:test-jar (default) > generate-sources @ init-test >>>
> [INFO]  
> [INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
> [INFO]  
> [INFO] <<< maven-source-plugin:2.2.1:test-jar (default) < generate-sources @ init-test <<<
> [INFO]  
> [INFO] --- maven-source-plugin:2.2.1:test-jar (default) @ init-test ---
> [INFO] No sources in project. Archive not created.
> [INFO] ------------------------------------------------------------------------
> [INFO] BUILD SUCCESS
> [INFO] ------------------------------------------------------------------------
> [INFO] Total time: 0.925 s
> [INFO] Finished at: 2015-01-15T16:05:19+00:00
> [INFO] Final Memory: 10M/228M
> [INFO] --------------------------------------------------------
>  
> Note how auto-clean appears three times!
>  
> In practice, this means that auto-clean runs at the start of the build, and then
> is usually run again at some point after the maven-jar-plugin is executed, deleting
> the jar file.
>  
> Is there any way to stop this from occurring?
>  
> M
>  
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org (mailto:users-unsubscribe@maven.apache.org)
> For additional commands, e-mail: users-help@maven.apache.org (mailto:users-help@maven.apache.org)
>  
>  



Re: source-plugin vs, the initialize phase?

Posted by Karl Heinz Marbaise <kh...@gmx.de>.
Hi,

On 1/15/15 5:45 PM, org.apache.maven.user@io7m.com wrote:
> 'Lo.
>
> On 2015-01-15T17:41:33 +0100
> Karl Heinz Marbaise <kh...@gmx.de> wrote:
>> Hi,
>>
>> you problem is simply you have bound the `jar` goal of the
>> maven-source-plugin which forks the life cycle.
>>
>
> On 2015-01-15T16:40:36 +0000
> Stuart McCulloch <mc...@gmail.com> wrote:
>> Use the jar-no-fork and test-jar-no-fork goals of the maven-source-plugin - these won’t fork the build (which is what is causing initialize to run multiple times)
>
> Thanks to you both, had no idea that even existed.
>
> Any idea why forking is the "default" behaviour?

In earlier days the idea was to make sure some kind of generated code 
(source/resources) should also being packed into the source package but 
over the time it shows that it was not such a wise idea...cause usually 
you use the whole build life cycle which makes sure having the code 
generated already...


Kind regards
Karl Heinz Marbaise

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: source-plugin vs, the initialize phase?

Posted by or...@io7m.com.
'Lo.

On 2015-01-15T17:41:33 +0100
Karl Heinz Marbaise <kh...@gmx.de> wrote:
> Hi,
> 
> you problem is simply you have bound the `jar` goal of the 
> maven-source-plugin which forks the life cycle.
> 

On 2015-01-15T16:40:36 +0000
Stuart McCulloch <mc...@gmail.com> wrote:
> Use the jar-no-fork and test-jar-no-fork goals of the maven-source-plugin - these won’t fork the build (which is what is causing initialize to run multiple times)

Thanks to you both, had no idea that even existed.

Any idea why forking is the "default" behaviour?

M

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: source-plugin vs, the initialize phase?

Posted by Karl Heinz Marbaise <kh...@gmx.de>.
Hi,

you problem is simply you have bound the `jar` goal of the 
maven-source-plugin which forks the life cycle.

To prevent that you should use the `jar-no-fork` goal instead [1]...


Kind regards
Karl Heinz Marbaise


[1] 
http://maven.apache.org/plugins/maven-source-plugin/jar-no-fork-mojo.html


On 1/15/15 5:24 PM, org.apache.maven.user@io7m.com wrote:
> Hi.
>
> I have a custom plugin that inserts some extra files into the jars
> produced by the jar plugin. Because of the way the plugin works, it
> will fail if it tries to make the modifications to a jar file that has
> already had the modifications made. For reasons that aren't really
> relevant here, modifying the plugin to cope with jars that have been
> modified isn't feasible.
>
> The simplest way to resolve this issue seems to be to have the clean
> plugin delete any existing jar file unconditionally (so if the user
> doesn't specify "clean" on the command line, the module is cleaned
> anyway and the plugin doesn't have to receive an already-edited jar).
>
> Anyway, the following pom file unconditonally runs the clean plugin
> in the initialize phase:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <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.io7m.example</groupId>
>    <artifactId>init-test</artifactId>
>    <version>0.1.0</version>
>    <packaging>jar</packaging>
>
>    <prerequisites>
>      <maven>3.0.3</maven>
>    </prerequisites>
>
>    <properties>
>      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
>      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
>    </properties>
>
>    <dependencies>
>    </dependencies>
>
>    <build>
>      <plugins>
>        <!-- Clean all artifacts automatically at the start of the build. -->
>        <plugin>
>          <artifactId>maven-clean-plugin</artifactId>
>          <version>2.5</version>
>          <executions>
>            <execution>
>              <id>auto-clean</id>
>              <phase>initialize</phase>
>              <goals>
>                <goal>clean</goal>
>              </goals>
>            </execution>
>          </executions>
>        </plugin>
>
>        <!-- Create source jar -->
>        <plugin>
>          <groupId>org.apache.maven.plugins</groupId>
>          <artifactId>maven-source-plugin</artifactId>
>          <version>2.2.1</version>
>          <executions>
>            <execution>
>              <phase>package</phase>
>              <goals>
>                <goal>jar</goal>
>                <goal>test-jar</goal>
>              </goals>
>            </execution>
>          </executions>
>        </plugin>
>      </plugins>
>    </build>
> </project>
>
> This would be fine, except that if you attempt to run the above pom with "mvn verify",
> the maven-source-plugin will cause the maven-clean-plugin to be executed twice:
>
> [INFO] Scanning for projects...
> [INFO]
> [INFO] ------------------------------------------------------------------------
> [INFO] Building init-test 0.1.0
> [INFO] ------------------------------------------------------------------------
> [INFO]
> [INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
> [INFO]
> [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ init-test ---
> [INFO] Using 'UTF-8' encoding to copy filtered resources.
> [INFO] skip non existing resourceDirectory /tmp/sandbox/init-test/src/main/resources
> [INFO]
> [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ init-test ---
> [INFO] No sources to compile
> [INFO]
> [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ init-test ---
> [INFO] Using 'UTF-8' encoding to copy filtered resources.
> [INFO] skip non existing resourceDirectory /tmp/sandbox/init-test/src/test/resources
> [INFO]
> [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ init-test ---
> [INFO] No sources to compile
> [INFO]
> [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ init-test ---
> [INFO] No tests to run.
> [INFO]
> [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ init-test ---
> [WARNING] JAR will be empty - no content was marked for inclusion!
> [INFO] Building jar: /tmp/sandbox/init-test/target/init-test-0.1.0.jar
> [INFO]
> [INFO] >>> maven-source-plugin:2.2.1:jar (default) > generate-sources @ init-test >>>
> [INFO]
> [INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
> [INFO] Deleting /tmp/sandbox/init-test/target
> [INFO]
> [INFO] <<< maven-source-plugin:2.2.1:jar (default) < generate-sources @ init-test <<<
> [INFO]
> [INFO] --- maven-source-plugin:2.2.1:jar (default) @ init-test ---
> [INFO] No sources in project. Archive not created.
> [INFO]
> [INFO] >>> maven-source-plugin:2.2.1:test-jar (default) > generate-sources @ init-test >>>
> [INFO]
> [INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ init-test ---
> [INFO]
> [INFO] <<< maven-source-plugin:2.2.1:test-jar (default) < generate-sources @ init-test <<<
> [INFO]
> [INFO] --- maven-source-plugin:2.2.1:test-jar (default) @ init-test ---
> [INFO] No sources in project. Archive not created.
> [INFO] ------------------------------------------------------------------------
> [INFO] BUILD SUCCESS
> [INFO] ------------------------------------------------------------------------
> [INFO] Total time: 0.925 s
> [INFO] Finished at: 2015-01-15T16:05:19+00:00
> [INFO] Final Memory: 10M/228M
> [INFO] --------------------------------------------------------
>
> Note how auto-clean appears three times!
>
> In practice, this means that auto-clean runs at the start of the build, and then
> is usually run again at some point after the maven-jar-plugin is executed, deleting
> the jar file.
>
> Is there any way to stop this from occurring?
>
> M
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org