Howto disable mirror repository in maven settings - maven

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.

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.

Specifiy Nexus repository path at one place

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.

Can I have multiple local repository for maven?

In maven settings, there is an entity which refers the local repository:
<localRepository>~/.m2/repository</localRepository>
When I add another one, like this:
<localRepository>~/another/place</localRepository>
it raises Duplicated tag error.
Can I have multiple local repository or maybe add another direcotry to the local repository?
EDIT
This idea seems a possible answer, too:
mvn -Dmaven.repo.local=/path/to/repo
Yes you can have and you can do it in your POM.xml itself. Below is an example.
<project>
...
<repositories>
<repository>
<id>firstrepo</id>
<name>repo</name>
<url>http://myrepo.my</url>
</repository>
<repository>
<id>secondrepo</id>
<name>repo2</name>
<url>http://myrepo.yours</url>
</repository>
</repositories>
...
</project>
Second Method by creating profile in your settings.xml
For multiple local repositories you can have multiple settings.xml file.
In the command line specify alternate path using
mvn -Dmaven.repo.local=/path/to/repo For more information you can check this link. Hope it helps.

Accessing local repository in offline mode

I need to make Maven access local repository (file://) when it is ran in offline mode (basically, I am trying to setup repository hierarchy so it does not put artifacts where I don't need them).
This does not work out-of-the-box, though I always assumed this scenario is supported. Is there some flag to enable particular repository in offline mode?
For the dev environment only, if you use IntelliJ IDEA, you can configure Maven to work offline as follow:
Preferences... > Search for Maven > Work offline (checked)
It is possible to set up profiles for repositories utilizing the local file system rather than a network address.
<repository>
<id>mymaven</id>
<url>file://D:\mylocalrepo</url>
</repository>
According to documentation, it is also possible to reference offline mode in a property value.
${settings.offline}
You would then leverage these together to activate a given settings profile according to the examples here. (If Maven doesn't detect the property, try evaluating it directly using the above syntax.)
<profiles>
<profile>
<id>myNeededProfile</id>
<activation>
<activeByDefault>false</activeByDefault>
...
<property>
<name>offline</name>
<value>true</value>
</property>
...
</activation>
...
</profile>
</profiles>
I believe that the Maven Help Plugin can guide this development by computing which profile will be active under certain conditions.
I also think that this could be accomplished more simply by explicitly invoking a profile from the command line each time offline mode is requested.
mvn groupId:artifactId:goal -o -P profile-1,profile-2
Or, even more straightforwardly, by having two separate settings files and subbing them out specifically for the offline/online operations. You could write a command-line wrapper in whatever OS environment you're using to detect the offline request, then move and rename the files before executing the Maven commands, then move them back upon completion.
Maven always tried to connect to central repository. You can define own central (it is only property, which you can redefine)
<repository>
<id>central</id>
<name>My Central</name>
<layout>default</layout>
<url>${my_url}</url>
</repository>
Make the same for snapshot, plugin repository etc. Configuration you can use as profile - it will be optional. See ingyhere answer.

Maven unable to download from remote repository

I'm trying to build a Maven module that depends on SVNKit.
So my pom.xml looks like this:
<dependencies>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>tmatesoft-releases</id>
<url>http://maven.tmatesoft.com/content/repositories/releases/</url>
</repository>
</repositories>
When I try to do a mvn clean install, it looks like it attemps to look into a Nexus repository, and obviously cannot find it. I'm obviously trying to download the artifact from a remote repository at: http://maven.tmatesoft.com/content/repositories/releases
I took a look at my settings.xml file in my ~/.m2/ folder and I see that there is a mirror that looks like this:
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://Our_Nexus_Repository</url>
</mirror>
I thought that, if you define a <reposotiry> tag, your module's pom.xml will take precedence over what's defined in your settings.xml file.
Any help is appreciated. Thank you.
this is because the mirror will mirror all the repositories, : <mirrorOf>*</mirrorOf>
you can change replace it like this : <mirrorOf>*,!tmatesoft-releases</mirrorOf>
more info please visit : http://maven.apache.org/guides/mini/guide-mirror-settings.html
With the way that you're using the mirror statement in your settings.xml, you're directing maven to always go to your repository. If that is indeed the behavior that you wish, you should create a proxy for the tmatesoft-releases repository inside your nexus installation.
A common methodology for doing this is to setup a repository group for all releases you wish to have on your nexus installation. Then just add proxy repos to the group as needed, and users won't need to change their settings.xml to instantly have visibility to the new repos you add.

Resources