Can I add maven repositories in the command line?

JavaMaven 2Build Process

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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"

Solution 2 - Java

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.

Solution 3 - Java

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>

Solution 4 - Java

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

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSindri TraustasonView Question on Stackoverflow
Solution 1 - JavaRich SellerView Answer on Stackoverflow
Solution 2 - JavaddimitrovView Answer on Stackoverflow
Solution 3 - JavaJorge FerreiraView Answer on Stackoverflow
Solution 4 - JavaEduard WirchView Answer on Stackoverflow
Solution 5 - JavaKevin WrightView Answer on Stackoverflow
Solution 6 - JavaHenry BView Answer on Stackoverflow
Solution 7 - JavaSteve MoyerView Answer on Stackoverflow