views:

448

answers:

4

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.

+1  A: 

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

Pascal Thivent
The source attachment will be lost any time you use `mvn eclipse:eclipse` though. (I think)
sfussenegger
@sfussenegger With `eclipse:eclipse`, certainly. But the OP is using m2eclipse and this setting isn't lost upon modifications of the pom.xml to my knowledge.
Pascal Thivent
Great reply. Thanks for your help.
monzonj
A: 

After you have downloaded it into your local repository, you could make a copy of it. Give it a new artifactId (e.g. libraryName-myVersion) and add dependencies in the pom. Make sure you change the folder names, jar names, pom names and the artifactId itself in the pom. Store everything in your local repository. Now you can use your hacked version of your dependency.

But to be honest, I do not thing this is a good idea to do. But maybe it helps/could not be avoided in your case.

Nils Schmidt
+1  A: 

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&lt;/url&gt;
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central&lt;/url&gt;
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>
      </repositories>
      <id>nexus</id>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central&lt;/url&gt;
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </pluginRepository>
    </profile>
  </profiles>
</settings>
sfussenegger
A: 

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.

monzonj