Some Newbie
Some Newbie

Reputation: 1089

Using multiple repositories in Maven

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? When I added

<mirrors>
    <mirror>
        <id>MY ID</id>
        <url>MY URL</url>
        <mirrorOf>*</mirrorOf>
    </mirror>
</mirrors>

in settings.xml, I can look into my own internal repository but it overrides the central repository.

I am sure this is a commonly encountered problem for people new to Maven.

Upvotes: 9

Views: 23688

Answers (4)

user1050755
user1050755

Reputation: 11691

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".

Upvotes: 0

Some Newbie
Some Newbie

Reputation: 1089

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.

Upvotes: 0

parsifal
parsifal

Reputation: 226

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).

Upvotes: 9

Brian Agnew
Brian Agnew

Reputation: 272417

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.

Upvotes: 4

Related Questions