j_maly
j_maly

Reputation: 1121

Maven local repository in settings.xml vs pom.xml

I am new to Maven. I would like to use the following setup:

1) a remote repository to download public artifact

2) a shared local repository for downloaded artifacts which works nicely by adding the following to settings.xml:

<localRepository>C:/m2repo</localRepository>

3) Now I would also like to have a repository for artifacts referenced from a single project, like an old-fashioned "lib" folder.

So I have added the following to the project's pom.xml:

<repository>
  <id>repo</id>
  <name>repo</name> 
  <releases>
    <enabled>true</enabled>
    <checksumPolicy>ignore</checksumPolicy>
  </releases>
  <url>${project.baseUri}repo</url>
</repository>

I have deployed some jars to the "repo" folder, but when I add them as dependencies in pom.xml, they are not resolved. Maven only looks to the shared repository and the remote repository. When I copy the contents of "repo" to the shared "m2repo", it works. Am I missing something or is this setup not possible at all?

Upvotes: 8

Views: 47095

Answers (2)

j_maly
j_maly

Reputation: 1121

The cause of my problem was the mirroring of repositories in my settings.xml, I have mirroring set to all repositories:

<mirror>
  <mirrorOf>*</mirrorOf>
</mirror>

which I changed to only external repositories:

<mirror>   
  <mirrorOf>external:*</mirrorOf> 
</mirror>

Now my artifacts are found in the "project" repository. However, Maven behaves differently than I expected, as ceilfors writes, the filesystem repository is treated the same as remote repositories - artifacts are copied from there to the local repository, which leads to duplication of jars between the local repository and other filesystem repositories. So I see the setting is not ideal. However, in my case, I still find it the quickiest way to mavenize the project.

Upvotes: 5

ceilfors
ceilfors

Reputation: 2727

Though possible, personally I do not think that it is a good practice to do that. Depending on your usage this post will be helpful: http://blog.sonatype.com/people/2009/02/why-putting-repositories-in-your-poms-is-a-bad-idea. You also want a reproducible and portable project, this question discusses this related topic. Please don't check in those jars into your VCS just for portability sake!

I will try to help if you still insist. To make your model to work, your "repo folder" must follow the structure defined here.

Even though your "repo folder" is a local file structure, Maven will still treat it as if it is a remote repository, just like maven central. This means, whenever you are building your project, Maven will try to download the jars from your "repo folder" to your .m2 repository too:

Downloading: file:/j/my-project/my-local-repo/com/google/guava/guava/14.0.1/guava-14.0.1.pom
Downloaded: file:/j/my-project/my-local-repo/com/google/guava/guava/14.0.1/guava-14.0.1.pom (6 KB at 276.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/google/google/1/google-1.pom
Downloaded: http://repo.maven.apache.org/maven2/com/google/google/1/google-1.pom (2 KB at 10.4 KB/sec)

You can see that the guava jar will be downloaded from the "repo folder" to my .m2 repository since it is available from my "repo folder".

Upvotes: 7

Related Questions