How to create a local central repository for maven artifacts - maven

My organization uses maven for all our projects. Every time we create new maven project, the artifacts are downloaded from maven central repository. But i want to create organization level local repository where all the maven artifacts are available and when new maven project created should look for the artifact in local repository and if not found, then only look in the maven central repository. Can anyone let me know how to do this.
Thanks.

You should take a look at Sonatype Nexus repository manager. It can be used as a proxy to cache remote repositories. After Nexus has been set up, you will just have to edit your settings.xml and add something like this:
<mirrors>
<mirror>
<id>nexus-proxy</id>
<name>Nexus Proxy</name>
<url>http://nexus.example.com/nexus/content/groups/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

You could use Artifactory or a similar Repository Management tool

Related

How to use local maven repository to create container image instead of central maven repository for google cloud

I am trying to create container image of a java application. Dependencies of the application are either on Jfrog repository or my local .m2 folder, not on central.
When I run following command to build and upload image:
gcloud builds submit --tag gcr.io/[PROJECT-ID]/image-name
It tries to download dependencies from central repository which are not available. I have changed the settings.xml of maven to point to Jfrog repository but still it isnt working. I believe it should first check local .m2 repository then it should go to central.
Any explanation/solution will be appreciated. TIA
For Maven to go somewhere else than Maven Central tell it to use a mirror instead. Unfortunately this cannot be done from the command line, but needs to be done with a settings file.
Sample ~/.m2/settings.xml for pointing to a Nexus 3 instance:
<settings>
<mirrors>
<mirror>
<id>nexus1</id>
<mirrorOf>external:*</mirrorOf>
<url>http://nexus/repository/maven-public/</url>
</mirror>
</mirrors>
</settings>
Note that "external:*" says to still use local file repositories if you have artifacts in your source tree you need Maven to resolve locally.
Maven is searching for dependencies only at the central repo.If you want to download dependencies from elsewhere you must specify it to the pom.xml.
<!-- url_of_project -->
<dependency>
....
</dependency>
Local .m2 repo is only searched first for SNAPSHOT versions.If the project is not SNAPSHOT,central repo is searched initially.

How to Change Maven repository to inbuilt repository in same network

In our project we need to setup a repository in a remote machine and I need to point to that repository. I have tried changing settings.xml.
But that didn't work. Then I added repository directly in pom.
This works. But still I am seeing the artifacts are downloaded from central repository. When I checked the effective pom, I am seeing that central repository is been mentioned along with mentioned internal repository
I dont need artifacts to be downloaded from central repository. Instead I need to download from my remote repository. How can I achieve it. TIA :)
You can force Maven to use a single repository by having it mirror all repository requests. The repository must contain all of the desired artifacts, or be able to proxy the requests to other repositories. This setting is most useful when using an internal company repository with the Maven Repository Manager to proxy external requests.
To achieve this, set mirrorOf to *.
<settings>
...
<mirrors>
<mirror>
<id>internal-repository</id>
<name>Maven Repository Manager running on repo.mycompany.com</name>
<url>http://repo.mycompany.com/proxy</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
...
</settings>
more details here

Maven cannot pull jars from Mirros

We have a central repository within intranet. I set mirror as follow. The issue is when I try to download a dependency that is not available in my local repository, the dependency cannot be downloaded completely. Only .lastUpdated file is downloaded in directory. I cannot figure out whey this weird thing happened. But it I comment the mirror setting, the dependency could be downloaded correctly. But I need some ourselves developed dependencies that are residing on central repository. That means I cannot bypass the central repository to search on internet. Does anyone come across this creepy issue?
<mirror>
<id>central-proxy</id>
<name>Central Repo</name>
<url>http://*.*.*.*.*/artifactory/repo</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>

How to use mirrorOf in Maven

What is the difference between using
<mirrorOf>central</mirrorOf>
and
<mirrorOf>*</mirrorOf>
We have a Nexus server running proxied to the Maven public repository and using "*" instead of "central" downloads some artifiacts but not all.
Reading http://maven.apache.org/guides/mini/guide-mirror-settings.html does not suggest what could cause this ?
<mirrorOf>central</mirrorOf>
says if request comes to lookup for central maven repository look in its mirror instead
where
<mirrorOf>*</mirrorOf>
says for any request to download from any repository look in this mirrored repository
if you have second and if it is failing to download some artifacts it could be because you are not proxying certain repository in your own nexus repository

Difference between using a single repository and a single mirror

The maven documentation says:
http://maven.apache.org/guides/mini/guide-mirror-settings.html
Using A Single Repository. You can force Maven to use a single
repository by having it mirror all repository requests. The repository
must contain all of the desired artifacts, or be able to proxy the
requests to other repositories. This setting is most useful when using
an internal company repository with the Maven Repository Manager to
proxy external requests.
To achieve this, set mirrorOf to *.
<settings>
...
<mirrors>
<mirror>
<id>internal-repository</id>
<name>Maven Repository Manager running on repo.mycompany.com</name>
<url>http://repo.mycompany.com/proxy</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
...
</settings>
I don't understand this paragraph.
Shouldn't it be something like this?
Using A Single Mirror You can force Maven to use a single
mirror by having it mirror all repository requests. The mirror must contain all of the desired artifacts for all the
repositories, or be able to proxy the requests to other
repositories. This setting is most useful when using an internal
company repository with the Maven Repository Manager to proxy external
requests. To achieve this, set mirrorOf to *.
<settings>
...
<mirrors>
<mirror>
<id>internal-mirror</id>
<name>Maven Mirror Manager running on mirror.mycompany.com</name>
<url>http://mirror.mycompany.com/proxy</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
...
</settings>
Where is my miss-understanding?
I mean, <mirror><id>internal-repository</id>... seems just wrong to me.
Mirror means a repository that is used as a passerelle/proxy to an other repository.
When using a repository manager like Nexus, Artiafactory, Archiva... you dispose of one local entreprise repository wich proxifies remotes ones.
So there is no need to declare too many repositories in your pom or setting.xml. Using Just one mirror which redirect all requests to the repository manager you have will be sufficient.
That is the meaning of the documentation.
As maven documentation says:
Repositories can be declared inside a project, which means that if you have your own custom repositories, those sharing your project easily get the right settings out of the box. However, you may want to use an alternative mirror for a particular repository without changing the project files.
For repositories in settings.xml, declaring mirrors for them is redundant because you can just replace the repositories directly. You should use mirrors for repositories in pom.xml.
For example. Project X is a public project that list all the repos it required in its pom.xml.
Most people can just download and build the project directly.
Alex wants to build the project on his working PC at company, and his company has an internal enterprice repo. So he uses the company repo in his settings.xml as a mirror for the project repos.
Bob cannot access some project repos directly due to connection issue. He can use other public mirrors for that repo.
Repos in pom.xml is used as default implements and mirrors should be used for special cases.

Resources