Specifiy Nexus repository path at one place - maven

At the moment, I need to specify the path of Nexus both in the settings.xml (for building with Maven) and in a parent pom (for deploying with Maven). Is there a way to put this information into just one place?

In your topmost POM, specify maven deploy plugin version 2.8 or above.
Then in your settings.xml specify
<profile>
<id>nexus</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<altSnapshotDeploymentRepository>id::layout::url</altSnapshotDeploymentRepository>
<altReleaseDeploymentRepository>id::layout::url</altReleaseDeploymentRepository>
...
where id is the same id as given in the (settings.xml) definition of your server (for credentials):
<server>
<id>id</id>
<username>usr</username>
<password>pass</password>
</server>
layout is default (except if you are using maven 1)
url is the place where you want to upload to (your remote repo).
This eliminates the need to specify the deployment repo inside the project's POM.

Related

Override URL to nexus repository specified in pom.xml

I have web project which I am going to deploy to nexus repository after successful build on jenkins. Currently in project in pom.xml I have following configuration as below where host and port to nexus repository is hardcoded:
<profiles>
<profile>
<id>deploy-snapshot</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<name>Repository for snapshots</name>
<url>http://ip1:port1/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</profile>
</profiles>
My goal is override nexus url from jenkins without any changes in pom.xml, because currently that configuration in pom.xml is used on another environment which cannot be reconfigured.
It would be good to know in which way it can be done on jenkins taking into account that in future I am going to make similar for other job which will be in charge of deploying npm packages.
I've looked into following jenkins plugin https://wiki.jenkins.io/display/JENKINS/Nexus+Artifact+Uploader, but not sure that this one is actual one, also not sure that plugin will be good for zip archives for npm build.
That was requested in 2008(!) with Make the issue 295: "distributionManagement.site.url configurable from the command line"
In your case, check if passing the property altDeploymentRepository would help:
-DaltDeploymentRepository=...
More precisely, as in "Maven deploy:deploy using -DaltDeploymentRepository"
-DaltDeploymentRepository=releaseRepository::default::http://your.repo.url
"defaut" is the maven2 layout ("legacy" is for maven 1)
In order to overwrite it, you can set it in settings.xml file
In the version of Jenkins I'm using, which is ver. 1.602, if you configure your project as a Maven project, you can specify a "Deploy artifacts to Maven repostitory" post build action for which you can indicate the destination repository.

always download sources (and javadocs) from maven ant task

I am trying to get this ant-based project's init target to download all the sources and javadocs.
I added the following to my ~/.m2/settings.xml (as per Maven – Always download sources and javadocs) but it doesn't force source downloads when used from ant:
<profiles>
<profile>
<id>downloadSources</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<downloadSources>true</downloadSources>
</properties>
</profile>
</profiles>
The only way I could get the sources to download was by hacking build.xml so that all <artifact:dependencies> elements include sourcesFilesetId="sources.dependency.fileset", but this is a pretty distasteful commit that is unlikely to be accepted by the maintainers. A better solution would exist with a property file definition, preferably in the user's settings (not something that mutates the project definition)
Is there a simpler way to ensure that all the sources (and potentially javadocs) are globally downloaded in maven ant tasks?

How to create a Maven "Project Repo"

I've been asked to look at an old project that requires Maven 2.1 and a couple JARs that are not (and will not) stored in our Nexus.
I'm trying to follow the advice from #Nikita Volkov in this post about creating a project repo to hold the JARs as artifacts. The idea being I can check this repo into source control. I can then check it out on any machine and have it build without any special configuration.
To start with I've created a repo and added my first Jar to it by running:
mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=lib/myJar.jar -DgroupId=my.group -DartifactId=myArtifact -Dversion=0.0.1
I then create POM that looks like:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>my.group</groupId>
<artifactId>MyApp</artifactId>
<packaging>ear</packaging>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>my-repo</id>
<url>file://${project.basedir}/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>my.group</groupId>
<artifactId>myArtifact</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
<build>
</build>
</project>
When I view this in Eclipse if flags the dependency with error
Missing artifact my.group:myArtifact:jar:0.0.1.
When I run it from the command line Iget the error
Unable to find resource 'my.group:myArtifact:pom:0.0.1' in repository my-repo
Clearly I've not understood something in the original post, but I don't see what
So, can anybody provide a working example of how to create a in-project Maven Repo?
Update
The files stored in my local repo are:
my/group/myArtifact/0.0.1/myArtifact-0.0.1.jar
my/group/myArtifact/0.0.1/myArtifact-0.0.1.jar.md5
my/group/myArtifact/0.0.1/myArtifact-0.0.1.jar.sha1
my/group/myArtifact/0.0.1/myArtifact-0.0.1.pom
my/group/myArtifact/0.0.1/myArtifact-0.0.1.pom.md5
my/group/myArtifact/0.0.1/myArtifact-0.0.1.pom.sha1
my/group/myArtifact/maven-metadata-local.xml
my/group/myArtifact/maven-metadata-local.xml.md5
my/group/myArtifact/maven-metadata-local.xml.sha1
Update
The contents of my .m2/settings file is:
<?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>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://192.168.152.78:5000/nexus/content/groups/public/</url>
</mirror>
</mirrors>
</settings>
Working solution after several updates to the original question
Your settings.xml file declares a catch-all mirror. This takes effect over the local repository declaration in your pom file. Either remove the catch-all mirror, or try excluding the repository ID of your project repository from the mirroring:
<mirror>*,!my-repo</mirror>
Original answer
Looks like you install the library to the default local Maven repository location (~/.m2/repository), but then you try to pick it up from a location within your project.
Try changing the repository location for Maven before you run the "mvn install:install-file" goal. You can do this by adding a "localRepository" setting in your settings.xml.
You could also create a new settings.xml specifically for your project and tell Maven to use that whenever you work on your project (-s parameter on the command line). IDEs like Eclipse or IntelliJ also support using an alternative settings.xml file.

Adding maven nexus repo to my pom.xml

I have installed nexus on my local machine. I want my pom file to point to this repo. How can I add my custom repository to my pom.xml file?
From Maven - Settings Reference
The repositories for download and deployment are defined by the repositories and distributionManagement elements of the POM. However, certain settings such as username and password should not be distributed along with the pom.xml. This type of information should exist on the build server in the settings.xml.
<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">
...
<servers>
<server>
<id>server001</id>
<username>my_login</username>
<password>my_password</password>
<privateKey>${user.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>
...
</settings>
id: This is the ID of the server (not of the user to login as) that matches the id element of the repository/mirror that Maven tries to connect to.
username, password: These elements appear as a pair denoting the login and password required to authenticate to this server.
privateKey, passphrase: Like the previous two elements, this pair specifies a path to a private key (default is ${user.home}/.ssh/id_dsa) and a passphrase, if required. The passphrase and password elements may be externalized in the future, but for now they must be set plain-text in the settings.xml file.
filePermissions, directoryPermissions: When a repository file or directory is created on deployment, these are the permissions to use. The legal values of each is a three digit number corrosponding to *nix file permissions, ie. 664, or 775.
Note: If you use a private key to login to the server, make sure you omit the element. Otherwise, the key will be ignored.
All you should need is the id, username and password
The id and URL should be defined in your pom.xml like this:
<repositories>
...
<repository>
<id>acme-nexus-releases</id>
<name>acme nexus</name>
<url>https://nexus.acme.net/content/repositories/releases</url>
</repository>
...
</repositories>
If you need a username and password to your server, you should encrypt it.
Maven Password Encryption
First of all I can highly recommend reading the Nexus book. It will explain the benefits of using a Maven repository manager.
There is a section on how to configure your Maven build to use Nexus:
http://www.sonatype.com/books/nexus-book/reference/config.html
This leads me to question why you altering your POM file? I suspect what you really want to do is setup Nexus as a remote repository mirror. This is done in your Maven settings file.
The following tells Maven use Nexus as your default repository (Instead of Maven Central)
<settings>
..
..
<mirrors>
<mirror>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
This is desired behaviour since your Nexus repository is configured to cache artifacts retrieved from Central (which is good for build performance).
Note:
The "public" repository group could include other repositories proxied by your Nexus instance (Not just Maven Central). You probabily want this behaviour, as it centralizes all repository management. It just makes your build less portable for people outside of your organization.
It seems the answers here do not support an enterprise use case where a Nexus server has multiple users and has project-based isolation (protection) based on user id ALONG with using an automated build (CI) system like Jenkins. You would not be able to create a settings.xml file to satisfy the different user ids needed for different projects. I am not sure how to solve this, except by opening Nexus up to anonymous access for reading repositories, unless the projects could store a project-specific generic user id in their pom.xml.
From the Apache Maven site
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>
"The repositories for download and deployment are defined by the repositories and distributionManagement elements of the POM. However, certain settings such as username and password should not be distributed along with the pom.xml. This type of information should exist on the build server in the settings.xml." - Apache Maven site - settings reference
<servers>
<server>
<id>server001</id>
<username>my_login</username>
<password>my_password</password>
<privateKey>${user.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>
If you don't want or you cannot modify the settings.xml file, you can create a new one at the root of your project, and call maven passing it as a parameter with the -s argument:
$ mvn COMMAND ... -s settings.xml
From maven setting reference, you can not put your username/password in a pom.xml
The repositories for download and deployment are defined by the repositories and distributionManagement elements of the POM. However, certain settings such as username and password should not be distributed along with the pom.xml. This type of information should exist on the build server in the settings.xml.
You can first add a repository in your pom and then add the username/password in the $MAVEN_HOME/conf/settings.xml:
<servers>
<server>
<id>my-internal-site</id>
<username>yourUsername</username>
<password>yourPassword</password>
</server>
</servers>

Howto disable mirror repository in maven settings

In my maven ~./.m2/settings.xml I have defined a mirror and some repositories:
<mirrors>
<mirror>
<id>someid</id>
.....
</mirro>
</mirrors>
...
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository> <id>repo....</id>
....
</profile>
</profiles>
This works fine.
There are some projects where I want do disable the mirror and the default profile.
I know that i can define a seperate profile for the repositories, but i don't know how I can tell the maven eclipse plugin not to use the default profile or a specific profile.
Also: how can I change the mirror for a project?
Unfortunately this is impossible with single settings.xml. There is feature request in Maven JIRA, vote for this!
JIRA ticket MNG-3525
Pull Request to implement the feature
Workaround is to have two settings.xml and running maven with selected configuration:
mvn -s my-settings.xml
Copy the settings.xml file, remove the mirror entry and tell maven to use with the --settings file command line option.
Use XSLT or a command line tool like XMLStarlet to automate the process:
xmlstarlet ed -N 's=http://maven.apache.org/SETTINGS/1.0.0' --delete "//s:mirror" settings.xml
prints a new settings.xml file to stdout which doesn't contain any mirror settings.
Update: The XML namespace has recently changed. Make sure you use the same string as the one at the top of the file. Kudos to Roman Ivanov for pointing this out.
Multiple settings.xml is not necessary I think to do this.
It is possible to control mirrors using profiles.
I can use a property for my repository id for example a suffix ${repo-suffix}
$ mvn help:effective-pom | grep "<distributionManagement>" -A 3
<distributionManagement>
<repository>
<id>deployment${repo-suffix}</id>
<name>Internal Releases</name>
Then I can add repo-suffix to a profile for example to give it value -1.
<profile>
<id>my-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<repo-suffix>-1</repo-suffix>
...
This way I now have a dynamically defined repository id in pom files.
$ mvn help:effective-pom | grep "<distributionManagement>" -A 3
<distributionManagement>
<repository>
<id>deployment-1</id>
<name>Internal Releases</name>
For these this deployment-1 repository I can define mirrors in my settings.xml. This is effectively the same as being able to put a mirror in a profile.
The entries in settings.xml applies to all the maven projects on the system and thus is not meant to be tailored for individual projects.
If you want different projects to have different profiles, then you should specify them in the project's pom. You need not have <profiles> section in your ~/m2/settings.xml.
As for <mirrors> they apply to repositories that you want to mirror. You can choose which repositories need to be mirrored, but not which projects should use the mirror and which should not. You can always run the project in offline mode, if you do not want it to download from a remote repository.

Resources