Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 744
Andriy Kopachevskyy Avatar asked Oct 27 '10 10:10

Andriy Kopachevskyy


People also ask

How do I add a project to local Maven repository?

In Maven, you can use “ mvn install ” to package your project and deploy to local repository automatically, so that other developers can use it.


3 Answers

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
like image 159
Sean Patrick Floyd Avatar answered Sep 19 '22 23:09

Sean Patrick Floyd


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

like image 36
Lonzak Avatar answered Sep 20 '22 23:09

Lonzak


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)

like image 34
Vokail Avatar answered Sep 23 '22 23:09

Vokail