Parent pom not resolvable unless I add repo to child pom - conundrum - maven

I want to include a parent in a project pom, containing distribution management and repositories. As one would expect.
When I mvn package the child, maven is unable to resolve the parent pom, unsurprisingly.
I can make it resolve the parent by adding my internal nexus repository to the child pom. But this sounds like eggs laying chickens - I'm telling the child something it should know from the parent. I would have to add this repo to every child pom that has a parent.
How can I avoid this?

As khmarbaise wrote, you need to define your repositories in a profile in your settings.xml (this seems somewhat weird, but is actually the only feasible way):
<profiles>
<profile>
<id>repos</id>
<repositories>
<repository>
<id>my-local-repo</id>
<name>Projektserver Snapshots</name>
<url>http://my-server/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
<updatePolicy>daily</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>repos</activeProfile>
</activeProfiles>
Depending on your actual config, you would also include mirror setting and might call your local nexus central.

Sounds like the parent has been deployed to your internal nexus repository so it picks it up from there once you add in the distribution management and repository details.
To make it pick it up from your local copy. First ensure the versions match. Then navigate to the parent and run mvn clean install. This will push the parent pom to your local maven repo.
You may also get round this by ensuring you have added the releativePath element to the parent details in the child pom, you may not need to manually build the parent then but im not 100% sure and havent tested that.

The only things which should defined in your pom is distributionManagement the repositories should be defined in settings.xml instead.

Related

Maven Tycho: Cannot add artifact descriptor which has not been created by this repository

I'm trying to build an RCP application via Tycho and I receive this error when exporting the product.
I don't really understand the issue, but could it be that the reason why it fails it's because I'm using multiple P2 repositories to retrieve my plugins dependencies?
This is the snipped of the repositories I've defined in my parent POM. The rest is pretty standard Tycho.
<repositories>
<repository>
<id>eclipse-luna</id>
<url>${eclipseLuna}</url>
<layout>p2</layout>
</repository>
<repository>
<id>systems-rc-p2</id>
<url>${systemsRcP2}</url>
<layout>p2</layout>
</repository>
<repository>
<id>systems-snapshots-p2</id>
<url>${systemsSnapshotsP2}</url>
<layout>p2</layout>
</repository>
</repositories>
I found the issue.
Basically for some reason (project needs) I had to change the "sourceDirectory" and the "outputDirectory" of the project, pointing them to the classic "src" and "bin" instead of the Maven default "src/main" and "target".
In particular, what was causing the issue was the redefinition of the <outputDirectory> property and the <directory> one. It looks like Tycho does not like it at all.

Maven 3 - Look up custom repository only for specific groupid

Is it possible to tell maven to look up user defined repository only for some specific groupids?
It seems to try the user defined repo first for all artifacts, and ends up taking lot longer to build.
[EDIT]
e.g. artifacts belonging to group "com.example" are hosted on the private repository; but other OSS artifacts like "org.apache", "org.codehaus" etc. are not. Maven tries searching the third-party artifacts in the private repo first, and then in central. I would like to filter the private repo lookups to "com.example" only
I was trying to find a definitive source, but the answer is no. Basically Maven cycles through the repositories you have defined until it either finds the dependency it needs, or there are no other repositories to check.
Off hand I think the order is it goes through repositories found in your pom.xml first to last, and then the repositories it finds in your settings.xml first to last.
So the trick, if there is one, when you have a single dependency that will be found in a certain repositories, and that is all that is there, is to make it one of the last repositories to be checked, or as close to last.
So what you could do, is add the something like the following in your pom.xml:
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
<!-- Your custom repository here -->
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>custom</id>
<name>Custom Repo</name>
<url>Custom repo URL</url>
</repository>
</repositories>
That way central gets checked first. Its hacky, but should help with speed.

Installing a maven project with a dependency on a parent

I have a collection of related projects that inherit from a common maven project.
Since they are still in alpha release, they all (including the parent) are deployed in the Sonatype snapshots repository, instead of Maven central.
The configuration for accessing the maven central is in the parent project of my application.
So its POM specifies as its parent:
...
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
...
And set the corresponding repository at Sonatype:
...
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
...
So the children projects do not have to repeat this configuration.
The parent also provides many other things that are common to all my projects.
Currently I keep all these projects in unrelated directories.
Now, if a user of my application checkout one of my projects and attempt to install it with mvn install, he will receive the error: Non-resolvable parent POM: Could not find artifact ...
This makes sense to me, since the project cannot access the Sonatype repository (e.g., for downloading the parent) since it is precisely the parent the one that contains how to connect to such repository.
I do not want to request the user to install first the parent project, since he should be able to install what he needs in just one single step.
Then what is the recommended way to distribute my libraries so the user can install any of them with one single command ?
It occurs to me that I could include the parent POM in each of the projects (for example, using git submodules) so the parent can be resolved locally. But I am wondering if this is the best way to organize this (?). Any better alternative is appreciated.
UPDATE
I added this to my ~/.m2/settings.xml so the parent POM could be resolved in the Sonatype snapshots repository. Apparently it is working fine and the parent POM is resolved as any other dependency.
<profiles>
<profile>
<id>allow-snapshots</id>
<activation><activeByDefault>true</activeByDefault></activation>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
If you want to keep it to one step install than you have to list enough information in the project pom, i.e. either:
no parent pom, put all the information into each project pom
tell maven where to find your other projects. List the sonatype shnapshot repo in each project file.
A repository manager only helps those with access to it. That is ok if the users are in one organisation. Using the sonatype repository reaches a wider circle.

Where should I define maven repositories given that I use mirrorOf * in settings.xml?

I have a nexus repo on my network. In settings.xml on the build server we have
<mirror>
<id>company.com</id>
<name>nexus</name>
<url>http://build.company.com/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
On this build server we have a number of proxy repositories defined for public repos, and I have some commercially licensed artifacts in a hosted repo.
And a profile - Maven cannot resolve my parent pom (artifact in nexus) without this:
<profiles>
<profile>
<id>repos</id>
<repositories>
<repository>
<id>my-local-repo</id>
<name>bootstrapthingy</name>
<url>http://build.company.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>repos</activeProfile>
</activeProfiles>
My question today:
I also have removed all my
<repositories>
tags from the parent pom that all projects (should eventually!) inherit, and everything seems to work.
Is this well and good? I seem to end up a lot thinking about best practice when I work with maven - lately, around where should information be kept?
As my repositories are now defined at Nexus level, there is an element of my build that is no longer source code controlled, and this bothers me.
Yes I would argue you're on the right track!
Maven recommends to think about your infrastructure and plan it! By that it splits project concerns from infrastructure aspects. Project specific configuration goes into the pom.xml while I would vote to put infrastructure configuration into settings.xml
So the company mirror / proxy goes into settings.xml (as infrastructure may change) along with its authentication and environment settings (that are project independant!)
Usually projects do not rely on a per-project repository. If they do they could in almost any case use the nexus server for that (lets say explicit SNAPSHOT dependencies). So the practice to not have repositories in a pom.xml is ok. URL's change and builds should not request artifacts at different locations. It endangers your build reproduce-ability (as does adding all kinds of unstable remote repos into nexus).
I think within a company you need to consider (or simply acknowledge) that builds in projects are not self-maintained. Most open-source projects are since they do not have a common shared infrastructure they may benefit from (or suffer under?). You need to do the best of it but having infrastructure issues solved in settings.xml also means that the project does not need to do that anymore. Has pro's and con's - no doubt about that :)

Maven: adding dependency not present in the mirror

We are using a local nexus mirror for all of our dependencies.
I need the following dependency in one of the projects:
<depedency>
<groupId>com.smartgwt</groupId>
<artifactId>smartgwt</artifactId>
<version>3.0</version>
</depedency>
from the repository: http://www.smartclient.com/maven2
But maven is giving me error saying that "Failure to find com.smartgwt:smartgwt:jar:3.0".
What might be the problem and how can I solve it?
(Maybe this is very trivial question but I am fairly new to Maven)
I'm assuming Nexus is working for all your standard dependencies hosted on Maven Central.
You can work out where Maven is downloading from by turning on debugging with the -X parameter when doing a build. There will be a lot of noise but if you look a few lines above where your build fails because of failing to find the dependency, it will tell you:
where it is trying to download the dependency
whether it is using a mirror
if there are any HTTP error codes when downloading
How is http://www.smartclient.com/maven2 set up in your Nexus proxy? As a separate proxy repository? Can Nexus access this repo (is it 'In Service' and not blocked)?
Is this repository in Nexus added to the 'public' group? If you don't want this then:
You have to configure a separate mirror in your settings.xml for this repository which points to the URL in Nexus.
Also check that you have added the repository in your POM, e.g.
<project>
...
<repositories>
<repository>
<id>smartclient</id>
<name>SmartClient Maven Repository</name>
<url>http://www.smartclient.com/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
...
</project>
And then configure a proxy entry in your settings.xml for this repository:
<settings>
...
<mirrors>
<mirror>
<id>smartclient-nexus-proxy</id>
<mirrorOf>smartclient</mirrorOf>
<url><url of your smartclient proxy repository in Nexus></url>
</mirror>
...

Resources