74

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.

Sindri Traustason
  • 5,445
  • 6
  • 48
  • 66

8 Answers8

83

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"
plus-
  • 45,453
  • 15
  • 60
  • 73
Rich Seller
  • 83,208
  • 23
  • 172
  • 177
  • 2
    This is useful for testing a closed sonatype repo before release. – schmmd Oct 08 '12 at 17:50
  • 4
    How do I specify username and password for the remote? – Daniil Shevelev Dec 04 '14 at 16:30
  • 2
    It is also possible to define MAVEN_OPTS variable so you don't need to define -Dmaven.repo.local each time you call run maven. – Arnost Valicek May 05 '15 at 13:09
  • 3
    You can also define MAVEN_OPTS variable to avoid setting this parameter each time you run maven. See http://stackoverflow.com/questions/16591080/maven-alternative-m2-directory/16591838#16591838 for some examples. – Arnost Valicek May 05 '15 at 13:10
  • The local repository must be an absolute path, https://maven.apache.org/guides/mini/guide-configuring-maven.html. – luka5z Dec 28 '16 at 16:20
  • @luka5z I'm able to use a relative path just fine in Windows Maven 3.5.4, e.g. `mvn -Dmaven.repo.local=lib` – Cardin Sep 15 '18 at 15:52
  • 26
    Does not work in maven 3. I cound't find a option to set the remote repo from command line in newer maven – neves Aug 27 '19 at 20:07
8

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
  • 3,293
  • 3
  • 31
  • 46
7

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">
    ...
    <repositories>
            <repository>
                <id>MavenCentral</id>
                <name>Maven repository</name>
                <url>http://repo1.maven.org/maven2</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
...
            <repository>
                <id>Codehaus Snapshots</id>
                <url>http://snapshots.repository.codehaus.org/</url>
                <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/</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
                <releases>
                    <enabled>false</enabled>
                </releases>
            </pluginRepository>
        </pluginRepositories>

    ...

    </project>
Jorge Ferreira
  • 96,051
  • 25
  • 122
  • 132
4

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
  • 49,540
  • 9
  • 105
  • 155
3

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
  • 9,785
  • 9
  • 61
  • 73
2

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.

Henry B
  • 7,947
  • 10
  • 42
  • 46
  • 10
    `-Dmaven.repo.remote` does not seem to work with newer versions of Maven. I understand that XML settings are better, but I would like that option for running builds on servers I don't control :( – scottysseus Nov 13 '18 at 14:48
1

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
  • 5,663
  • 1
  • 24
  • 34
0

I am using xmlstarlet to achieve this. Tested for Maven 3 on CentOS 7, Maven 2 was not tested yet.

XML_FULLPATH="$HOME/.m2/settings.xml"
MIRROR_ID='example'
MIRROR_MIRROROF='*'
MIRROR_NAME='Example Mirror'
MIRROR_URL='http://example.com/maven2'


## Preview settings without comment:
xmlstarlet ed -d '//comment()' "$XML_FULLPATH"


## Add Mirror settings:
xmlstarlet ed -L \
    --subnode "/_:settings/_:mirrors" --type elem --name "mirrorTMP" --value "" \
    --subnode "/_:settings/_:mirrors/mirrorTMP" --type elem --name "id" --value "$MIRROR_ID" \
    --subnode "/_:settings/_:mirrors/mirrorTMP" --type elem --name "mirrorOf" --value "$MIRROR_MIRROROF" \
    --subnode "/_:settings/_:mirrors/mirrorTMP" --type elem --name "name" --value "$MIRROR_NAME" \
    --subnode "/_:settings/_:mirrors/mirrorTMP" --type elem --name "url" --value "$MIRROR_URL" \
    --rename "/_:settings/_:mirrors/mirrorTMP" --value "mirror" \
    "$XML_FULLPATH"


## Remove Mirror settings by id:
xmlstarlet ed -L \
    --delete "/_:settings/_:mirrors/_:mirror[_:id=\"$MIRROR_ID\"]" \
    "$XML_FULLPATH"

The idea is from: How to insert a new element under another with xmlstarlet?.

YGXXII
  • 331
  • 2
  • 7