What does "http://central" mean in my Maven settings.xml? - maven

I've been copying "sample" settings.xml files for ages now, and almost all of them seem to include a repository with the URL http://central. This bugs me, because of course there could actually be a machine on the local domain called "central", so this is a valid URN, but it also must (might?) have some special meaning to Maven.
Is it just shorthand that's commonly used, but the actual URL is ignored? Could I replace it with something else, or remove it entirely? Is it documented anywhere?
If it matters, I develop on a corporate network that has an internal iBiblio mirror, that acts as "central" for us.

AFAIK, it is a bogus URL which mentions at Configure Maven to Download from Nexus as the following example: -
<settings>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<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>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
The nexus profile is configured to download from the central repository with a bogus URL of http://central.
This URL is overridden by the mirror setting in the same settings.xml file to point to the URL of your single Nexus group. The nexus group is then listed as an active profile in the activeProfiles element.
I hope this may help.

We also used exactly this sample config, even with the same comments for about 15(!) years without any problems. For maven builds it does not matter, as always nexus is requested for any dependencies.
But today I tried to built a project with a combined ant+maven build which failed to get dependencies via artifact:remoteRepository Task from central Repo.
The solution was to fix the "url" tag in the central repo: https://repo.maven.apache.org/maven2
So it looks like it doesn't matter in most cases, but to avoid any corner cases, always use the right url.

Related

Is there such a thing as a local remote Maven repository?

I have a build box on which is installed:
Maven
Bamboo
Archiva
I have configured Bamboo to grab my Maven project from a remote Git source and then build it with the goals 'clean install'.
I have configured Archiva with two repos:
mirror - a mirror of central
dev - repo for my artifacts
I have made the following changes to Maven settings.xml:
# Define local repo - this is the same location as i have set up for the Archiva 'dev' repo.
<localRepository>/opt/maven-repo/dev</localRepository>
# Define the Archiva mirror i set up
<mirror>
<id>mirror</id>
<url>http://localhost:8080/repository/mirror/</url>
<mirrorOf>external:*</mirrorOf>
</mirror>
When I execute the build Maven grabs everything external via the mirror and then adds the built artifact to dev, along with the other jars it grabbed from mirror. So i now have some duplicate jars...
\repo\mirror\junit\junit
\repo\mirror\classworlds\classworlds
\repo\dev\junit\junit
\repo\dev\classworlds\classworlds
\repo\dev\me\myartifact
My question is, is the correct approach? Really I want to keep 'dev' with just my artifacts and mirror with everything from central - i don't want duplicates.
Should I be using the LocalRepository config in settings.xml or should I be using 'mvn deploy' to put the artifact in my Archiva repository by a different method?
Could someone clarify the different use cases for a local and remote repository?
Finally, how should I be defining my POM? Currently, I have just defined
<distributionManagement>
<repository>
<id>dev</id>
<url>file:///repo/dev</url>
</repository>
</distributionManagement>
Should i be adding in my mirror?
To put artifacts in your repository manager you should use the default which is maven-deploy-plugin which can be controlled by distributionManagement. The target where to put those things is controlled by defining the distributionManagement.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<distributionManagement>
<repository>
<id>releases</id>
<name>Release</name>
<url>http://urlArchiva/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshots</name>
<url>http://urlArchiva/snapshots/</url>
</snapshotRepository>
...
</distributionManagement>
...
</project>
The repositories which are used to consume artifacts from is defined in the settings.xml
<settings>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://serverUrlArchiva/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>
</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>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
On Bamboo you should be able to control which settings.xml is used to have an local repository per build which makes build independent from each other.

How to use the internet for maven builds instead, if the corporate maven repository is offline or down?

I have a corporate repository defined in my POM:
<distributionManagement>
<repository>
<id>central</id>
<name>libs-release-local</name>
<url>http://bi-pub.wgresorts.com:8081/artifactory/libs-release-local</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>libs-snapshot-local</name>
<url>http://bi-pub.wgresorts.com:8081/artifactory/libs-snapshot-local</url>
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
</distributionManagement>
But they keep unplugging and moving or rebooting that box, so sometimes it's not available. Is there a way to tell my maven poms "try and use the corporate repository, but if it's down use the internet?" I tried specifying the repository and plugin repository outside of the distribution management section to no avail.
May anyone offer guidance please?
In settings.xml I have a mirror and a local repository defined:
<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>repo</name>
<url>http://bi-pub.wgresorts.com:8081/artifactory/repo</url>
<id>repo</id>
</mirror>
</mirrors>
<localRepository>C:\apache-maven-3.1.1\.m2\repository</localRepository>
I don't want to have to keep changing the settings.xml depending on the whims of the IT department, can I set it up so it tries the corporate one goes to the internet if it's down?
EDIT
If you used the id central, you'll have to first create a pom with central that points to the real one on the internet before you can fix this. It can also point to your corporate repository but with a different ID. Then it will fall over to the internet if your corporate repository is down.
I think you may also have to delete the mirror section from the settings.xml
For better or worse, binary repository is a critical piece of the production pipeline.
You don't ask "what options do I have if my site is down", do you? You just keep your site up. Same story here, your IT department should realize that.
You can run Artifactory in DR mode (active/passive) or even in HA mode (active/active). Your repository has all the means to stay up 24/7, so you really don't need those workarounds.
First of all your settings.xml file is not correct.
<settings>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<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>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
You have to use the above definition to say maven to search for artifacts as well plugins into the mirror repository (doesn't matter if it's a nexus or artifactory).
As supplemental you have defined uniqueVersion which has no effect starting with Maven 3.
Apart from that the definition in setting.xml is for reading whereas the definition in pom.xml distributionManagement is used for writing which means deploying artifacts or the site.
So in both cases you need the repository up and working as already mentioned by JBaruch. If you IT department is not able to handle such cases (basic IT operations) i would hardly think about that.
Every build in your company depends on the availability of the Repository Manager either for downloading artifacts or for deploying results of the builds and for following steps of the pipeline as well.

Maven deployment user can deploy, but not read repository

The last few days I was trying to set a maven-development environment up. We're using TeamCity for the CI, SonarQube for analysis and SonaType Nexus for the repository management.
TeamCity and SonarQube are working like a charm - The nexus however gives us lots of trouble.
This kind of set up is nothing special, I've done it several times now. This time however, I got a very weird bug: TeamCity is able to deploy artifacts to the nexus, but gives an org.apache.maven.wagon.authorization.AuthorizationException: Not authorized , ReasonPhrase:Unauthorized. error when reading dependencies from the nexus.
I even tried changing the user that TeamCity uses from deployment to admin, same issue. chown returned the correct values as well (owned by the "buildagent" user).
I seriously have no clue what could cause this issue, I already tried reinstalling the nexus 3 times, even added the admin role to the "deployment" user - no change whatsoever.
The settings.xml the "buildagent" user uses has the following content (passwords marked with XXX):
<servers>
<!-- This is the username password used to access the nexus repository -->
<server>
<id>central</id>
<username>deployment</username>
<password>XXX</password>
</server>
<server>
<id>rn-releases</id>
<username>deployment</username>
<password>XXX</password>
</server>
<server>
<id>rn-snapshots</id>
<username>deployment</username>
<password>XXX</password>
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>central</mirrorOf>
<url>http://build.example.com:8301/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<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>
For your information: The build log from TeamCity states that the settings are getting read correctly as well, so that's not the cause either.
I really would like to fix this asap, since we have developers waiting to continue on their projects (and we don't want to give out the url if it's not secured, we have private projects running on the CI).
Thanks in advance!
Edit:
So I now even tried installing artifactory - and I still have the same issue. It seems to be something with either A: Maven or B:TeamCity.
The mirror to Nexus has the id nexus, but there's no <server>-entry with this id. If you add that to the settings.xml with the correct credentials, it should all work again.

Nexus is not caching maven central plugins

Im using Maven 3.0.4 and Nexus 2.0.6. I have set up my settings.xml as the Nexus instruction show for using a single repository.
I get the error below when maven tries to run maven -U clean.
[ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.4.1 or one of its d
ependencies could not be resolved: Failed to read artifact descriptor for org.ap
ache.maven.plugins:maven-clean-plugin:jar:2.4.1: Could not find artifact org.apa
che.maven.plugins:maven-clean-plugin:pom:2.4.1 in nexus (http://localhost:8081/n
exus/content/groups/public) -> [Help 1]
If I remove the nexus mirror from the settings and go directly to maven central the command works. The settings for the maven repo in nexus show that it is in service and it is in the public group (its listed last).
I am not behind a proxy to access the internet.
Here is my settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<offline>false</offline>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<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>
<profile>
<id>maven-central</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
Make sure the Central proxy repository is properly configured, and the proxied URL is http://repo1.maven.org/maven2/. Check that you can see cached artifacts at the repository's URL, should be http://localhost:8081/nexus/content/repositories/central/org/apache/maven/plugins/maven-clean-plugin/2.4.0/maven-clean-plugin-2.4.1.pom.
Make sure you have a Central proxy at all, is there anything listed at http://localhost:8081/nexus/content/repositories/central/.
If you're behind a proxy, you can configure the proxy under the Default HTTP Proxy Settings (optional) section in the Administration->Nexus pane.
Then, make sure the Public Repositories group repository is configured to include the Central repository in its list of included repositories.
If everything looks fine so far, check the logs, maybe there's a helpful message in there.
Try downloading this directly through a web browser:
http://localhost:8081/nexus/content/groups/public/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom
If this doesn't work check the sonatype-work/nexus/logs/nexus.log file for more information about the failure.
I had the same symptom as the OP (Nexus was not mirroring an artifact) and found that it was caused by a route definition.
For example, you have an artifact org.blabla:blabla-api:1.0 which is in Maven Central. However you have set up a route matching .*/org/blabla/.* that forces any matching requests to look only in the proxied repository blabla-public ... but unfortunately blabla-public doesn't contain that particular artifact.
Solution: either update the route to add Central to the list of repos used by the route, or delete the route.
(This probably wasn't the cause for the OP, but I'm posting it in case it helps any other visitors.)

How do I configure maven to access maven central if nexus server is down?

I would like to setup my build such that it automatically attempts to download an artifact from maven central iff our nexus server is unreachable. I have the following in settings.xml and I'm not sure how to change it (if even possible).
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://mynexus</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://mynexus</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
In order to use a repository manager (Nexus included) you need to have a mirrorOf * element defined that will intercept all the repository urls and send them to Nexus for resolution. In Maven2 and 3, the mirrorOf element is not able to be configured in a profile. This means there is no easy way to flip back and forth without changing your settings.
You would need to comment out the mirrors section and then deactivate the Nexus profile to have Maven revert back to standard behavior.
Fortunately though Nexus is very stable and it should never go down.

Resources