Andriy Kopachevskyy
Andriy Kopachevskyy

Reputation: 7706

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.

Upvotes: 65

Views: 24656

Answers (4)

bence of outer space
bence of outer space

Reputation: 251

mvn install:install-file -Dfile=hello-world-1.0.0.jar -Dsources=hello-world-1.0.0-sources.jar -DpomFile=hello-world-1.0.0.pom

Upvotes: 5

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299218

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

Upvotes: 77

Lonzak
Lonzak

Reputation: 9816

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

Upvotes: 1

Vokail
Vokail

Reputation: 691

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)

Upvotes: 7

Related Questions