Apache Maven Source Plugin

1. Overview

The Source Plugin creates a jar archive of the source files of the current project. The jar file is, by default, created in the project's target directory.

2. Plugin Goals

The Source Plugin has five goals:
  • source:aggregate aggregrates sources for all modules in an aggregator project.
  • source:jar is used to bundle the main sources of the project into a jar archive.
  • source:test-jar on the other hand, is used to bundle the test sources of the project into a jar archive.
  • source:jar-no-fork is similar to jar but does not fork the build lifecycle.
  • source:test-jar-no-fork is similar to test-jar but does not fork the build lifecycle.

3. Usage

The source plugin can be used to create a jar file of the project sources from the command line or by binding the goal to the project's build lifecycle. To generate the jar from the command line, use the following command:
mvn source:jar
A jar file of the test sources can also be generated by executing:
mvn source:test-jar
Installing the sources along with your artifact There are two ways to do this. You can either bind this plugin to a phase or you can add it to a profile. The goals source:jar-no-fork and source:test-jar-no-fork are preferred for binding the goal to the build lifecycle.

Installing the sources using a phase binding

Here is how you would configure the plugin in your pom.xml to run automatically during the verify phase:
<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <phase>verify</phase>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>
We are using the verify phase here because it is the phase that comes before the install phase, thus making sure that the sources jar has been created before the install takes place.

Installing the sources using a profile

If you want to install a jar of your sources along with your artifact during the release process, you can add this to your pom.xml file:
<project>
  ...
  <profiles>
    <profile>
      <id>release</id>
      <build>
        <plugins>
          <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-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  ...
</project>

4. Examples

To customize the plugin, you can change its configuration parameters in your POM, as shown below:
<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.0.1</version>
        <configuration>
          <outputDirectory>/absolute/path/to/the/output/directory</outputDirectory>
          <finalName>filename-of-generated-jar-file</finalName>
          <attach>false</attach>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>
The generated jar file will be named by the value of the finalName plus "-sources" if it is the main sources. Otherwise, it would be finalName plus "-test-sources" if it is the test sources. It will be generated in the specified outputDirectory. The attach parameter specifies whether the java sources will be attached to the artifact list of the project.

5. Conclusion

In this quick guide, we went over the source plugin and gave instructions on using and customizing it. Also we have seen the different plugin goals and their usage.
Read more on : https://maven.apache.org/plugins/maven-source-plugin/examples/configureplugin.html

Comments