tags:

views:

675

answers:

2

When running a Maven build on the CI server, I generate the site to publish the documentation and reports, and also deploy the artifact to the snapshot repository for use by other projects. To do this I run the following goals:

mvn clean site deploy

This means the unit tests are run twice, once for the site lifecycle and once for the deploy lifecycle. If I configure the site goal to be bound to the standard lifecycle the tests are still run twice, running the site goal always causes the tests to be run because of the @requiresDependencyResolution test annotation. This is fine if you're only creating the site, but in the context of a deploy it greatly increases the build time for no benefit.

I have a workaround that involves copying the SiteMojo (and the required parents) to a new plugin and removing the @requiresDependencyResolution test annotation from the copy.

This modified mojo will generate the reports without forcing the tests to be run again but I'd prefer a solution that doesn't involve any hacking of the site plugin. Is there a way to suppress the requiresDependencyResolution annotation?

+1  A: 

My current approach is to create a new plugin with copies of the relevant types from the maven-site-plugin. These types are identical to the standard versions except for changing the type name, the goal name and the removal of the @requiresDependencyResolution test annotation.

The copied types are:

org.apache.maven.plugins.site.AbstractSiteMojo
org.apache.maven.plugins.site.AbstractSiteRenderingMojo

The parent mojos are required so Maven can process the javadoc-based annotations (this shouldn't be required for Maven 2.2.0+).

org.apache.maven.plugins.site.SiteMojo
org.apache.maven.plugins.site.SiteJarMojo

These two are copied as SiteOnlyMojo and SiteJarOnlyMojo respectively, SiteJarOnlyMojo is changed to inherit from SiteOnlyMojo . Otherwise the only changes are to change the goal namess and remove the annotation.

So SiteOnlyMojo has:

* @goal site
* @requiresDependencyResolution test

changed to:

* @goal site-only

and SiteJarOnlyMojo has:

* @goal jar
* @phase package

changed to:

* @goal jar-only
* @phase package

These types are declared in a maven-plugin project with artifactId maven-site-only-plugin with a dependency declared on the proper site plugin.

To use this I define a profile (I don't want the reports running on every execution, only when -Psite is declared on the command line) and bind it to the prepare-package phase (prior to 2.1.0, you'd have to bind it to the package phase instead).

<profile>
  <id>site</id>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-site-only-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
          <execution>
            <phase>prepare-package</phase>
            <goals>
              <goal>jar-only</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>
Rich Seller
+1  A: 

I'm surprised this works - the @requiresDependencyResolution test tag doesn't actually trigger the tests being built - that should be one of the reports that you've included. Normally, I recommend running the site and the build in separate Maven executions in CI so you can get fast feedback on your build and publish the latest site only when that succeeds.

Another alternative is to run it as mvn clean deploy site, and choose the report-only mojo for surefire-report-maven-plugin (this is usually the report that is running the tests again). This will use the previous test results. Of course, another alternative is disabling that report altogether, since you likely get those results from another source such as your CI server anyway.

Brett Porter