How can I add source code to my dependency libraries in Maven?

11,415

Solution 1

How can I modify the dependencies in order to use libraries from my local project instead from the maven repository

You can't. If a sources JAR isn't available in the central repository, just put the sources somewhere on your file system in a folder, JAR or zip (you could install:install-file a sources archive in your local repository, following Maven's conventions) and setup Eclipse to use them (right-click on the JAR under Maven Dependencies in the Package Explorer, select Java Source Attachment and setup the Location path).

Solution 2

I've solved the problem in a very straight forward way:

I have copied into the folder ${user.home}/.m2/repository/{group-name}/{artifactId}/{version}/ the source file following MAVEN standard: {artifactId}-{version}-sources.jar and it works as a charm! Eclipse checks the local repository and finds the sources.

I don't know if this is the MAVEN way, though.

Solution 3

I use free version of Artifactory repository. I created a jar file {artifactId}-{version}-sources.jar and uploaded to the repository into the same group-id as binary jar file. Then in my pom I added dependency:

<dependency>
            <groupId>mygroupid</groupId>
            <artifactId>artifact</artifactId>
            <version>1.0</version>
            <classifier>source</classifier>
        </dependency>

During maven build phase source jar was downloaded to my local repository. I use netbeans 7.0 and it automatically managed everything for me. For example, right click on method and choosing go toSource correctly brings me to source code in the jar.

Solution 4

You could use the install-file mojo to locally install artifacts into your local maven repository. If you want to share this artifact with others (say your team or another workstation), you could use your own repository manager (e.g. Nexus) and configure it as a mirror for any repository, e.g. central. Nexus will fetch (and cache) artifacts from central. Additionally, you may upload just about any artifact (like junit-addons sources) to your nexus installation.

In order to do configure a mirror, you'll have to edit ${user.home}/.m2/settings.xml

<settings>
  <!-- SNIP -->
  <mirrors>
    <mirror>
      <id>nexus-releases</id>
      <mirrorOf>*</mirrorOf>
      <!-- replace nexus.example.com with the location of your nexus installation -->
      <url>http://nexus.example.com/releases</url>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>
      </repositories>
      <id>nexus</id>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </pluginRepository>
    </profile>
  </profiles>
</settings>
Share:
11,415
monzonj
Author by

monzonj

Updated on June 04, 2022

Comments

  • monzonj
    monzonj about 2 years

    For instance, I have included into my dependencies junit-addons : junit-addons. But in the maven repository there isn't any source code. And I know it exists (I have downloaded it). How can I modify the dependencies in order to use libraries from my local project instead from the maven repository (I would omit the junit-addons from the respository and use a local JAR and its source code instead).

    Note: I use m2eclipse.