Generating a JAR of the source code

For many projects, it is useful to generate a JAR of the source code along with the artifact. The source thus generated can be imported to IDEs and used for browsing and debugging. Typically, the artifacts of most open source projects are accompanied by sources and Javadocs.

How to do it...

  1. Open a project for which you want to generate and attach the source code (project-with-source-code).
  2. Add the following plugin configuration to the pom file:
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <executions>
              <execution>
                <id>attach-sources</id>
                <phase>package</phase>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
  3. Run the following Maven command:
    mvn clean package
    
  4. Observe the output:
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project-with-source-attached ---
    [INFO] Building jar: C:projectsapache-maven-cookbookproject-with-source-attached	argetproject-with-source-attached-1.0-SNAPSHOT.jar
    [INFO]
    [INFO] --- maven-source-plugin:2.4:jar-no-fork (attach-sources) @ project-with-source-attached ---
    [INFO] Building jar: C:projectsapache-maven-cookbookproject-with-source-attached	argetproject-with-source-attached-1.0-SNAPSHOT-sources.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] -----------------------------------------------------------------------
    
  5. Examine the target folder:
    How to do it...

How it works...

We added the Maven Source plugin to the pom file. We also configured the plugin to run the jar-no-fork goal during the package phase. The goal creates a JAR of the project source code and makes it available along with the project artifacts.

The jar-no-fork goal is used to bind the goal to the build lifecycle. To run the plugin and create the JAR independent of the lifecycle, the jar goal can be used as follows:

mvn source:jar

Subsequent phases (such as install) install the source artifact along with the project artifact.

There's more...

What if we want to attach the Javadoc instead of (or in addition to) sources? Let us do this:

  1. Add the following plugin to the pom file:
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-javadoc-plugin</artifactId>
          <version>2.10.1</version>
          <executions>
            <execution>
              <id>attach-javadocs</id>
              <phase>package</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
  2. Build the aggregator project:
    mvn clean package
    
  3. Observe the output:
    There's more...

Maven runs the jar goal of the Maven Javadoc plugin in addition to the jar-no-fork goal of the Maven Source plugin. Both the JARs are now created, in addition to the project artifacts, and are now available for distribution.

Tip

Besides sources, the test sources and test Javadocs can also be generated and attached, if relevant to the project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216