views:

568

answers:

1

I am writing a Maven plugin which needs to check if a certain project dependency has javadocs and sources available... and if so, would like to download them and archive them on a server.

I cannot find out how to check if the javadocs and source are available or how to access them if they are.

Any help would be appreciated.

+2  A: 

You can reference additional artifacts by adding the classifier tag to a dependency. The classifier is the additional part of the artifact's name in the repository, e.g junit-4.5-sources.jar

So to directly declare a dependency on the junit sources jar you can specify it as follows:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.5</version>
  <classifier>sources</classifier
  <scope>test</scope>
</dependency>

If you want to download all the dependency sources, use the maven-dependency-plugin's copy-dependencies goal specifying the classifier sources. The following example defines two executions, one for sources and one for javadocs.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>sources</id>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <classifier>sources</classifier>
            <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
            <outputDirectory>${project.build.directory}/sources</outputDirectory>
          </configuration>
        </execution>
        <execution>
          <id>javadocs</id>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <classifier>javadoc</classifier>
            <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
            <outputDirectory>${project.build.directory}/javadocs</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

If you want to package all the downloaded artifacts into a zip, you can use the maven-assembly-plugin to create an archive of the project. The example below are the contents of an assembly descriptor file to include the sources and javadocs directories:

<assembly>
  <id>project</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <useDefaultExcludes>true</useDefaultExcludes>
      <includes>
        <include>${project.build.directory}/sources</include>
        <include>${project.build.directory}/javadocs</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

To reference the assembly, add a plugin configuration to your pom. This assumes the above contents have been put in src/main/assembly/sources.xml (make sure it is defined after the dependency configuration above):

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.2-beta-4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptors>
              <descriptor>src/main/assembly/sources.xml</descriptor>
            </descriptors>
          </configuration>
        </execution>
      </executions>
    </plugin>
Rich Seller