How to properly remove a dependency in a Maven project - maven

I have a Java Maven project where I have some dependencies defined in the pom.xml file. Recently I decided to move from Junit to TestNG so I deleted the Junit dependency from my pom.xml and added the TestNG one.
I was expecting to see the Junit jar library disappear from the Maven Dependencies folder as a part of the process but that didn't happen. I can still see the jar file in the dependencies folder and it is still being used by my test cases.
I can see TestNG jar is there as well but it's not being used. I can change it manually of course but that wasn't my intention.
Am I doing something wrong are there any additional steps that I missed that will allow me to remove the old dependency?

The following applies for the global .m2 folder where maven dependencies can be downloaded and is common to your maven projects.
the .m2 folder will not clean by itself as those dependencies could be used in another project so its wrong assumption that replacing a dependency in a a pom file will automatically remove from your repo.
you can look at the depedency plugin and run the following
mvn dependency:purge-local-repository
or
mvn dependency:purge-local-repository -DreResolve=false

Related

Why doesn't the dependency contain a jar in the maven public repository https://mvnrepository.com?

I a maven rookie and am wondering how to get a binary jar file if it is not already in the repo. Specifically i'm in need of: jackson-dataformats-text-2.13.0.jar. Do I need to build it myself? I'm used to creating a project and marking a library as a dependency and seeing the jar downloaded into my .m2 cache but all i see in my cache is:
jchan#jchan-Z170N:~/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformats-text/2.13.0$ ls
jackson-dataformats-text-2.13.0.jar.lastUpdated jackson-dataformats-text-2.13.0.pom.sha1
jackson-dataformats-text-2.13.0.pom _remote.repositories
Can someone advise how I am to get a built version of the jar from maven central?
We are still maintaining our ant build and I need the jar file for this. (i know i know, ancient stuff but team is not ready to port just yet).
parent pom don't contain jar file
This is the reason why no bundle link is present on the official public maven repository https://mvnrepository.com
The maven dependency is not a jar, is a parent. So the extension is: .pom which is just a plain pom.xml
Parent dependencies don't contain compiled class like .jar.
In your specific case, there are another dependencies who contains jars:
https://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/jackson-dataformat-yaml/2.13.0/
https://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/jackson-dataformat-xml/2.13.0/
advice
Check what classes do you need on your ant project and search if exist a jar (with the classes you need) on https://mvnrepository.com
Another option is to get all the dependencies from pom :
https://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/jackson-dataformats-text/2.9.0/jackson-dataformats-text-2.9.0.pom and download them into your ant project. In theory is the same of add the parent pom in a maven project

how to add my custom jar dependencies to my webapplication?

I have child project with some dependencies. When I upload this jar to nexus it's ok.
But when I use this jar in my webapplication and I use mvn clean install -U command. Maven create a war, but the child project dependencies isn't in the war lib folder.
What is the problem?
Check with mvn dependency:tree on the project. Often, the problem is a wrong scope. Make sure the dependencies all have scope compile.

Maven on the empty project downloads tons of dependencies

I use Maven on daily basis in my work for more then 5 years. But I never tried to test the minimum dependencies project.
So I created a new directory on my disk and put inside a pom.xml file. It is the most simple pom file you can create. It contains only this:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>info.jikra</groupId>
<artifactId>whatever</artifactId>
<version>0.1</version>
</project>
And that's it. There is nothing more. No other directories, not a single Java file, nothing.
Then I cleared my local repository and ran mvn clean install in the folder with my pom file.
Maven downloaded tons of dependencies I don't need. My project is empty, there is only one pom file. Yet, there are more then 7,6M of files in my local repository, now.
I'm not any kind of Maven master, so I wonder why all those dependencies are necessary. Does anyone know?
Your project has some predefined plugins declared as well as packaging (default is jar) which defines a list of additional plugins as well as their bindings. Those are getting downloaded along with their transitive dependencies.
You can run mvn help:effective-pom in order to see what's actually present in your project.
You could also see plugins and their dependencies with: mvn dependency:resolve-plugins

maven - how does it work? Missing some some jars

I am trying to move my MyEclipes projects to maven. But of course there are problems. After creating a web priject I get missing jar files - about 5
org.springframework.security jar files e.g. org.springframework.security.ldap-3.0.5.RELEASE
show as missing in the jar build path. They are not in the corresponding .m2 directory. I un-installed ME4S, and deleted .m2, which force .me to be rebuilt on re-install, but it has the same problem.
How do I fix this?
It would be very helpful to understand how the .m2 process works - where is this coming from and how is it controlled?
I am not sure about the MyEclipse part, but this seems to be a pure maven question.
Maven (2/3) uses the pom.xml. This file describe your project. In that file you should define a list of dependencies (which can have their own dependencies and so on).
Maven read the pom.xml and build the classpath accordingly using direct and transitive dependencies.
You can use the mvn dependency:tree command to see how your classpath is built.
More on the plugin page

Adding Jar files to Maven

I have started to work on maven project recently.
The piece of code I am writing needs JAR files that are not a part of the project.
When I add the JARS TO THE build path and use a mvn clean install, the build is failing with errors saying that the classes that were supposed to be in the jar were not found.
Is there anything that i am missing?
Is there a different way to add the JAR's in maven projects?
If the JARs are already hoisted in some public Maven repositories, add them to the <dependency> section in the pom.xml . You may have to configure the address of these public Maven repositories in the <repositories> section in the pom.xml in order to cause Maven can connect them.
Otherwise , you have to use the install command to include these JARs into the your local repository and then add their <dependency> section in the pom.xml
The command to install the JARs into your local repository:
mvn install:install-file -DgroupId=com.abc -DartifactId=XXXXX
-Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jars
It sounds like you don't understand how Maven works. You don't add jars to the build path, you declare them in the pom, and let Maven download to the local drive, and it adds it to the build path for you. I would recommend you read this 5 minute intro, and understand how the dependency management works.

Resources