Can I add maven repositories in the command line? - java

I'm aware I can add maven repositories for fetching dependencies in ~/.m2/settings.xml. But is it possible to add a repository using command line, something like:
mvn install -Dmaven.repository=http://example.com/maven2
The reason I want to do this is because I'm using a continuous integration tool where I have full control over the command line options it uses to call maven, but managing the settings.xml for the user that runs the integration tool is a bit of a hassle.

You can do this but you're probably better off doing it in the POM as others have said.
On the command line you can specify a property for the local repository, and another repository for the remote repositories. The remote repository will have all default settings though
The example below specifies two remote repositories and a custom local repository.
mvn package -Dmaven.repo.remote=http://www.ibiblio.org/maven/,http://myrepo
-Dmaven.repo.local="c:\test\repo"

One of the goals for Maven't Project Object Model (POM) is to capture all information needed to reliably reproduce an artifact, thus passing settings impacting the artifact creation is strongly discouraged.
To achieve your goal, you can check in your user-level settings.xml file with each project and use the -s (or --settings) option to pass it to the build.

I am not sure if you can do it using the command line. You can on the other hand add repositories in the pom.xml as in the following example. Using this approach you do not need to change the ~/.m2/settings.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<repositories>
<repository>
<id>MavenCentral</id>
<name>Maven repository</name>
<url>http://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
...
<repository>
<id>Codehaus Snapshots</id>
<url>http://snapshots.repository.codehaus.org/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
...
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<name>Apache Snapshot Repository</name>
<url>
http://people.apache.org/repo/m2-snapshot-repository
</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
<pluginRepository>
<id>Codehaus Snapshots</id>
<url>http://snapshots.repository.codehaus.org/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
...
</project>

As
#Jorge Ferreira
already said put your repository definitions in the pom.xml. Use profiles adittionally to select the repository to use via command line:
mvn deploy -P MyRepo2
mvn deploy -P MyRepo1

I'll assume here that you're asking this because you occasionally want to add a new 3rd-party repository to your builds. I may be wrong of course... :)
Your best bet in this case is to use a managed proxy such as artifactory or nexus. Then make a one-time change in settings.xml to set this up as a mirror for the world.
Any 3rd party repos that you need to add from that point on can be handled via the proxy.

I haven't really used maven 2 before, our system is still working on maven 1.x because of some issues with maven 2.
However, looking at the documentation for maven 2 it seems that there aren't any specific System properties like that. However, you could probably build one into your poms/settings using the System properties. See System properties part of this http://maven.apache.org/settings.html
So you'd have ${maven.repository} in your settings file and then use the -Dmaven.repository like you do above.
I am unsure as to if this would work, but with some tweaking I am sure you can come up with something.

Create a POM that has the repository settings that you want and then use a parent element in your project POMs to inherit the additional repositories. The use of an "organization" POM has several other benefits when a group of projects belong to one team.

I am using xmlstarlet to achieve this. Tested for Maven 3 on CentOS 7, Maven 2 was not tested yet.
XML_FULLPATH="$HOME/.m2/settings.xml"
MIRROR_ID='example'
MIRROR_MIRROROF='*'
MIRROR_NAME='Example Mirror'
MIRROR_URL='http://example.com/maven2'
## Preview settings without comment:
xmlstarlet ed -d '//comment()' "$XML_FULLPATH"
## Add Mirror settings:
xmlstarlet ed -L \
--subnode "/_:settings/_:mirrors" --type elem --name "mirrorTMP" --value "" \
--subnode "/_:settings/_:mirrors/mirrorTMP" --type elem --name "id" --value "$MIRROR_ID" \
--subnode "/_:settings/_:mirrors/mirrorTMP" --type elem --name "mirrorOf" --value "$MIRROR_MIRROROF" \
--subnode "/_:settings/_:mirrors/mirrorTMP" --type elem --name "name" --value "$MIRROR_NAME" \
--subnode "/_:settings/_:mirrors/mirrorTMP" --type elem --name "url" --value "$MIRROR_URL" \
--rename "/_:settings/_:mirrors/mirrorTMP" --value "mirror" \
"$XML_FULLPATH"
## Remove Mirror settings by id:
xmlstarlet ed -L \
--delete "/_:settings/_:mirrors/_:mirror[_:id=\"$MIRROR_ID\"]" \
"$XML_FULLPATH"
The idea is from: How to insert a new element under another with xmlstarlet?.

Related

How do you upload a Maven artifact to Github Packages using the command line?

I am trying to upload a Maven artifact I haven't built to my Organization's GitHub package registry. I am using the deploy:deploy-file Maven plugin in order to do so. Here is the command I have been using:
mvn deploy:deploy-file
-Dfile=[THE JAR FILE]
-Durl=https://maven.pkg.github.com/[ORG]
-Dregistry=https://maven.pkg.github.com/[ORG]
-DgroupId=[GID]
-DartifactId=[ARTIFACTID]
-Dversion=[VERSION]
-DgeneratePom=false
-Dtoken=[MY GITHUB TOKEN]
As a result I am receiving 401 errors from Github.
I have made sure that:
I have sufficient permissions inside of my Organization (currently Owner).
The token i am using is valid and has the appropriated scopes: I put all of them on to test.
Also, the github package page states:
<!-- Just a single step: Deploy using a GitHub token -->
$ mvn deploy -Dregistry=https://maven.pkg.github.com/[org] -Dtoken=GH_TOKEN
Why can't I find any information in Maven documentation about registry or token parameters?
Can I upload this file to the organization's registry without any kind of XML configuration file, using only the cli?
Thanks in advance.
I had success with this:
mvn deploy:deploy-file -Dfile=./[JAR].jar
-DpomFile=./pom.xml
-DrepositoryId=github
-Durl=https://maven.pkg.github.com/[OWNER]/[REPO]
-Dtoken=GH_TOKEN
And a settings.xml in my maven home directory:
<settings>
<servers>
<server>
<id>github</id>
<username>[GITHUB USERNAME]</username>
<password>[GENERATED ACCESS TOKEN]</password>
</server>
</servers>
</settings>
And inside my POM:
...
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github.com/[OWNER]/[REPO]</url>
</repository>
</distributionManagement>
...
To workaround the repo issue - since I didn't want each package to be published to a different repo, I created a repo named packages and published the packages from all the other repos to it, using the same config as in the other two answers.
Url should have a repository name as well.
In one of my projects I have this in pom.xml
<distributionManagement>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/stirante/lol-client-java-api</url>
</repository>
</distributionManagement>

Building jersey example is failing

Sorry for this question, but I couldn't find anything similiar to this in the web, so here we go:
I am trying to install jersey as follows :
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false-DgroupId=com.example -DartifactId=simple-service-webapp -Dpackage=com.example-DarchetypeVersion=2.22.1
It's from their website for an example project. This starts to execute, so JAVA_HOME and mvn are set up properly. But as soon as the console states "Generating Project in Batch mode", it fails with a "Build Failure", Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin.... The desired archetype does not exist. I can upload a screenshot of the console log if needed.
From this url I could correctly generate the archetype whith the following step.
1-First I create an empty maven project
then I modified the pom file like this
<groupId>fr.toto</groupId>
<artifactId>tata</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<repositories>
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/snapshots/</url>
<layout>default</layout>
</repository>
</repositories>
Then I run the following command inside the root dir of the empty project I created at step 1 :
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=fr.cnamts -DartifactId=tata -Dpackage=fr.cnamts \
-DarchetypeVersion=2.22.1
And voilĂ  it works!

maven: how to prevent plugin updates

Similar to this question, I can't do a glassfish deploy because some server in Australia is down at the moment. I've had the artifacts cached locally for months.
How can I tell maven to not attempt to update the plugin (and its dependencies)? I tried adding the following to my pom, but it didn't help:
<pluginRepositories>
<pluginRepository>
<id>ocean</id>
<url>http://maven.ocean.net.au</url>
<releases>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
I also tried the command line switches. There's -npu, which didn't seem to help with Maven 2.2.1. On Maven 3 the docs say "Ineffective, only kept for backward compatibility", so I don't have a lot of hope for that one.
We will be moving to Nexus soon, but there ought to be a simple way to tell maven not to attempt a plugin update.
Use the -o or --offline switch to work offline - this should make Maven not bother to check any repositories for new updates or snapshots, as if you did not have network access.
Additionally - are you specifying the <version> for each of your plugins? There should be no need to check for updates to release versions of a plugin.
If you use maven 2 there's a file called plugin-registry.xml where you can specify the version of each plugin. The ugly thing about this file, is that it has to be copied / installed on each box that uses maven (ci server, dev boxes, etc).
The benefit is that you can specify a version, and one it's downloaded, it won't be updated again.
If you migrate to maven 3, this has been sorted out, and you can specify the version you want on the pom.

Eclipse | Maven : Multiple annotations found at this line

I'm trying to import a maven project into Eclipse.
I'm using Helios. I've downloaded m2eclipse. I've imported the project.
But I'am having so much troubles to compile the project.
The full project contains 5 Eclipse projects, ie: prj1, prj2, prj3, prj4 and prj5
If I look the (Eclipse) marker at prj1/pom.xml I have this troubles:
Multiple annotations found at this line:
- Missing artifact log4j:log4j:jar:1.2.15:compile
- Missing artifact org.apache.xmlbeans:xmlbeans-xpath:jar:2.4.0:compile
- Missing artifact org.apache.ws.commons.axiom:axiom-dom:jar:1.2.5:compile
- Missing artifact org.apache.httpcomponents:httpcore:jar:4.0-alpha5:compile
.... and so many more ...
If I understood how maven works those dependecies must be downloaded my maven, am I wrong?
Why is it that those dependencies are not being downloaded? Should I download one by one, by hand?
It is not a unique issue, happens every now and then (sometimes due to a slow connection and sometimes due to proxy servers not allowing to download)
You can get rid of this by either of the following ways:
1) Force Update: Right Click on the Project in Eclipse -> Maven -> Update Project
On this screen select the check box Force Update for Snapshots/Releases
2) Clearing Maven Cache: If you are still facing a problem, go to the local repository on your system, which might be present at C:\Users\myusername\.m2\repository and delete the .cache folder and then follow step 1.
If you're still facing issues after this, manually go to the org/apache folder and delete everything and then follow step 1. (This will definitely solve the issue.)
Make sure your build path is going to /target/classes
to check:
right click on your project and go to properties
-> choose java build path
-> then go to the source tab
the default output folder is on the bottom
With Eclipse/Maven projects, I've always had more luck building from the command line (mvn clean package) first in order to download all of the dependencies. Once that completes without errors, then I import the project into Eclipse.
It can be a mistake in pom repository / dependency definition
i.e. I want include in pom groupId:org.clapper artifactId:javautil
The home page of clapper say use:
http://software.clapper.org/javautil
<repositories>
<repository>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<id>clapper-org-maven-repo</id>
<name>org.clapper Maven Repo</name>
<url>http://maven.clapper.org/</url>
<layout>default</layout>
</repository>
...
</repositories>
and
<dependency>
<groupId>org.clapper</groupId>
<artifactId>javautil</artifactId>
<version>3.1.2</version>
</dependency>
It don't work! I got "Multiple annotations found at this line"!!!
If I use dependency from
https://github.com/shilad/wikibrain/blob/master/wikibrain-utils/pom.xml
<repository>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<id>clapper-org-maven-repo</id>
<name>org.clapper Maven Repo</name>
<url>http://maven.clapper.org/</url>
<layout>default</layout>
</repository>
+
<dependency>
<groupId>org.clapper</groupId>
<artifactId>javautil</artifactId>
<version>3.1.1</version>
</dependency>
It work fine!!!

How can I deploy artifacts from a Maven build to the SourceForge File Release System?

I am using SourceForge for some Open Source projects and I want to automate the deployment of releases to the SourceForge File Release System. I use Maven for my builds and the standard SFTP deployment mechanism doesn't seem to work unless you do some manual preparation work. I have come across some old postings on other forums suggesting that the only approach is to write a Wagon specifically for SourceForge.
Has anybody had any recent experience with this?
I'm not able to test this to confirm, but I believe it is possible without writing any plugins.
You can deploy to SourceForge using SCP, and the maven-deploy-plugin can be configured to use SCP so it should work. You can also deploy your site to SourceForge via SCP.
You would configure the SourceForge server in your settings.xml to use a "combined" username with a comma separator. With these credentials:
SourceForge username: foo
SourceForge user password: secret
SourceForge project name: bar
Path: /home/frs/project/P/PR/PROJECT_UNIX_NAME/
- Substitute your project UNIX name data for /P/PR/PROJECT_UNIX_NAME
The server element would look like this:
<server>
<id>sourceforge</id>
<username>foo,bar</username>
<password>secret</password>
</server>
And the distributionManagement section in your POM would look like this:
<!-- Enabling the use of FTP -->
<distributionManagement>
<repository>
<id>ssh-repository</id>
<url>
scpexe://frs.sourceforge.net:/home/frs/project/P/PR/PROJECT_UNIX_NAME</url>
</repository>
</distributionManagement>
Finally declare that ssh-external is to be used:
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh-external</artifactId>
<version>1.0-alpha-5</version>
</extension>
</extensions>
</build>
If this doesn't work, you may be able to use the recommended approach in the site reference above, i.e. create a shell on shell.sourceforge.net with your username and project group:
ssh -t <username>,<project name>#shell.sf.net create
Then use shell.sourceforge.net (instead of web.sourceforge.net) in your site URL in the diestributionManagement section:
<url>scp://shell.sourceforge.net/home/frs/project/P/PR/PROJECT_UNIX_NAME/</url>
I have uploaded an example to sourceforge.net at: http://sf-mvn-plugins.sourceforge.net/example-1jar-thinlet/
You can check out it via svn - so you can see how to use plugins for upload and download of and to sourceforge.net file system area and web site.
The main points to upload are to use sftp:
Add this similar code to your pom.xml
<distributionManagement>
<!-- use the following if you're not using a snapshot version. -->
<repository>
<id>sourceforge-sf-mvn-plugins</id>
<name>FRS Area</name>
<uniqueVersion>false</uniqueVersion>
<url>sftp://web.sourceforge.net/home/frs/project/s/sf/sf-mvn-plugins/m2-repo</url>
</repository>
<site>
<id>sourceforge-sf-mvn-plugins</id>
<name>Web Area</name>
<url>
sftp://web.sourceforge.net/home/groups/s/sf/sf-mvn-plugins/htdocs/${artifactId}
</url>
</site>
</distributionManagement>
Add similar code to settings.xml
<server>
<id>sourceforge-sf-mvn-plugins-svn</id>
<username>tmichel,sf-mvn-plugins</username>
<password>secret</password>
</server>
<server>
<id>sourceforge-sf-mvn-plugins</id>
<username>user,project</username>
<password>secret</password>
</server>
The main point for download is to use the wagon-http-sourceforge maven plugin - please see at: sf-mvn-plugins.sourceforge.net/wagon-http-sourceforge/FAQ.html
Please add the following code to your pom.xml
<repositories>
<repository>
<id>sourceforge-svn</id>
<name>SF Maven Plugin SVN Repository</name>
<url>http://sf-mvn-plugins.svn.sourceforge.net/svnroot/sf-mvn-plugins/_m2-repo/trunk</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>sourceforge-frs</id>
<name>SF Maven Plugin Repository</name>
<url>http://sourceforge.net/projects/sf-mvn-plugins/files/m2-repo</url>
</pluginRepository>
</pluginRepositories>
<build>
<extensions>
<extension>
<groupId>net.sf.maven.plugins</groupId>
<artifactId>wagon-http-sourceforge</artifactId>
<version>0.4</version>
</extension>
</extensions>
:
</build>
It looks like I am going to have to write this myself.
https://sourceforge.net/projects/wagon-sf/
After trying this a number of times, I finally got it to work -- with sftp not scp. This should work from a unix box (or Mac) -- I'm not sure about sftp clients for Windoze. I am using mvn version 2.2.0 and I don't think I have any special plugins installed. This deploys the various mvn packages to the Files section of my project page.
You'll need to change the following in your settings to get it to work:
user -- replace with your sourceforce username
secret -- replace with your password
ormlite -- replace with your project name
/o/or/ -- replace with the first char and first 2 chars of your project name
In my $HOME/.m2/settings.xml file I have the following for the SF server:
<server>
<id>sourceforge</id>
<password>secret</password>
<filePermissions>775</filePermissions>
<directoryPermissions>775</directoryPermissions>
</server>
I don't specify the username in the settings.xml file because it needs to be username,project and I want to deploy multiple packages to SF. Then, in my pom.xml file for the ormlite package I have the following:
<distributionManagement>
<repository>
<id>sourceforge</id>
<name>SourceForge</name>
<url>sftp://user,ormlite#frs.sourceforge.net:/home/frs/project/o/or/ormlite/releases
</url>
</repository>
<snapshotRepository>
<id>sourceforge</id>
<name>SourceForge</name>
<url>sftp://user,ormlite#frs.sourceforge.net:/home/frs/project/o/or/ormlite/snapshots
</url>
</snapshotRepository>
</distributionManagement>
Obviously the /releases and /snapshots directory suffixes can be changed depending on your file hierarchy.
Where timp = user and webmacro = project
scp url does not work:
scp://timp,webmacro#shell.sourceforge.net:/home/groups/w/we/webmacro/htdocs/maven2/
sftp url works:
sftp://timp,webmacro#web.sourceforge.net:/home/groups/w/we/webmacro/htdocs/maven2
or for project release artifacts:
sftp://timp,webmacro#web.sourceforge.net:/home/frs/project/w/we/webmacro/releases
scp will work to shell.sourceforge.net, but you have to create the shell before use with
ssh -t timp,webmacro#shell.sourceforge.net create
This really did not turn out to be that hard. First up I had the mvn site:deploy working following the instructions at this sourceforge site. Basically you start the sourceforge shell with
ssh -t user,project#shell.sourceforge.net create
That will create the shell at their end with a folder mounted to your project on a path such as (depending on your projects name):
/home/groups/c/ch/chex4j/
In that shell I on the sourceforge server I created a folder for my repo under the project apache folder "htdocs" with
mkdir /home/groups/c/ch/chex4j/htdocs/maven2
In my settings.xml I set the username and password to that shell server so that maven can login:
<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>chex4j.sf.net</id>
<username>me,myproject</username>
<password>password</password>
<filePermissions>775</filePermissions>
<directoryPermissions>775</directoryPermissions>
</server>
</servers>
</settings>
In the pom.xml you just need your distibutionManagement section setup to name the server by ID that you set the password for in your settings file:
<distributionManagement>
<site>
<id>chex4j.sf.net</id>
<url>scp://shell.sourceforge.net/home/groups/c/ch/chex4j/htdocs/
</url>
</site>
<repository>
<id>chex4j.sf.net</id>
<name>SourceForge shell repo</name>
<url>scp://shell.sourceforge.net/home/groups/c/ch/chex4j/htdocs/maven2</url>
</repository>
</distributionManagement>
There the repository entry is the one for the mvn deploy command and the site entry is for the mvn site:deploy command. Then all I have to do is start the shell connection to bring up the server side then on my local side just run:
mvn deploy
which uploads the jar, pom and sources and the like onto my sourceforge projects website. If you try to hit the /maven2 folder on your project website sourceforge kindly tell you that directory listing is off by default and how to fix it. To do this on the server shell you create a .htaccess file in your htdocs/maven2 folder containing the following apache options
Options +Indexes
Then bingo, you have a maven repo which looks like:
http://chex4j.sourceforge.net/maven2/net/sf/chex4j/chex4j-core/1.0.0/
Your sf.net shell it shuts down after a number of hours to not hog resources; so you run the "ssh -t ... create" when you want to deploy the site or your build artifacts.
You can browse all my maven project code under sourceforge to see my working settings:
http://chex4j.svn.sourceforge.net/viewvc/chex4j/branches/1.0.x/chex4j-core/
SCP URL does work. But do not use ":" after server name. MVN tries to read the following test as integer (port number).
You do not need to establish tunnels as simbo1905 did.
The Maven SourceForge plug-in does not work with Maven 2. Also I believe this plug-in uses FTP which is no longer supported.
I found that CruiseControl can upload releases to SFEE and also works with Maven and Maven2

Categories

Resources