<p>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:</p>

<pre><code>mvn install -Dmaven.repository=http://example.com/maven2
</code></pre>

<p>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.</p>

[{'answer_id': 71132, 'author': 'Jorge Ferreira', 'author_id': 6508, 'author_profile': 'https://Stackoverflow.com/users/6508', 'pm_score': 3, 'selected': False, 'text': '<p>I am not sure if you can do it using the command line. You can on the other hand add repositories in the <strong>pom.xml</strong> as in the following example. Using this approach you do not need to change the <strong>~/.m2/settings.xml</strong> file.</p>\n\n<pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;\n    &lt;project xmlns="http://maven.apache.org/POM/4.0.0"\n        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;\n    ...\n    &lt;repositories&gt;\n            &lt;repository&gt;\n                &lt;id&gt;MavenCentral&lt;/id&gt;\n                &lt;name&gt;Maven repository&lt;/name&gt;\n                &lt;url&gt;http://repo1.maven.org/maven2&lt;/url&gt;\n                &lt;releases&gt;\n                    &lt;enabled&gt;true&lt;/enabled&gt;\n                &lt;/releases&gt;\n                &lt;snapshots&gt;\n                    &lt;enabled&gt;false&lt;/enabled&gt;\n                &lt;/snapshots&gt;\n            &lt;/repository&gt;\n...\n            &lt;repository&gt;\n                &lt;id&gt;Codehaus Snapshots&lt;/id&gt;\n                &lt;url&gt;http://snapshots.repository.codehaus.org/&lt;/url&gt;\n                &lt;snapshots&gt;\n                    &lt;enabled&gt;true&lt;/enabled&gt;\n                &lt;/snapshots&gt;\n                &lt;releases&gt;\n                    &lt;enabled&gt;false&lt;/enabled&gt;\n                &lt;/releases&gt;\n            &lt;/repository&gt;\n        &lt;/repositories&gt;\n\n    ...\n\n        &lt;pluginRepositories&gt;\n            &lt;pluginRepository&gt;\n                &lt;id&gt;apache.snapshots&lt;/id&gt;\n                &lt;name&gt;Apache Snapshot Repository&lt;/name&gt;\n                &lt;url&gt;\n                    http://people.apache.org/repo/m2-snapshot-repository\n                &lt;/url&gt;\n                &lt;releases&gt;\n                    &lt;enabled&gt;false&lt;/enabled&gt;\n                &lt;/releases&gt;\n            &lt;/pluginRepository&gt;\n            &lt;pluginRepository&gt;\n                &lt;id&gt;Codehaus Snapshots&lt;/id&gt;\n                &lt;url&gt;http://snapshots.repository.codehaus.org/&lt;/url&gt;\n                &lt;snapshots&gt;\n                    &lt;enabled&gt;true&lt;/enabled&gt;\n                &lt;/snapshots&gt;\n                &lt;releases&gt;\n                    &lt;enabled&gt;false&lt;/enabled&gt;\n                &lt;/releases&gt;\n            &lt;/pluginRepository&gt;\n        &lt;/pluginRepositories&gt;\n\n    ...\n\n    &lt;/project&gt;\n</code></pre>\n'}, {'answer_id': 71179, 'author': 'Henry B', 'author_id': 6414, 'author_profile': 'https://Stackoverflow.com/users/6414', 'pm_score': 2, 'selected': False, 'text': '<p>I haven\'t really used maven 2 before, our system is still working on maven 1.x because of some issues with maven 2.</p>\n\n<p>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 <a href="http://maven.apache.org/settings.html" rel="nofollow noreferrer">http://maven.apache.org/settings.html</a></p>\n\n<p>So you\'d have ${maven.repository} in your settings file and then use the -Dmaven.repository like you do above.</p>\n\n<p>I am unsure as to if this would work, but with some tweaking I am sure you can come up with something.</p>\n'}, {'answer_id': 95559, 'author': 'Eduard Wirch', 'author_id': 17428, 'author_profile': 'https://Stackoverflow.com/users/17428', 'pm_score': 2, 'selected': False, 'text': '<p>As\n@Jorge Ferreira\nalready said put your repository definitions in the pom.xml. Use <a href="http://maven.apache.org/pom.html#Profiles" rel="nofollow noreferrer">profiles</a> adittionally to select the repository to use via command line:</p>\n\n<pre><code>mvn deploy -P MyRepo2\n\nmvn deploy -P MyRepo1\n</code></pre>\n'}, {'answer_id': 95711, 'author': 'Steve Moyer', 'author_id': 17008, 'author_profile': 'https://Stackoverflow.com/users/17008', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 96765, 'author': 'ddimitrov', 'author_id': 18187, 'author_profile': 'https://Stackoverflow.com/users/18187', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n\n<p>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. </p>\n"}, {'answer_id': 1193664, 'author': 'Rich Seller', 'author_id': 123582, 'author_profile': 'https://Stackoverflow.com/users/123582', 'pm_score': 7, 'selected': True, 'text': '<p>You can do this but you\'re probably better off doing it in the POM as others have said.</p>\n\n<p>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</p>\n\n<p>The example below specifies two remote repositories and a custom local repository.</p>\n\n<pre><code>mvn package -Dmaven.repo.remote=http://www.ibiblio.org/maven/,http://myrepo \n  -Dmaven.repo.local="c:\\test\\repo"\n</code></pre>\n'}, {'answer_id': 2031913, 'author': 'Kevin Wright', 'author_id': 165009, 'author_profile': 'https://Stackoverflow.com/users/165009', 'pm_score': 2, 'selected': False, 'text': "<p>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... :)</p>\n\n<p>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.</p>\n\n<p>Any 3rd party repos that you need to add from that point on can be handled via the proxy.</p>\n"}, {'answer_id': 73148984, 'author': 'YGXXII', 'author_id': 6102698, 'author_profile': 'https://Stackoverflow.com/users/6102698', 'pm_score': 0, 'selected': False, 'text': '<p>I am using <code>xmlstarlet</code> to achieve this. Tested for Maven 3 on CentOS 7, Maven 2 was not tested yet.</p>\n<pre class="lang-bash prettyprint-override"><code>XML_FULLPATH=&quot;$HOME/.m2/settings.xml&quot;\nMIRROR_ID=\'example\'\nMIRROR_MIRROROF=\'*\'\nMIRROR_NAME=\'Example Mirror\'\nMIRROR_URL=\'http://example.com/maven2\'\n\n\n## Preview settings without comment:\nxmlstarlet ed -d \'//comment()\' &quot;$XML_FULLPATH&quot;\n\n\n## Add Mirror settings:\nxmlstarlet ed -L \\\n    --subnode &quot;/_:settings/_:mirrors&quot; --type elem --name &quot;mirrorTMP&quot; --value &quot;&quot; \\\n    --subnode &quot;/_:settings/_:mirrors/mirrorTMP&quot; --type elem --name &quot;id&quot; --value &quot;$MIRROR_ID&quot; \\\n    --subnode &quot;/_:settings/_:mirrors/mirrorTMP&quot; --type elem --name &quot;mirrorOf&quot; --value &quot;$MIRROR_MIRROROF&quot; \\\n    --subnode &quot;/_:settings/_:mirrors/mirrorTMP&quot; --type elem --name &quot;name&quot; --value &quot;$MIRROR_NAME&quot; \\\n    --subnode &quot;/_:settings/_:mirrors/mirrorTMP&quot; --type elem --name &quot;url&quot; --value &quot;$MIRROR_URL&quot; \\\n    --rename &quot;/_:settings/_:mirrors/mirrorTMP&quot; --value &quot;mirror&quot; \\\n    &quot;$XML_FULLPATH&quot;\n\n\n## Remove Mirror settings by id:\nxmlstarlet ed -L \\\n    --delete &quot;/_:settings/_:mirrors/_:mirror[_:id=\\&quot;$MIRROR_ID\\&quot;]&quot; \\\n    &quot;$XML_FULLPATH&quot;\n</code></pre>\n<p>The idea is from: <a href="https://stackoverflow.com/a/9172796/6102698">How to insert a new element under another with xmlstarlet?</a>.</p>\n'}]
['https://Stackoverflow.com/questions/71030', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1113/']