How to upload sources to local Maven repository

JavaMaven 2Maven

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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)

Solution 3 - Java

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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndriy KopachevskyyView Question on Stackoverflow
Solution 1 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 2 - JavaVokailView Answer on Stackoverflow
Solution 3 - JavaLonzakView Answer on Stackoverflow