Can I add maven repositories in the command line?

后端 未结 7 1764
小鲜肉
小鲜肉 2020-12-07 19:52

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:



        
相关标签:
7条回答
  • 2020-12-07 20:23

    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>
    
    0 讨论(0)
  • 2020-12-07 20:26

    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.

    0 讨论(0)
  • 2020-12-07 20:36

    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.

    0 讨论(0)
  • 2020-12-07 20:37

    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.

    0 讨论(0)
  • 2020-12-07 20:44

    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
    
    0 讨论(0)
  • 2020-12-07 20:45

    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"
    
    0 讨论(0)
提交回复
热议问题