Skip to content
Advertisement

How to upload sources to local Maven repository

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.

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement