Table of Contents
In this tutorial, we will see how to generate source code jar for maven based project.
If you have simple java project, then you need to convert it maven project.
Sometimes, you need to pack source code with your project jar. This is extremely useful when people use your library and can attach source code for debugging.
You can simply use maven-source-plugin
to achieve the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
Once you put this in your pom.xml, you should be able to see source jar along with your project jar.
Here is complete pom.xml
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.arpit.java2blog</groupId> <artifactId>Java2blogMaven</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
Deploy the jar
You need to run mvn install
to generate the source jar along with project jar.
I am using eclipse to run this mvn command.
You can also go to project location in terminal and execute mvn install
to generate the source code jar.
Here is output of the command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[INFO] >>> maven-source-plugin:3.2.1:jar (attach-sources) > generate-sources @ Java2blogMaven >>> [INFO] [INFO] <<< maven-source-plugin:3.2.1:jar (attach-sources) < generate-sources @ Java2blogMaven <<< [INFO] [INFO] [INFO] --- maven-source-plugin:3.2.1:jar (attach-sources) @ Java2blogMaven --- [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ Java2blogMaven --- [INFO] Installing /Users/apple/eclipse-workspace/Java2blogMaven/target/Java2blogMaven-0.0.1-SNAPSHOT.jar to /Users/apple/.m2/repository/org/arpit/java2blog/Java2blogMaven/0.0.1-SNAPSHOT/Java2blogMaven-0.0.1-SNAPSHOT.jar [INFO] Installing /Users/apple/eclipse-workspace/Java2blogMaven/pom.xml to /Users/apple/.m2/repository/org/arpit/java2blog/Java2blogMaven/0.0.1-SNAPSHOT/Java2blogMaven-0.0.1-SNAPSHOT.pom [INFO] Installing /Users/apple/eclipse-workspace/Java2blogMaven/target/Java2blogMaven-0.0.1-SNAPSHOT-sources.jar to /Users/apple/.m2/repository/org/arpit/java2blog/Java2blogMaven/0.0.1-SNAPSHOT/Java2blogMaven-0.0.1-SNAPSHOT-sources.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.783 s [INFO] Finished at: 2020-03-20T00:13:15+05:30 [INFO] ------------------------------------------------------------------------ |
Check the local repository
You should be able to find jar file in maven local repository. Here is output of above command.
I was able to find source jar at this location.
/Users/apple/.m2/repository/org/arpit/java2blog/Java2blogMaven
That’s all about how to generate source code jar for Maven based project.