Make local repository fall to the default repositories - maven

I have a local nexus repository in my organization. I would like to make sure that when a new artifact needs to be loaded on a developer's machine it will first go to the local repository and if the artifact is not found fallback to the default maven repositories and download it to the local repository and then to the developer's machine.
Is using mirrorOf in each machine's settings.xml the way to go?

Defining a mirror in the settings.xml configures maven to override the location of a repository it is configured. Instead of using the location the repository requests it will use the one declared in the mirror.
To override the location of all repositories including the central repository add the following to the settings.xml file.
<mirrors>
<mirror>
<id>nexus-mirror</id>
<url>http://nexus.example.com:8081/nexus/content/groups/examplegroup/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
A nexus server can host group and proxy type repositories. A group repository allows you reference multiple over repositories. A proxy repository delegates to a remote repository and caches its artifacts.
In this case the examplegroup needs to be configured to include proxy respositories of the remote repositories you want to fallback to.
You can configure additional repositories in the pom.xml file. These are tried in addition to the default repositories.
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

Related

Maven dependencies from internal and central repository

I have an internal Nexus repository in Maven where some plugins are deployed. There are some dependency jar file that are not present in the nexus repository while some are there. Is it possible to configure maven to search for the dependency jar files in internal repository and if not present search in the maven central repository.
Update
Made the similar configuration as in answer of JimHawkins. But still I guess its looking only in the nexus internal repositories for the dependencies. Here are some of the debus messges it prints:
[DEBUG] Using mirror Nexus (<internal-repo>) for central (repo1.maven.org/maven2).
[DEBUG] Using mirror Nexus (<internal-repo>) for Nexus (my.repository.com/repo/path)
[ERROR] Unresolveable build extension: Plugin <plugin-name> or one of its dependencies
could not be resolved: Failure to find org.co dehaus.plexus:plexus-utils:jar:1.1 in <internal-repo>
was cached in the local repository
If you modify your personal maven settings.xml (located in <HOME>/.m2) like shown below, maven searches your Nexus for dependencies, instead of looking in maven central rpository. If maven doesn't find them in Nexus, Nexus will download them from the maven central repository and than provide it to maven.
Every dependency is also stored in your local maven repository on your workstation, after maven fetched it from Nexus
You can tell Nexus to search for new artifacts not only in maven central repository, but also in other public repos (such as JBoss public repository).
See also: Maven configuration
Use these settings in settings.xml:
<mirrors>
<!--
mirror | Specifies a repository mirror site to use instead of a
given repository. The repository that | this mirror serves has an ID
that matches the mirrorOf element of this mirror. IDs are used | for
inheritance and direct lookup purposes, and must be unique across
the set of mirrors. | <mirror> <id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this
Mirror.</name> <url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://your.internal-nexus.com/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</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>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>

Retrieving dependencies from maven repository

So for my maven project I require jar dependencies that are available in public repositories and also jar dependencies that are on our company internal repository.
How do I configure my pom.xml so that I will retrieve the dependencies from public repositories and our company internal repository without synchronizing and uploading the stuff from public repos to the company internal repository.
In your settings.xml, which is usually in HOME_DIR/.m2 you need to add the company repository, maven central is included by default.
In the example below it will look for your artifact in each repo in order, starting with maven central.
<profile>
<id>extras</id>
<repositories>
<repository>
<id>release</id>
<name>libs-release</name>
<url>http://internal.corp:8091/artifactory/libs-release</url>
</repository>
<repository>
<id>snapshot</id>
<name>libs-snapshot</name>
<url>http://internal.corp:8091/artifactory/libs-snapshot-local</url>
</repository>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://repo.springsource.org/libs-milestone</url>
</repository>
</repositories>
</profile>
I would also say that I set up our corporate repository so that is has a virtual repository to maven central. This means as people use artifacts they will be stored in the company repository. This is normal practice.

How to deploy to archiva using Jenkins and maven

I am trying to deploy to my archiva repo using Jenkins and maven. I am using the "post-build actions" option: "deploy artifacts to maven repository" and I have added the configuration plugin where I added a settings.xml and defined the server details (id, username, password). I also added this file to "build environment" settings where I provided the file as a configuration file.
The problem I am having is the error: not authorized , reasonphrase: unauthorized.
The username and password are for a user with role "repository manager" as the archiva doc instructs. I have set up the pom.xml as well, like the documentation instructs.
I notice that the first error is:
ERROR: failed to retrieve remote metadata someGroupId:someArtifactId:someVersion-SNAPSHOT/maven-metadata.xml
I don't understand where the error comes from and how to resolve it. Please help.
Some suggestions:
1.) Ensure that you have all your servers listed in your Maven settings.xml. This gets me sometimes.
2.) Ensure that your snapshot repo id matches the repo id defined within Archiva.
3.) Ensure that you have access to the snapshots repo, even as admin. Permissions can be revoked.
4.) Ensure that you have the right password.
5.) I've had a restart of Archiva fix this problem before.
6.) The following settings.xml config will allow you to deploy snapshots to a custom snapshots repo that's part of a repository group (i.e. - a snapshots repo for a particular team):
<mirror>
<id><repo_group_id></id>
<mirrorOf>*, !<team_snapshot_repo_id></mirrorOf>
<name>My Team's Maven Repository</name>
<url>http://<HOST>:<PORT>/archiva/repository/<repo_group_id>/</url>
</mirror>
7.) Here's what I add to my pom.xml if I want to deploy the artifact to my snapshots Maven repo:
<distributionManagement>
<repository>
<id>internal</id>
<url>http://HOST:PORT/archiva/repository/internal/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Archiva Managed Snapshot Repository</name>
<url>http://HOST:PORT/archiva/repository/snapshots/</url>
<layout>default</layout>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>snapshots</id>
<url>http://HOST:PORT/archiva/repository/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

Maven Project + Apache Maven + Apache archiva

This is what I did
As a first step, I have configured the Apache Archiva and the service runs perfectly as a standalone service though it created some trouble for me while trying to run in tomcat.
Edited the setting.xml in the MAVEN_HOME pointing to the Apache Archiva repository by adding mirror.
With the project's pom.xml, I see the following
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
Should I remove this to make my Archiva repository effective? I am afraid that this code makes a direct call from the Central repository without making use of the Archiva. Is this correct? Please advise
yes you can. This repository is the default one (and no need to declare it in your pom).
Just a mirror with <mirrorOf>central</mirrorOf> in your settings

Take artifacts from multiple repositories with Maven

How can I configure a maven project to take one of the artifacts from a different repository?
I would like to include this in the project https://github.com/twitter/hadoop-lzo but I can find it only in the twitter repository, not on the central maven repository.
You can set the repositories you want to use in your settings.xml, or in your POM.
If you have a team working on the project, you might want to put this in the POM so everybody has the conf.
You can do it like :
<repositories>
<repository>
<id>Maven Central</id>
<url>http://repo1.maven.org/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
.... other repos
</repositories>

Resources