How to make Maven deploy using a different, ad-hoc group Id? - maven

Some background:
I have copied the master branch of an external github repository, not owned by me, into a personal repository
This repo contains a Java project and uses Maven for building and compilation
The group ID defined in all pom.xml files is org.mylibrary (let's assume)
When deploying to Artifactory, the deployment is done to myrepo/org/mylibrary/...
I'd like to deploy instead to:
myrepo/org/myorganization/mylibrary
but I would like to avoid editing all the pom.xml files, and replacing all lines containing:
<groupId>org.mylibrary</groupId>
with:
<groupId>org.myorganization.mylibrary</groupId>.
I'm configuring the deployment via the distributionManagement element:
<distributionManagement>
<repository>
<name>releases</name>
<id>deployment.credentials</id>
<url>https://artifactory.lab.myorg.org/artifactory/myrepo</url>
</repository>
</distributionManagement>
What's a clean way of solving this? Replacing the groupId definition in all pom.xml files does the trick, but, as said, that's something I want to avoid.
Thanks!

When deploying to any repository, Artifactory utilizes the layout that is configured for this repository. By default, for Maven repositories, Artifactory uses the "maven-default" layout.
I would recommend you to first add your requested layout and then configure your repository with the newly created layout.
I hope this information is found helpful.

Related

How to publish all jar for an existing project in a nexus/sonartype?

I have a maven project, and I have already all my dependencies in my local host. I want to push all these dependencies into nexus/sonartype.
I don't want to push my jar one by one by uploading via nexus interface.
So I copied the content of my maven repository ($MAVEN_HOME/repository) into {nexus-data}/storage/public
Nexus have to recalculate index after restarting?
You might want to use Distribution Management -
<distributionManagement>
<repository>
<id>some-artifactory</id>
<name>Artifactory Name</name>
<url>http://your.artifactory.address/releases</url>
</repository>
<snapshotRepository>
<id>other-artifactory</id>
<name>Other Artifactory Name</name>
<url>http://your.artifactory.address/snapshots</url>
</snapshotRepository>
</distributionManagement>
Repository
Where as the repositories element specifies in the POM the location
and manner in which Maven may download remote artifacts for use by the
current project, distributionManagement specifies where (and how) this
project will get to a remote repository when it is deployed. The
repository elements will be used for snapshot distribution if the
snapshotRepository is not defined.
So I copied the content of my maven repository ($MAVEN_HOME/repository) into {nexus-data}/storage/public
Don't do that. In Nexus 3, Nexus doesn't use raw files so relying on this approach isn't going to work for you anyway.
You should be using distribution management as described by #nullpointer. If you can't for some reason, you could write a script to use Nexus' REST API to upload many artifacts. That's more useful when you aren't building using Maven. For example, maybe you have a whole pile of legacy jar files to upload.

Maven parent pom m2 vs .m2

I have a Maven project shared with a team of people. While attempting to buildi it on Jenkins, I'm looking at the details of the project's parent pom. I'd like a confirmation of my interpretation of the following setting in parent pom:
<repository>
<id>m2-local</id>
<url>file:m2</url>
</repository>
To me, the above appears to refer to the parent's m2 folder of the project. But the id m2-local makes no sense to me. There are no other references of m2-local anywhere else in the pom. I also looked for m2-local in .m2/settings.xml, but nothing for it there either. Is it possible that m2-local has no significance at all here? Also, file:m2 is for Windows notation, but Jenkins is sitting on Linux. What should it look like on Linux?
<repository>
<id>m2-local</id>
<url>file:m2</url>
</repository>
This configuration is declaring a Maven repository, whose id is m2-local and that points to file:m2. The id is just an identifier and has no other meaning. What is more relevant is the URL.
It means that when Maven will try to look for dependencies, it will look for them at the URL file:m2, along with the URL of the other potential repositories. The URL file:m2 points to a folder on the local machine called m2 and located at the same place as the POM (this is because it is using a relative path). Such a notation is both valid in Windows and Linux.
Basically, it means that for your build to run on your Jenkins machine, you need to have a folder name m2 located at the root of the project (location of the POM) and Maven will look inside for artifacts.
I suggest you read Introduction to Repositories on the Maven documentation.

Where does maven dependency fetch packages from?

As per this below snapshot, I see list of packages for hibernate:
I regularly see update index activity by m2e plugin(maven) in eclipse, for which I have no clue, What does it mean?
Where are these packages fetched from and displayed?
What is groupId/ArtifactId? Why can't one just say package/class instead?
Where are these packages fetched from and displayed?
By default, Maven will download from the Maven Central Repository, which is located at this URL: http://search.maven.org/
You can also add a custom repository by using the <repository> tag. Here is an example of how you can add the JBoss repository to your Maven project:
<project>
<repositories>
<repository>
<id>JBoss repository</id
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
</project>
Maven will download the artifacts when it needs them. So doing an mvn update or mvn install would trigger Maven to go to the repository if it doesn't already have the necessary JARs locally. And the local folder where the JAR files gets stored is C:\Users\your_windows_user\.m2\repository by default.
What is groupId/ArtifactId? Why can't one just say package/class instead?
Maven operates by managing dependencies, which are individual JAR files. So if you need to use a class, Maven will pull in the entire JAR file containing that class. The main reason for this is that Java libraries typically ship as JAR files, not individual classes.

Is it possible to deploy to a Nexus Repository using Maven 1?

Some of our projects still use Maven 1. Is it possible to deploy artifacts to a Nexus Maven 1 Repository using "maven:deploy" goal? I could not find the properties to set username and password.
We found a work around by sharing the storage folder of the Nexus server and deploying directly into that folder using file protocol, but this is not really a preferred solution.
If you're using Maven 2 (not Maven 3) you can also deploy artifacts in Maven 1 format by adding "legacy to your distributionManagment
<distributionManagement>
<repository>
<id>nexus</id>
<name>Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/maven1</url>
<layout>legacy</layout>
</repository>
...
</distributionManagement>
This won't work with Maven 3, the legacy layout support was removed in that version of Maven.
Nexus supports hosted repositories using the Maven 1 format so you can use the usual deployment setup. Unfortunately I do NOT remember how to do the deployment with credentials in Maven 1, but I assume the archived documentation would detail that.
If you can not get this to work easily and the project is not VERY complex I would actually suggest to drop Maven 1 and upgrade to Maven 3. This would solve your problem and bring numerous improvements to your development team. Maven 1 has been unsupported and deprecated for years and which puts you into this troublesome situation.

Maven - pointing to a *single* repository

I've got a Nexus Maven repository that I want to be the one and only repository used by my projects.
Per the Nexus documentation, To solve this problem I should change my local .m2/settings.xml.
Changing my settings.xml to solve this problem isn't a good practice, as it's not portable across different developer machines / CI servers.
What I want to do is change my global parent pom to say "use this and only this repository for all projects that use me as a parent." How can I do that? I see that I can add my repository as one of the repositories that are checked, but not the only repository. I tried cutting and pasting the 'mirror' section of the settings file to my pom, but 'mirror' is not allowed there.
Any ideas?
Thanks,
Roy
Tweak Nexus' instructions of modifying your $HOME/.m2/settings.xml. Have a networked settings file that refers to Nexus. This settings file should be used by all developers (and your CI server).
How do all developers refer to this one settings file?
There are a couple of ways of skinning that cat.
Option 1: Alias your Maven command.
On UNIX: alias mvn=mvn -s /networked/path/to/settings.xml
ON Windows: Open mvn.bat. Find mvn invocation and alter it similarly
Option 2: Home/settings linked to networked location
ON UNIX: cd $HOME; rm settings.xml;ln -s /networked/path/to/settings.xml settings.xml
ON Windows: I don't know the equivalent foo for symbolic linking
Assuming that you have a "standard" Maven install for all developers, you can change $M2_HOME/conf/settings.xml
Otherwise, you're out of luck with anything short of an intelligent network proxy.
Have you tried to configure the <repositories> element in your pom?
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>
More information and details can be found at the Maven Internal Repositories page.

Resources