How to install software in Maven? [duplicate] - java

This question already has answers here:
How to simply download a JAR using Maven?
(16 answers)
Closed 9 years ago.
So I'm trying to wrap my head around Maven. After installing it I now want to install JodaTime because it's a dependency for another project.
I know I can download it and do a mvn clean install from within the JodaTime dir. From my experience with other package managers such as apt I suspect there is also a way of installing new software without manually downloading and untarring though. I just tried mvn install JodaTime, but I get an error saying:
Unknown lifecycle phase "JodaTime". You must specify a valid lifecycle
phase [etc. etc.]
So my question; is there a way to install software in Maven without manually downloading it?

If your project needs joda-time artifact as a dependency, you don't need to install it, all you need is to specify a dependency:
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
If you're trying to build joda-time yourself from source in case you wanted to modify the source of joda-time then you need to check out the source from https://github.com/JodaOrg/joda-time and run mvn install to install it to your local repository.

Maven is not build to do that (like sudo apt install in linux). But the closest is this as far as I know
dependency:get mvn dependency:get -DrepoUrl=http://repo1.maven.org/maven2/ \
-DgroupId=junit -DartifactId=junit -Dversion=4.8.2 \
Check this also

Usually it is enough to just define a dependency in your pom.xml and MAven will do the right thing:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.6</version>
</dependency>
In some cases you might need to define additional repositories in your settings.xml, but usually that is all you have to do.

Related

Maven dependency from local file - resolve any (*) symbol

I want to use missioncontrol module which is not published at maven repository and is installed with JDK. I could manually install it by mvn install like it's done in this script. But I'd prefer maven to resolve this dependency automatically.
The issue is I don't know exact version of module on each machine (assuming that JAVA_HOME is defined for every machine). And maven does not seem to automatically resolve the * placeholder:
<dependency>
<groupId>com.jrockit.mc</groupId>
<artifactId>com.jrockit.mc.common</artifactId>
<version>LATEST</version>
<scope>system</scope>
<systemPath>${env.JAVA_HOME}/lib/missioncontrol/plugins/com.jrockit.mc.common_*.jar</systemPath>
</dependency>
Is there any proper way to do this? Or is it a bad practice for some reason? Thanks.
Does your jar file name changes with java version

Maven: eliminate all but latest version of library in local repo

There's a library that I am using in my project and I have gone through many versions of it during development, so my local Maven repository contains many of the older versions of this library.
I suspect conflicts might be causing some strange issues I have been having, so I want to eliminate all but the latest version of this library from my local maven repository.
How do I do this?
If it makes any difference, I am currently using a SNAPSHOT version of this library (as recommended by its developers).
PS: deleting the entire repository is not a very practical option, as the repo is currently over 1.2G and I have a slow, capped connection.
If you know the dependency coordinates you can delete them manually from the local directory. For example if this was your dependency:
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.9</version>
</dependency>
On Linux you could run:
rm -rf ~/.m2/repository/org/glassfish/jersey/jersey-bom/
On Windows you can use explorer to delete it or run:
rd /S /Q C:\Users\%username%\.m2\repository\org\glassfish\jersey\jersey-bom\
purge-local-repository plugin for maven could help in this situation.
Try to execute maven with following command :
mvn clean dependency:purge-local-repository -DreResolve=false -Dverbose=true -DactTransitively=false

Building maven project with custom version of artifact instead of the one from repo

I'm new to maven, sorry for nub question: need to build maven project using my version of artifact instead of the one from repo.
More detailed:
I downloaded jboss sources from github and built them using maven 3. It was great! I need to do some changes in jboss dependancy called "picketbox". Now it is an artifact in jboss's "pom.xml".
I built my own version of picketbox in my_picketbox.jar file. How can I tell maven to use my .jar instead of the one from repo?
Maybe I'm missing something but if you explicitly renamed the artifact then it will just be a matter of replacing picketbox with my_picketboxin the relevant JBoss POM-file(s).
<dependency>
<groupId>...</groupId>
<artifactId>my_picketbox</artifactId>
<version>...</version>
</dependency>
And of course, you make sure your artifact is in your local repo by mvn install'ing it.
Cheers,
i would install the jar file using the maven-install plugin.
http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html
First, install your version of Picketbox into your local Maven repository. If your custom version is a Maven project, you can do that by running
mvn install
If your custom Picketbox version is not a Maven project, install the Jar itself into your local Maven repository like this:
mvn install:install-file -Dfile=my_picketbox.jar -DgroupId=org.picketbox -DartifactId=my_picketbox -Dversion=2.3 -Dpackaging=jar
Then change the version of Picketbox that JBoss depends on by adding this snippet to the pom.xml file of the JBoss project you're building (replace the existing dependency on pocketbox with this one):
<dependency>
<groupId>org.picketbox</groupId>
<artifactId>my_picketbox</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>x.x.x</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/xxxx.jar</systemPath>
</dependency>
This allows you to reference libaries that are not stored localy rather than maven central.
Maven takes what has been specified in POM files; mind you, that the POM including the dependency you wrote about can be buried very deeply, not even within the sources you downloaded.
Search your project tree for the artifact in question and replace its version number to your version. If this does not help, do as Nils says in the other answer, but remember to install the artifact in the original version (you may have to find it first in your local repository and remove it).

Maven2: use ${basedir} in jar path

I am using an external, proprietary jar in my project. When I hard-code the path as follows in my pom.xml, it works fine:
<dependency>
<groupId>com.foo.bar</groupId>
<artifactId>bar</artifactId>
<version>5.2</version>
<scope>system</scope>
<type>jar</type>
<systemPath>D:\workspace\myproj\external\companyname\lib\proprietary_api.jar</systemPath>
</dependency>
However, when I try to use the ${basedir} variable, maven can't find the jar:
<dependency>
<groupId>com.foo.bar</groupId>
<artifactId>bar</artifactId>
<version>5.2</version>
<scope>system</scope>
<type>jar</type>
<systemPath>${basedir}\external\companyname\lib\proprietary_api.jar</systemPath>
</dependency>
The pom is located in D:\workspace\myproj
This also needs to be cross-platform compatible (dev on Windows, deploy on Linux).
Thanks!
It is wrong to use system scope for your proprietary JARs. You should deploy or install it into the local/central repository.
I'm not sure this will help, but try using forward (/) instead of backward (\) slashes. Also, try running it with mvn -e and mvn -X (the latter will produce a lot of debugging lines) - this might help you pinpoint the problem.
Here's an example:
http://jmonkeyengine.org/groups/contribution-depot-jme3/forum/topic/maven-2-pomxml
of using ${basedir} in the same way you want.
Btw, why don't you mvn install:install-file the dependency instead of using systemPath? See:
http://maven.apache.org/plugins/maven-install-plugin/usage.html
In order to be cross-platform compatible use ${file.separator} instead of the slashes..
so that will automatically convert to OS specified format
To make it work both on windows and linux, you have to start using profiles. In that way, a particular profile will get activated based on the OS and the build will become portable.
In each profile, you can define a property called jarPath (just an example) and refer that property in your dependency.
Maven Profiles Introduction
Look into the OS tag and configuration tag of the profile. Make sure your build are always portable and less manual steps needs to be done.involved.
Use forward slashes in the path.
The ${basedir} placeholder is extrapolated only once per Maven run. If this project is not the topmost project in your project hierarchy, then ${basedir} will be extrapolated to the location of the topmost project (i.e. the project where Maven started), not the current project.

Add a dependency in Maven

How do I take a jar file that I have and add it to the dependency system in maven 2? I will be the maintainer of this dependency and my code needs this jar in the class path so that it will compile.
You'll have to do this in two steps:
1. Give your JAR a groupId, artifactId and version and add it to your repository.
If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:
mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ....
2. Update dependent projects to reference this JAR.
Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:
<dependencies>
...
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
You can also specify a dependency not in a maven repository. Could be usefull when no central maven repository for your team exist or if you have a CI server
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>commons-utils</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-utils.jar</systemPath>
</dependency>
Actually, on investigating this, I think all these answers are incorrect. Your question is misleading because of our level of understanding of maven. And I say our because I'm just getting introduced to maven.
In Eclipse, when you want to add a jar file to your project, normally you download the jar manually and then drop it into the lib directory. With maven, you don't do it this way. Here's what you do:
Go to mvnrepository
Search for the library you want to add
Copy the dependency statement into your pom.xml
rebuild via mvn
Now, maven will connect and download the jar along with the list of dependencies, and automatically resolve any additional dependencies that jar may have had. So if the jar also needed commons-logging, that will be downloaded as well.
I'd do this:
add the dependency as you like in your pom:
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
run mvn install it will try to download the jar and fail. On the process, it
will give you the complete command of installing the jar with the error message. Copy that command and run it! easy huh?!
I'll assume that you're asking how to push a dependency out to a "well-known repository," and not simply asking how to update your POM.
If yes, then this is what you want to read.
And for anyone looking to set up an internal repository server, look here (half of the problem with using Maven 2 is finding the docs)

Categories