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.
project-with-source-code
).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>
mvn clean package
[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] -----------------------------------------------------------------------
target
folder: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.
What if we want to attach the Javadoc instead of (or in addition to) sources? Let us do this:
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>
mvn clean package
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.
3.144.97.216