Suppose I have a Maven 2 Java project on my local machine. I build the project .jar
file and push it to my local Maven repo, using mvn install
.
Question:
How can I force Maven to also push the project sources jar
to the local repo?
This is useful if I'll use the above mentioned project as dependency while developing a new project, and can use the mvn eclipse:eclipse -DdownloadSources
feature.
In Maven, you can use “ mvn install ” to package your project and deploy to local repository automatically, so that other developers can use it.
This snippet automatically installs / deploys a source jar from any install / deploy:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>[whatever version is current]</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
Use this link to check for the current version of the maven-source-plugin
Or from command line:
mvn clean source:jar install
One addition to the above answer:
It still wasn't working correctly for me. The sources.jar was generated and available in the target folder but not in the local repository. The reason for that was that I specified the phase differently: <phase>install</phase>
In this case the maven install plugin is executed first and copies the jar files into the local repo. Afterwards the source.jar is generated (and thus not copied).
[INFO] --- maven-install-plugin:2.4:install (default-install) @ foo ---
[INFO] Installing foo.jar into local repo
[INFO] Installing pom.xml into local repo
[INFO] Installing javadoc.jar into local repo
[INFO]
[INFO] --- maven-source-plugin:3.0.1:jar-no-fork (attach-sources) @ foo ---
[INFO] Building jar: foo-sources.jar
So it is important to specify an earlier phase than install (like it is correctly mentioned in the accepted answer).
I have found a better answer, just add this on your pom.xml
<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>
And run from command line:
mvn install
Now maven install on your local repository jar and sources
Solution found on: https://www.mkyong.com/maven/generate-source-code-jar-for-maven-based-project/ (I'm not affiliated in any way)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With