views:

3093

answers:

7

I'm aware I can add maven repositories for fetching dependencies in ~/.m2/settings.xml. But is it possible to add a repository using command line, something like:

mvn install -Dmaven.repository=http://example.com/maven2

The reason I want to do this is because I'm using a continuous integration tool where I have full control over the command line options it uses to call maven, but managing the settings.xml for the user that runs the integration tool is a bit of a hassle.

+2  A: 

I am not sure if you can do it using the command line. You can on the other hand add repositories in the pom.xml as in the following example. Using this approach you do not need to change the ~/.m2/settings.xml file.

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    ...
    <repositories>
         <repository>
          <id>MavenCentral</id>
          <name>Maven repository</name>
          <url>http://repo1.maven.org/maven2&lt;/url&gt;
          <releases>
           <enabled>true</enabled>
          </releases>
          <snapshots>
           <enabled>false</enabled>
          </snapshots>
         </repository>
...
         <repository>
          <id>Codehaus Snapshots</id>
          <url>http://snapshots.repository.codehaus.org/&lt;/url&gt;
          <snapshots>
           <enabled>true</enabled>
          </snapshots>
          <releases>
           <enabled>false</enabled>
          </releases>
         </repository>
        </repositories>

    ...

        <pluginRepositories>
         <pluginRepository>
          <id>apache.snapshots</id>
          <name>Apache Snapshot Repository</name>
          <url>
           http://people.apache.org/repo/m2-snapshot-repository
          </url>
          <releases>
           <enabled>false</enabled>
          </releases>
         </pluginRepository>
         <pluginRepository>
          <id>Codehaus Snapshots</id>
          <url>http://snapshots.repository.codehaus.org/&lt;/url&gt;
          <snapshots>
           <enabled>true</enabled>
          </snapshots>
          <releases>
           <enabled>false</enabled>
          </releases>
         </pluginRepository>
        </pluginRepositories>

    ...

    </project>
smink
+1  A: 

I haven't really used maven 2 before, our system is still working on maven 1.x because of some issues with maven 2.

However, looking at the documentation for maven 2 it seems that there aren't any specific System properties like that. However, you could probably build one into your poms/settings using the System properties. See System properties part of this http://maven.apache.org/settings.html

So you'd have ${maven.repository} in your settings file and then use the -Dmaven.repository like you do above.

I am unsure as to if this would work, but with some tweaking I am sure you can come up with something.

PintSizedCat
+3  A: 

As @Jorge Ferreira already said put your repository definitions in the pom.xml. Use profiles adittionally to select the repository to use via command line:

mvn deploy -P MyRepo2

mvn deploy -P MyRepo1
Eduard Wirch
A: 

Create a POM that has the repository settings that you want and then use a parent element in your project POMs to inherit the additional repositories. The use of an "organization" POM has several other benefits when a group of projects belong to one team.

Steve Moyer
+4  A: 

One of the goals for Maven't Project Object Model (POM) is to capture all information needed to reliably reproduce an artifact, thus passing settings impacting the artifact creation is strongly discouraged.

To achieve your goal, you can check in your user-level settings.xml file with each project and use the -s (or --settings) option to pass it to the build.

ddimitrov
+7  A: 

You can do this but you're probably better off doing it in the POM as others have said.

On the command line you can specify a property for the local repository, and another repository for the remote repositories. The remote repository will have all default settings though

The example below specifies two remote repositories and a custom local repository.

mvn package -Dmaven.repo.remote=http://www.ibiblio.org/maven/,http://myrepo 
  -Dmaven.repo.local=c:\test\repo
Rich Seller
+2  A: 

I'll assume here that you're asking this because you occasionally want to add a new 3rd-party repository to your builds. I may be wrong of course... :)

Your best bet in this case is to use a managed proxy such as artifactory or nexus. Then make a one-time change in settings.xml to set this up as a mirror for the world.

Any 3rd party repos that you need to add from that point on can be handled via the proxy.

Kevin Wright