Using multiple repositories in Maven

后端 未结 4 1070
逝去的感伤
逝去的感伤 2021-01-12 06:57

I have a project that uses an internal repository and the central repository. Is there a way I can configure the settings.xml such that I can use both instead of just one? W

相关标签:
4条回答
  • 2021-01-12 07:32

    Your mirror directive is currently mirroring everything. You can exclude named repositories thus:

    <mirrorOf>!myExcludedRepo,*</mirrorOf>
    

    The above mirrors everything except the repository named myExcludedRepo.

    Here's the Maven guide for mirroring. Note particularly the section marked Advanced. There are a lot of capabilities there.

    0 讨论(0)
  • 2021-01-12 07:32

    I use three repos (using Nexus), the first one is a Proxy repository to which I add all external repositories. The other two are my internal repositories for deployment of releases and snapshots. Here is my .m2/settings.xml:

    <settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
        <!-- http://maven.apache.org/ref/3.0.4/maven-settings/settings.html -->
    
        <interactiveMode>true</interactiveMode>
        <offline>false</offline>
    
        <mirrors>
        <mirror>
            <id>nexus</id>
            <mirrorOf>external:*</mirrorOf>
            <url>http://localhost:3129/nexus/content/groups/public</url>
        </mirror>
        </mirrors>
    
        <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
    
                <repository>
                    <id>internal.releases</id>
                    <url>http://localhost:3129/nexus/content/repositories/releases</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>        
    
                <repository>
                    <id>internal.snapshots</id>
                    <url>http://localhost:3129/nexus/content/repositories/snapshots</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>        
            </repositories>
    
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
            <properties>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>           
            </properties>
        </profile>
        </profiles>
    
        <activeProfiles>
        <activeProfile>nexus</activeProfile>
        </activeProfiles>  
    
        <servers>
        <server>
            <id>internal.releases</id>
            <username>admin</username>
            <password>XXX</password>
        </server>
        <server>
            <id>internal.snapshots</id>
            <username>admin</username>
            <password>XXX</password>
        </server>
        </servers>
    
    </settings>
    

    Maven uses them all, so you might want to use the Routing feature of Nexus to block "^/my/private/packages/.*" so it does not try to fetch internal packages from external repositories. I haven't added the internal repos to the proxy repo.

    You don't need to adjust your pom.xml files for that to work. Just enter "mvn deploy".

    0 讨论(0)
  • 2021-01-12 07:40

    I found a working answer, which is to change the pom.xml instead and add remote repositories on the fly, as instructed by this link

    Thanks for the answers, folks, and I'd assume your suggestions can be useful for more complicated scenarios with more repositories and complex configurations.

    0 讨论(0)
  • 2021-01-12 07:46

    There are a few of ways to do this.

    The best, IMO, is to have your local repository server act as a proxy for Maven Central. Both Nexus and Artifactory do this out of the box. If you're using Apache or another web server, you should switch.

    You can also update your settings exclude the target server from your mirror:

    <mirrorOf>*,!MyOtherRepository</mirrorOf>
    

    This works if you have multiple local repository servers, but I don't think you can exclude central this way: by default, Maven looks for artifacts in central, and your server acts as a stand-in for it.

    Which leaves explicit repository entries in your POMS, which reference the local repository. If your local repository just serves your artifacts, this might be the second-simplest thing to do (especially if you use a parent POM that holds the repository specification).

    0 讨论(0)
提交回复
热议问题