How to upload sources to local Maven repository

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)


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