maven - how does it work? Missing some some jars - maven

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

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

can't find my maven artifacts

I'm working in eclipse and I have a problem that I can't import my personal java libraries.
I created the libraries and 'installed' them into my local maven repo (using mvn install). This created a subdirectory related to the 'version' name that was in the POM file from when I ran the command. Which seemed fine.
So in this directory there where the usual jar files and other stuff.
When I released this file I manually changed the name of the version in the POM. going from 0.0.1-SNAPSHOT to 0.0.1-RELEASE
This seems to have worked as I would have expected.
However I can't seem to find import the new release jar.
Using the maven repositories browser in eclipse I can see that the new artifact is in the 'local' repository.
I try to add the dependency in the following methods:
Select the main project -> Maven -> add dependency.
This adds the dependency details into the pom but with a type value detail of <type>pom.lastUpdated</type>
Select the project pom.xml file -> Maven -> add dependency.
This time the artifact for the 0.0.1-RELEASE is greyed out I can select it, but I guess nothing is actually happening.
The original 0.0.1-SNAPSHOT it selectable, and if I use this I do not have a <type> detail in the pom.
I don't understand why there is a difference in the RELEASE and SNAPSHOT artifacts, as they have both been generated in the same way, and clearly they are both visible in the browser, the contents of the directory on disk are the same. The file names and contents are identical with the exception of the word RELEASE or SNAPSHOT.
I know that I can simply add in the RELEASE jar to my build path, but this seems to be a ridiculous thing to have to do if I intend to use maven (or do I need to do this).
I don't want to use an external repo for storing my artifacts, and I'm not too keen to go to the trouble of installing nexus (or similar) on my local machine (just because I've had trouble with it in the past).
What am I missing so as I can get my maven project to see my local repository and all its artifacts.
Thanks in advance.
David
ps I've already tried things such as mvn dependency:purge-local-repository which definitely pulled in / updated all the local jar dependencies.
So I've managed to work around my problem.
As such this solution is ridiculous, and breaks all the purpose of maven.
So I found (from running maven from the cli) that there was an error in the parent of the project I was attempting to use.
The parent was missing a direct link to the scm plugin (version error).
Once I solved this problem, I then returned to the sub project, and got a lot of errors from missing stuff from the parent.
Essentially it was not 'seeing' all the log4j dependencies.
The solution (well non-solution really).
Add all the log4j dependencies to the sub project.
Edit in SCM and surefire test plugins (as it also started to fail the test code due to missing junit).
So this is great.
I have to define all my dependencies on log4j and in my sub / child project's pom.xml file.
I also need to define them all in my parent's pom.
As it then still refused to run tests in my new project (that used the above as a dependency), and refused to find log4j also. I then decided to add all of these as dependencies for my current project.
Great.
I thought the whole point of maven was I could define my dependency on log4j in my my logging library that I use (which is the dependency), and then it would 'automagically' pull in all the required from this dependency.
Clearly not.
As stated at the start. This is NOT AN ANSWER it is a crazy work around.
My logging library that I use should be able to define its own requirement on a specific log4j version (such as moving from log4j to log4j2), and then when I include this as a maven dependency any change to the required dependency should be seen automatically.
But No : I have to import the dependency on log4j in my other projects also. So now if I had updated my logging library from log4j to log4j2 I would need to go to all my project that use this library and update their pom's to ensure that I have the correct version of log4j.
Seems the whole point of maven has just been lost!
Can someone please tell me where I am going wrong!
David.

How to properly remove a dependency in a Maven project

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

What is the purpose of providing a downloaded pom.xml on mvnrepository.com

On mvnrepositry, when you search for a certain module, there's a link to download the binary. For some versions it has a pom.xml file available for download instead of the jar. What are you supposed to do with that pom.xml? It seems like if I specify a version that does not have a downloadable jar, but instead downloadable pom.xml, my maven build will fail. Is what I'm seeing correct?
Modules that only have pom files are maven modules with pom packaging. They are used to aggregate multiple modules into one unit. You can use such a module as a dependency for your maven project. Maven will download the pom file, analyze the dependencies included in that pom file and download those & add it to your automatically.
Even modules that have jars (jar packaging) have a pom file associated with them. This pom file defines the other dependencies that are required for using it. Maven will automatically process and fetch those dependencies (transitive dependencies).
This makes specifying and managing dependency for any project. You will specify the top level modules that your projects directly depends on and other things required will automatically figured out and downloaded. It also makes it easier when you have upgrade to a new version - all the transitive dependencies will get upgraded automatically.
One of the reason that cause this is because of licensing issue.
License for such JARs prohibit public redistribution in such approach. So someone provide only the POM so that you can get the JAR yourself and install it to your local maven repo/ internal repo, together with the POM provided.

Maven beginner question, get m2eclipse to download jar and add to build path?

From what I have read, after adding the relevant maven repositories, maven should automatically download the necessary jars to satisfy dependencies in the pom.xml file.
However, no jars ever get downloaded for me after I add dependencies in eclipse. Am I missing some glaringly obvious step?
I'd recommend to start from creating your project with m2eclipse. See more details in this article.
Basically, you need to make sure the following:
your Eclipse project has a valid pom.xml and all dependencies are available (you should see errors on Maven console, in the Problems or Markers view or when opening pom.xml in m2eclipse's POM editor)
Maven support is enabled for this project (you can use Maven / Enable Dependency Management from popup menu on that project)
project configuration is in sync with pom.xml (you can use Maven / Update Project Configuration from the project popup menu)
you can also use Maven / Update Dependencies to refresh your dependencies (e.g. when you got them in your Local Maven repo from the command line)
Dependencies jars aren't in your project but in your local maven repository.
These jars will be automatically used when you compile you project with maven (or m2eclipse).
If you don't have the needed jar yet, maven will download it for you.

Resources