Can I add maven repositories in the command line?

2020-02-02 06:22发布

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.

7条回答
欢心
2楼-- · 2020-02-02 06:45

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.

查看更多
你好瞎i
3楼-- · 2020-02-02 06:48

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.

查看更多
成全新的幸福
4楼-- · 2020-02-02 06:54

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.

查看更多
不美不萌又怎样
5楼-- · 2020-02-02 06:56

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
查看更多
ゆ 、 Hurt°
6楼-- · 2020-02-02 06:57

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"
查看更多
等我变得足够好
7楼-- · 2020-02-02 06:59

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.

查看更多
登录 后发表回答