How to get all remote repositories of a maven project hierarchy? - maven

I'm redirecting all maven repository access to an Artifactory with the following ~/.m2/settings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- unclear what version changes -> use 1.1.0 because it's higher -->
<servers>
<server>
<id>central</id>
<username>admin</username>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
</server>
</servers>
<mirrors>
<mirror>
<id>artifactory</id>
<name>artifactory</name>
<mirrorOf>*</mirrorOf>
<url>https://[hostname]:[port]/artifactory/remote-repos/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>artifactory</id>
<repositories>
<repository>
<id>central</id>
<name>remote-repos</name>
<url>https://[hostname]:[port]/artifactory/remote-repos</url>
<snapshots>
<enabled>false</enabled>
<updatePolicy>interval:25200</updatePolicy>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<name>remote-repos</name>
<url>https://[hostname]:[port]/artifactory/remote-repos</url>
<snapshots />
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>remote-repos</name>
<url>https://[hostname]:[port]/artifactory/remote-repos</url>
<snapshots>
<enabled>false</enabled>
<updatePolicy>interval:25200</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>snapshots</id>
<name>remote-repos</name>
<url>https://[hostname]:[port]/artifactory/remote-repos</url>
<snapshots />
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>
</settings>
and thus have to add extra remote repositories which are specified by a project (and its child projects) to the Artifactory instance. I currently use
find . -name pom.xml -exec grep -B 5 -C 5 '<repository>' {} +
which isn't very handy in case an URL is a variable and declared elsewhere and it doesn't skip duplicates. It's not the worst thing in the world, but maybe there's an improvement available.
The following doesn't work:
mvn versions:display-dependency-updates doesn't display remote repositories
mvn dependency:list-repositories only works until the first dependency can't be fetched if the proxy is enabled so that I have to figure out where to get it from, add the researched remote repository to Artifactory or move ~/.m2/settings.xml aside - less handy than the find command above
The solution should work recursively, i.e. include all repositories in all child projects and childrens child projects, etc.
It makes a lot of sense that a solutions don't require to download the dependencies directly from the remote repository first without the proxy since I'd like to transfer them through the Maven proxy immediately if possible - it's not a requirement, though.

A somewhat hacky approach could be those two steps:
Get the effective POMs. Note that the below goal generates an XML file containing all POMs at once. However, variable names will already be resolved.
mvn help:effective-pom -Doutput="effective-pom.xml"
Parse the resulting XML file and gather the repositories, e.g., using a Python script gather-repos.py.
#!/usr/bin/python
import sys, xml.etree.ElementTree as ET
root = ET.parse('effective-pom.xml').getroot()
repositories = dict()
for node in root.iter('{http://maven.apache.org/POM/4.0.0}repository'):
repo_id = node.findtext('{http://maven.apache.org/POM/4.0.0}id')
repositories[repo_id] = node
for node in repositories.itervalues():
ET.ElementTree(node).write(sys.stdout, default_namespace='http://maven.apache.org/POM/4.0.0')
Of course, the script can then be run via
chmod +x gather-repos.py
./gather-repos.py

Related

Maven Bnd Repository Plugin fails to fetch artifact from remote artifactory

I'm trying to get bnd tools structure working (started with tutorial https://bndtools.org/tutorial.html)
Added use of javax.vecmath (as simple library as I could think of) in cnf/central.maven file
javax.vecmath:vecmath:1.5.2
and added it to build path in org.example.impl with everything resolving just fine.
I have fork of this library in jFrog artifactory running on remote server, so next step was defining that repository in cnf/build.bnd adding (as described in https://bnd.bndtools.org/plugins/maven.html)
-plugin.10.Remote = \
aQute.bnd.repository.maven.provider.MavenBndRepository; \
releaseUrl=https://artifactory.website.com/artifactory/libs-release-local/; \
snapshotUrl=https://artifactory.website.com/artifactory/libs-snapshot-local/; \
index=${.}/release.maven; \
name="Maven Remote"
and appropriate reference in cnf/release.maven
javax.vecmath:vecmath:2.1.5
When adding this library to build path I get error "2.1.5 [Could not fetch javax.vecmath:vecmath:2.1.5]" with no further information.
I figured the only difference between maven central and my artifactory should be content of .m2/settings.xml with credentials (according to https://bnd.bndtools.org/instructions/connection-settings plugin should look there in the first place).
Configuration that works for maven in eclipse (when I open a maven project that has vecmath 2.1.5 as dependency it gets pulled without a problem to .m2/repository/javax/vecmath/vecmath/2.1.5 and once there it gets resolved just fine by bnd)
<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<servers>
<server>
<username>username</username>
<password>roigonsdnglosgnoisfgnsdjgnlafjksasgnl</password>
<id>central</id>
</server>
<server>
<username>username</username>
<password>roigonsdnglosgnoisfgnsdjgnlafjksasgnl</password>
<id>snapshots</id>
</server>
</servers>
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>libs-release</name>
<url>https://artifactory.website.com/artifactory/libs-release</url>
</repository>
<repository>
<snapshots />
<id>snapshots</id>
<name>libs-snapshot</name>
<url>https://artifactory.website.com/artifactory/libs-snapshot</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>libs-release</name>
<url>https://artifactory.website.com/artifactory/libs-release</url>
</pluginRepository>
<pluginRepository>
<snapshots />
<id>snapshots</id>
<name>libs-snapshot</name>
<url>https://artifactory.website.com/artifactory/libs-snapshot</url>
</pluginRepository>
</pluginRepositories>
<id>artifactory</id>
</profile>
</profiles>
<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>
</settings>
From this it seems bnd has no problem fetching from maven central to local m2 repository and using it from there, but fails to connect to my artifactory. Is there some key setting or difference I'm missing?
Ended up solving the issue with help from https://groups.google.com/forum/#!forum/bndtools-users having found two (three?) problems with my configuration.
The correct form for repository definition in build.bnd seems to be
-plugin.10.Remote: \
aQute.bnd.repository.maven.provider.MavenBndRepository; \
releaseUrl=https://artifactory.website.com/artifactory/libs-release-local/; \
snapshotUrl=https://artifactory.website.com/artifactory/libs-snapshot-local/; \
index=${.}/release.maven; \
name="Maven Remote"
Looking in https://bnd.bndtools.org/instructions/connection-settings server auth configuration for bnd differs from maven configuration (0.3.4, for some reason I missed that part)
<server>
<username>username</username>
<password>roigonsdnglosgnoisfgnsdjgnlafjksasgnl</password>
<id>https://*website.com</id>
</server>
and bnd is looking for connection settings first in .bnd, if there are none, then in .m2, but if there are incorrect/insufficient settings in .bnd it does not look in .m2 even if it contains correct/sufficient ones.
The default order in which bnd looks for settings is:
`~/.bnd/connection-settings.xml`
`~/.m2/settings.xml`

maven getting "Not Authorized" when trying to access nexus private repository

I've set up a private nexus repo manager on an EC2 instance and followed the various instructions floating around the internet on how to set up a maven project to use it. I have also disabled the anonymous account.
I am able to connect and copy repositories via curl -U username:password <the_url>
and it seems to work fine. However when I try to use maven (any goals) The very first thing I get is
[WARNING] Could not transfer metadata org.apache.maven.plugins:maven-compiler-plugin/maven-metadata.xml from/to nexus (http://MY_NEXUS_HOST:8081/nexus/content/groups/public): Not authorized , ReasonPhrase:Unauthorized.
The mvn command then fails because it can't find the plugin anywhere. So the fact that I can use the rest command and it works as expected, but not through maven indicates to me that it is a problem with the configuration. I think I understand what's going on pretty well, and I've checked and rechecked the files, but I don't see anything wrong.
Here's the settings.xml file
<servers>
<server>
<id>nexus-snapshot</id>
<username>USER_NAME</username>
<password>USER_PASSWD</password>
</server>
<server>
<id>nexus-release</id>
<username>USER_NAME</username>
<password>USER_PASSWD</password>
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://MY_NEXUS_HOST: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>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
And here is the relevent portion of the pom file
<distributionManagement>
<repository>
<id>nexus-release</id>
<name>Nexus Release Repository</name>
<url>http://MY_NEXUS_HOST:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>nexus-snapshot</id>
<name>Nexus Snapshot Repository</name>
<url>http://MY_NEXUS_HOST:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
I'm wondering if there's a way to see the exact issue I'm running into. For instance, if I'm getting a 401, 403 or (for some reason?) 404.
If someone can please help me I'd be ever so greatful.
Oh, both maven and nexus are the latest versions as of last week.
*edited because no matter how many times you check something before hitting submit...
Oh my funky goat.
The problem was that apparently in settings.xml, the Id field has to be the same as the one in the server field. ie:
<servers>
<server>
<id>nexus-release</id> <---THIS MUST MATCH
<username>USER_NAME</username>
<password>USER_PASSWD</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus-release</id> <---THIS
<mirrorOf>*</mirrorOf>
<url>http://MY_NEXUS_HOST:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
I guess it doesn't matter which one I use (they're both the same in this case, but that's not necessarily always true).

IntelliJ IDEA 13 doesn't recognize Maven mirrors

I have a Maven project with a mirror set up for central repo, like that:
<settings>
...
<mirrors>
<mirror>
<id>central-my</id>
<mirrorOf>central</mirrorOf>
<name>Maven Central Repo mirror</name>
<url>http://local_url:15999/nexus/content/repositories/central/</url>
</mirror>
</mirrors>
...
</settings>
For some reason, when I import this project into IDEA and make it use this settings.xml it still doesn't see this mirror, showing me http://repo.maven.apache.org/maven2 instead (Project Settings > Maven > Repositories). The problem is, I can't update from this repo because I'm on internal network. What can I do in this case?
Using mirrorOf is discouraged, since it defeats the idea of having separate repositories for promotions (e.g. from snapshots to staging to releases), access control etc. This feature exists in Maven since the time there was no proper Binary Repository with multiple repositories support in the market, so Maven developers lived in the world where one proxy exposed one URL for all the remote repositories it proxied. That's, of course, no longer true.
Another usage of this setting was to ensure your in-house repository is not shortcut by repositories declarations in dependencies' pom files, but there are better solutions for this problem as well.
All in all, don't use mirrorOf. Instead, you should "shadow" the central and plugins repositories, replacing them with your in-house repository URL.
Here's an example of settings.xml for Artifactory (should be similar for Nexus):
<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>libs-releases</name>
<url>http://jbaruch.artifactoryonline.com/jbaruch/libs-releases</url>
</repository>
<repository>
<snapshots />
<id>snapshots</id>
<name>remote-snapshot-repos</name>
<url>http://jbaruch.artifactoryonline.com/jbaruch/remote-snapshot-repos</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>plugins-releases</name>
<url>http://jbaruch.artifactoryonline.com/jbaruch/plugins-releases</url>
</pluginRepository>
<pluginRepository>
<snapshots />
<id>snapshots</id>
<name>plugins-snapshots</name>
<url>http://jbaruch.artifactoryonline.com/jbaruch/plugins-snapshots</url>
</pluginRepository>
</pluginRepositories>
<id>artifactory</id>
</profile>
</profiles>
<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>
</settings>

Build/Deploy third-party project in maven but upload to my snapshots repository

I build weekly a large third-party project (apache sling) in Jenkins. I've got jenkin's settings.xml set up to use my own archiva server to download all dependencies (using the mirror section).
However, I haven't figured out how to get the build to upload snapshots my own snapshots repository when doing a 'deploy'. It instead tries to upload the snapshots to an apache.org snapshots server and fails.
Is there a way to configure settings.xml to override the snapshots server in a similar way it's possible to override the repository? This has to be done without editing the project's pom.xml.
The reason I need to do this is because I need access to some of the snapshot versions as dependencies in another project, and I don't want to have to manually upload them all into archiva.
Here's 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">
<localRepository>/Users/Shared/Jenkins/.m2/repository</localRepository>
<servers>
<server>
<id>astra.internal</id>
<username>-deleted-</username>
<password>-deleted-</password>
</server>
<server>
<id>astra.snapshots</id>
<username>-deleted-</username>
<password>-deleted-</password>
</server>
</servers>
<mirrors>
<mirror>
<id>central-proxy</id>
<name>Local proxy of central repo</name>
<url>http://-deleted-.com/repository/internal</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>snapshots</id>
<name>Local proxy of snapshots</name>
<url>http://-deleted-.com/repository/internal</url>
<mirrorOf>snapshots</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>Repository Proxy</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<!-- ******************************************************* -->
<!-- repositories for jar artifacts -->
<profile>
<!-- ******************************************************* -->
<repositories>
<repository>
<id>astra.internal</id>
<url>http://-deleted-.com/repository/internal/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>astra.snapshots</id>
<url>http://-deleted-.com/repository/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
You can overwrite the Distribution management section with command line parameters.
mvn clean deploy -DaltDeploymentRepository=ui-snapshots::default::http://localhost:8081/artifactory/libs-snapshot-local/
Have a look at the maven deploy plugin.
Alternatively, you should be able to use the Jenkins post build step. There you need to specifically provide the Repo url anyway.
The location of the repository is part of the pom.xml, the distributionManagement often in one of the upper-most parents. So it is not something you can set from the settings.xml.
There's a good chance that you can change it with the distMgmtSnapshotsUrl property, see org.apache:apache:13:pom, assuming the sling-project uses this as its (indirect) parent.
As far as I know you have to edit the pom file for the project and have this section (with your server settings of course):
<distributionManagement>
<repository>
<id>archiva.internal</id>
<name>Internal Release Repository</name>
<url>dav:http://localhost:8080/repository/internal</url>
</repository>
<snapshotRepository>
<id>archiva.snapshots</id>
<name>Internal Snapshot Repository</name>
<url>http://localhost:8080/repository/snapshots</url>
</snapshotRepository>
</distributionManagement>
Would it help you to put it in a company wide pom you inherit from?

Maven archetype catalog : specify custom location(s)

I'm deploying a Nexus repository for Maven, and custom archetypes on it.
I would like to execute mvn archetype:generate and be prompted a list of internal + custom archetypes.
The only way I found to prompt custom archetypes (in an ergonomic way, meaning no URL) is to define the archetype-catalog path as a property in the settings. This is not a valid solution because I want several catalogs (and this property cannot be overriden in CLI).
Does anybody have a clue on how to do that ?
Thanks in advance,
[EDIT]
I found an issue report related : http://jira.codehaus.org/browse/ARCHETYPE-273
And I noticed that during archetype:generate, maven tries to reach the central repository :
[DEBUG] Searching for remote catalog: http://repo1.maven.org/maven2/archetype-catalog.xml
[DEBUG] Searching for remote catalog: http://repo1.maven.org/maven2
It ends by a "Connection Timed out" because I did not (and don't want to) specify a proxy...
I don't understand why maven doesn't check nexus catalog...
I also have a Nexus configured to mirror the Maven repositories and thus the remote catalog too.
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://afbwt03:8081/nexus/content/groups/JavaRepo/</url>
</mirror>
and:
<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>
I am able to access the remote catalog only when I use the following Maven command line:
mvn archetype:generate -DarchetypeCatalog=http://afbwt03:8081/nexus/content/groups/JavaRepo
If I don't define the archetypeCatalog variable, I get the same behavior as you do: trying to access the repo1. ... although some mirrors are configured.
You need to have
the property archetypeRepository defined in the active profile in your .m2/settings.xml
the repositories and pluginRepositories redirected to your mirror, on the same id "central".
and of course, the mirror defined
Apache maven documentation on archetype plugin specifies that archetypeRepository is definable user property:
http://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html
Your .m2/settings.xml should have these minimal elements
<?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">
<mirrors>
<mirror>
<id>central</id>
<name>Mirror for maven central</name>
<url>http://mymvnhost:8081/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>dev</id>
<properties>
<archetypeRepository>http://mymvnhost:8081/nexus/content/groups/public/</archetypeRepository>
</properties>
<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>dev</activeProfile>
</activeProfiles>
</settings>
With maven-archetype-plugin:3.1.1 you have to
add/edit archetype-catalog.xml on your repo to list your custom archetypes
edit your settings.xml to add this repo with id archetype.
invoke mvn archetype:generate -DarchetypeCatalog=remote
From https://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html :
If you want the catalogs to come from a different repository, please add the following to your settings.xml
<repository>
<id>archetype</id>
<url>https://repository.domain.com/path/to/repo/</url>
</repository>

Resources