mvn:install to place my jar in nexus remote repository - maven

I am using maven in eclipse (m2 eclipse)
When i execute mvn:install i want my jar(artifact) to be installed in the nexus repository which is located on the server(right now the installed jar is placed in the local system repository).. how can i do that ?
I even changed this to my local repository address

Typically, you'd use mvn deploy to deploy to a non-local repository.
You will, of course, need to have the repo configured, either in your maven settings.xml or in the project POM.
As we always use the same internal repo, I've done this in the Maven settings.xml, more precisely, the "profiles" section.
Here is my config for reference:
<profiles>
<profile>
<id>artifactory</id>
<repositories>
<repository>
<id>central</id>
<name>libs-releases</name>
<url>http://repo.example.com/libs-releases</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<name>libs-snapshots</name>
<url>http://repo.example.com/libs-snapshots</url>
<snapshots />
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>plugins-releases</name>
<url>http://repo.example.com/plugins-releases</url>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>

Related

Specify Maven plugin repository from the commandline

I want to use maven's dependency:get to download a package. I need to configure both a remote repository and a plugin repository. If I configure it in settings.xml it works:
(Example settings.xml from https://maven.apache.org/settings.html)
<profiles>
<profile>
...
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
...
</pluginRepositories>
...
</profile>
</profiles>
...
</settings>
mvn dependency:get -Dartifact=<artifact>
However I would like to be able to do this without configuring the settings.xml file. I know dependency:get lets you specify a -DremoteRepositories argument. There there any way to specify the plugin repository? In my particular case both repositories are actually the same location however if I only provide -DremoteRepositories maven fails to download the plugins.

Configure jcenter for only downloading artifacts and Artifactory for deploying artifacts

We have Artifactory setup and we use Maven central repo for downloading artifacts, which are then automatically cached in Artifactory. We also upload/deploy our own artifacts in Artifactory.
I now want to replace Maven central repo with jcenter and would like to continue using our Artifactory for uploading/deploying our own artifacts and for also caching the jcenter (and any third-party) artifacts. I can ask all developers to modify their settings.xml file as it will be a one-time activity so that's not a problem.
I saw this link by #helmedeiros which describes making changes in <repositories> and <pluginRepositories> section of settings.xml file. However, those are the sections where i specify URL for our Artifactory server. If i replace my Artifactory URL, then it would mean that i will be able to both fetch and upload artifacts from jcenter which is not what i want.
How can i ensure that all developers are only able to pull (NOT deploy/upload) from jcenter and deploy/upload ONLY to Artifactory?
Here's what we have right now in 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">
<servers>
<server>
<username>${security.getCurrentUsername()}</username>
<password>${security.getEscapedEncryptedPassword()!"*** Insert encrypted password here ***"}</password>
<id>central</id>
</server>
<server>
<username>${security.getCurrentUsername()}</username>
<password>${security.getEscapedEncryptedPassword()!"*** Insert encrypted password here ***"}</password>
<id>snapshots</id>
</server>
</servers>
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>libs-release</name>
<url>https://inhouse-artifactory/artifactory/libs-release</url>
</repository>
<repository>
<snapshots />
<id>snapshots</id>
<name>libs-snapshot</name>
<url>https://inhouse-artifactory/artifactory/libs-snapshot</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>plugins-release</name>
<url>https://inhouse-artifactory/artifactory/plugins-release</url>
</pluginRepository>
<pluginRepository>
<snapshots />
<id>snapshots</id>
<name>plugins-snapshot</name>
<url>https://inhouse-artifactory/artifactory/plugins-snapshot</url>
</pluginRepository>
</pluginRepositories>
<id>artifactory</id>
</profile>
</profiles>
<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>
</settings>
I will really appreciate any help in this regard.
I have exactly the same question :)
My solution is to create an Artifactory virtual repository (forgerock-third-party-virtual) to cache most of the public artifacts.
This virtual repository includes a remote repository based on jcenter:
On the virtual repository, there is no default deployment repository:
So with this setting, I hope the developers won't be able to push in this virtual repository.
According to the JFrog documentation, you have select one repository in this drop-down to be able to push into a virtual repository.
Regarding the deployment settings, we also have a parent pom where we specified our own repositories in the <distributionManagement> section.
On my build machines, I've added this profile (in .m2/settings.xml) to cache the artifacts:
<profile>
<id>force-third-party-repo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>forgerock-third-party</id>
<name>ForgeRock Third Party Repository</name>
<url>http://maven.forgerock.org/repo/forgerock-third-party-virtual</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>forgerock-third-party</id>
<name>ForgeRock Third Party Repository</name>
<url>http://maven.forgerock.org/repo/forgerock-third-party-virtual</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
I have other settings in this file to declare our own Artifactory repositories (for pushing/pulling our own artifacts) + some Maven credentials.
I did some tests with one of our Maven projects and it was working fine.
Once the new .m2/settings.xml will be ready, I'll push a template in an internal Artifactory repository, so the developers will be able to get this template with a curl command.
FYI, this is a POC for the moment. We want to move in production with these settings in a few days.

Configure nexus to look from local repository in the working directory first

I have a project that it has a directory named repo in which artifacts are stored. When I build it with maven, nexus says that the dependencies cannot be resolved; and when I remove nexus from maven settings, it can find the artifacts.
I want maven to look into this directory before asking nexus, or sth like that, so that I can build this without diactivating nexus every time. How can I do that? My current maven settings (~/.m2/settings.xml):
<settings>
<localRepository>/home/a/.m2/repository</localRepository>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<!--<url>http://nexus.yourdomain.nl/content/groups/public</url> -->
<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>
<!-- if you want to be able to switch to the defaultprofile profile put this in the active profile -->
<profile>
<id>defaultprofile</id>
<repositories>
<repository>
<id>maven.default</id>
<name>default maven repository</name>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>maven.snapshot</id>
<name>Maven snapshot repository</name>
<url>http://people.apache.org/repo/m2-snapshot-repository</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>defaultprofile</activeProfile>
</activeProfiles>
</settings>
Nexus website says the mirrorOf pattern of * causes any repository request to be redirected to this mirror and to your single repository group, which in the example is the public group.
It is possible to use other patterns in the mirrorOf field. A possible valuable setting is to use external:*. This matches all repositories except those using localhost or file based repositories.
Putting external:* instead of * solved my problem.
Update:
Second Solution:
Pass this option to maven works fine too:
mvn -Dmaven.repo.local=/path/to/repo

Instruct Maven to download third party plugin from https://oss.sonatype.org/content

There is a plugin I want to use on https://oss.sonatype.org/content/repositories/snapshots/
I know the maven command to run the plugin but how do I instruct Maven where the plugin should be downloaded from ?
I think I need to update my settings file to something like :
<mirrors>
<mirror>
<id>???</id>
<name>???</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</mirror>
</mirrors>
Is this correct ?
You should add a repository to your pom, that way the build will remain portable and other developers who build your code will not need to update their settings.xml.
This should do the job:
<repositories>
<repository>
<id>repo-id</id>
<name>repo-name</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
The best solution is to use the configuration in your current for only testing like this:
<project>
...
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<url>http://repository.apache.org/snapshots/</url>
</pluginRepository>
</pluginRepositories>
...
</project>
or change your settings appropriately like this:
<settings>
...
<profiles>
<profile>
<id>apache</id>
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<name>Maven Plugin Snapshots</name>
<url>http://repository.apache.org/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
...
</settings>
of course with activation of the profile. Or change the configuration of your repository manager.

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