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

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

Related

Problems with an oracle maven dependency

I'm trying to compile a project in Anypoint studio, and for some reason it's failing on one dependency -
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ucp</artifactId>
<version>12.1.0.2</version>
</dependency>
Should say, when I run this on my Windows laptop i have absolutely no issues and the jars download fine. Running it on my macbook, i get the following error -
Archive for required library: '/Users/matt/.m2/repository/com/oracle/jdbc/ucp/12.1.0.2/ucp-12.1.0.2.jar' in project 's-wms-hj-api' cannot be read or is not a valid ZIP file s-wms-hj-api
the message i get when i update project dependencies is -
[WARNING] Invalid POM for com.oracle.jdbc:ucp:jar:12.1.0.2, transitive dependencies (if any) will not be available, enable debug logging for more details
Is it something Mac related?
The dependency is not available in public repositories so the jar has to be installed manually in the local Maven repository. The error was likely because it was incorrectly installed.
The warning is the expected result because that method doesn't provide the pom.
I'm not sure why you don't see the warning in Windows. Perhaps a different Maven version or using a repository manager?
I've always dealt with Oracle the following way (I'm running a mac as well, though it really doesn't matter):
Download the .jar file from the internet somewhere. Install the dependency using Maven using these instructions: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Here's an example of the command, assuming you downloaded the jar for ucp version 12.1.0.2, you are currently in that directory, and the file is called ucp-12.1.0.2.jar:
mvn install:install-file \
-Dfile=ucp-12.1.0.2.jar \
-DgroupId=com.oracle.jdbc \
-DartifactId=ucp \
-Dversion=12.1.0.2 \
-Dpackaging=jar
This should install into your default ~/.m2/repository. If you want you can verify this with:
ls -al ~/.m2/repository/com/oracle/jdbc/ucp/12.1.0.2.jar
You should see a pom file in there.
Maven will now be ready to grab this file when you specify them in your pom. Keep in mind that the groupId, artifactId, and version parameters for the mvn install command relate exactly to what you'd put in the pom for those same fields.

Maven SNAPSHOT are taken into account as RELEASE

I have Java projects compiled with maven. Each project has its own POM which looks like the following :
<project ...>
<groupId>group.id</groupId>
<artifactId>scripts</artifactId>
<version>1.0.1-SNAPSHOT</version>
...
<dependencies>
<dependency>
<groupId>global</groupId>
<artifactId>common</artifactId>
<version>10.2.3-SNAPSHOT</version>
</dependency>
</dependencies>
...
</project>
SNAPSHOT as a RELEASE
If I do a mvn install, it will compile/install it the first time I execute the command.
Next times, it won't install it. It seams that maven consider it as a RELEASE, weither it is a SNAPSHOT.
I can see that in the timestamp included into the installed package. if I don't change the version number, the timestamp does not change either.
I believe I miss a maven configuration but this impacts a lot our development as we're force to change the projects versions (and their dependancies) for every test deployment.
Notes :
I'm using maven 3.3.3. It looks like I had not this behavior in 3.1.0 (I'm not 100% sure of the old version. Very old anyway).
Maven is coupled with Nexus. Maybe the issue comes from it and I'm focussing on the wrong horse.
Thank you for the hints.
Edit :
the question is :
How can I get maven installing SNAPSHOTs everytime instead of the first time only like a RELEASE ?
EDIT SOLVED
We've always done mvn install to compile and install the sources. For some reason, we haven't used the mvn clean command.
The correct maven command to recompile all the sources is mvn clean install.
Thank you #VinayVeluri
If versions are needed for the code changes, Please version yourself for the code base, though it is cumbersome gives the idea of versioning.
If it has to automatic regarding the snapshots update, then use
mvn -u clean install
Once added, this command line arg forces Maven to check all snapshots in a remote repository and update your local repository if it’s out of date.
source

Force mvn eclipse:eclipse to pick latest jar

I have a maven dependency which requires org\drools\drools-compiler 4.0.3 (drools-compiler 4.0.3 is not mentioned in my pom.xml but gets downloaded as a dependency). Now I am trying to integrate Drools framework with Spring. This requires me to use drools-spring 5.4.0.Final jar. Hence I have added the below pom entry.
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-spring</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>5.4.0.Final</version>
</dependency>
Now when I run a mvn clean install , I see the dependency drools-spring and corresponding dependencies (drools-compiler 5.4,drools core 5.4) getting downloaded, but however after doing mvn eclipse:eclipse, I still dont find the new dependencies showing up under maven dependencies in Eclipse classpath. In fact I still see the old 4.0.3 version under dependencies for drools-compiler and drools-core. I have tried an eclipse restart. Doesnt help either. How do I show the new 5.4 version jar instead of the 4.0.3 ?
As you mentioned you are using eclipse i will suggest to use maven eclipse plugin from eclipse market place.
When you will add maven plugin from eclipse you will able to see a Maven menu item when you will right click on the maven project then inside that menu you will able to see Update Maven project click on that.
If you do not want to do above two steps run the below command with -U.
Check below
mvn eclipse:eclipse -U
OR
mvn install -U
OR
mvn clean -U
See Maven: The Complete Reference, 6.1.11. Downloading and Verifying Dependencies or mvn --help:
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
Maven command with
I figured out I was adding the pom entries in the wrong pom.xml. Ideally the pom entries should be present in the service impl pom.xml.

How to install software in Maven? [duplicate]

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.

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