views:

48

answers:

2

I want to limit maven to use only private/not public maven repository, do these two settings have the same effect ?

1.Setting mirror in settings.xml

<mirrors>
        <mirror>
            <id>my-internal-site</id>
            <mirrorOf>*</mirrorOf>
            <name>our maven repository</name>
            <url>http://myserver/repository&lt;/url&gt;
        </mirror>
    </mirrors>

2.Setting repository in pom.xml

<repositories>
    <repository>
      <id>my-internal-site</id>
      <name>our maven repository</name>
      <url>http://myserver/repo&lt;/url&gt;
    </repository>
  </repositories>

Again the requirement is that maven never goes out to public repositories even if some dependencies are not there on the internal repository. thank you

A: 

No, they mean different things:

In the first example, you said that the given repository is a mirror of all repositories, including the official one.

In the second example, you simply add a new repository. In case a dependency is not found in the local repository, Maven will then look in this repository after having searched in the official repository.

Thus, to force the usage of an internal repository, you must configure the mirror in your settings.xml file.

This is explained in the official documentation of Maven.

romaintaz
+3  A: 

No they don't have the same effect.

The second setting add a new repository as a "complement" to central but doesn't prevent Maven to check central by itself.

The first one forces Maven to use a single repository by having it mirror all repository requests (by setting mirrorOf to *). This is the way to use a single repository.

What you're looking for is thus the first setting and need to be defined in the settings.xml.

Now, adding your corporate repository in the ~/.m2/settings.xml file of each machine can be a bit painful and what I like to do in a corporate environment is to distribute and install a "customized" version of Maven containing the mirror predefined in conf/settings.xml. This way, people just have to install the "corporate" version and they are ready to go.

Pascal Thivent
@Pascal Thivent great answer Pascal
c0mrade
+1: That is *exactly* what I am doing: distributed a customized version of maven, with a custom `conf/settings.xml` including the `mirrorOf` directive.
VonC
I too use a custom Maven checked into SVN. Herein lies the chicken/egg problem. As a Maven purist I hate checking binary files into SVN. We could get away with checking in only the conf folder but then where will the initial Maven come from? Food for thought...
Cliff
@cliff I didn't mention the how to distribute maven intentionally (at the discretion of the readers) but putting it in a VCS as part of a SCM strategy doesn't disturb me at all (and this is what I do too).
Pascal Thivent