Issue while adding a new maven module to a maven project - java

I've added a new maven module B to an existing maven project A.
Apart from module B, A contains additional 2 modules C & D.
I've properly defined the module B inside parent pom.xml.
I've also added the parent tag inside module B pom.xml. The versions of all the modules as the parent are 0.0.X-SNAPSHOT.
Now when I do a mvn clean install deploy, it always throws exception:
Failed to create archive: Could not find artifact B:jar:0.0.X-SNAPSHOT in central (https://nexus.xyz.com/content/groups/public) ,which kind-off makes sense.
Hence,as a last resort, I tried to manually push the file into the nexus repo using using mvn deploy:deploy-file command.
Now, if I again do mvn clean install deploy,it works smoothly.
But the problem re-appears when I try to release the project.
This time, it complains:
Failed to create archive: Could not find artifact B:jar:0.0.X in central (https://nexus.xyz.com/content/groups/public)
I tried with mvn clean install -U as well but in vain.
The problem doesn't appear for any other modules.
Kindly advise.Thanks.

Okay...after fiddling around with all sort of permutations and combination, I finally arrived at the conclusion.
Those who says that the reactor automatically sorts and resolves all dependencies on its own are incorrect.
It seems that the link below describes it all. Kindly look at the last point under "Reactor Sorting" section:
https://maven.apache.org/guides/mini/guide-multiple-modules.html#Reactor_Sorting
It suggests that the order in which the modules are defined are taken into consideration when no other rules applies.
I hope that helps other. :)

Related

A class is looking for methods of another class in the maven repository instead of the source code

I have two (really more, but we are interested in these two now) projects in the Eclipse workspace. One of them has got a class A that just obtained several new static methods.
I want another one class B, in another project, to call these methods. But I see these methods underlined by red, with message "The method ... is undefined for the type A". If I go into "open implementation" for the class A, Eclipse tries to open the source code attached to the old jar that lies in the maven repository. No source is attached, so I see only the tab with the name of the A class (it is there), but that is the old class version, without new methods.
Clean, Maven Clean - don't help. The same with restart Eclipse, clean all projects, reopening projects.
Please, don't propose to reinstall Eclipse - what I have is the picture JUST after reinstalling of everything, updating the project, maven installing it (worked), and adding these functions.
A Maven Install does not work for the same reason - falling on these references to new methods.
If I clean the repository by hand - on Maven Clean I am getting a FATAL error, with a demand for that old jar.
I have put the reference to the correct source of the A class to the Maven task source. I have added the reference to the correct project of the A class to the project of the B class in the Eclipse Properties of the project. Nothing changed.
There are similar problems here on SO, but I managed to get the some multiplication of all of them: both Eclipse and Maven had gone mad, no added references help, no cleaning helps.
Edit.
I have tried to make maven to create new snapshots, but the result is absolutely the same picture... "cannot find symbol" that is in the source! What is interesting, after removing of the repository the Eclipse starts to see the symbols OK. But I must run maven and it won't run with removed repository. Just as a crazy idea: Is there some maven tool to clean the repository so that it will know there is nothing there and won't try to look for things in it?
I have met with three unpleasant situations combined:
The local maven repository was damaged
The jar written in it was deprecated.
Due to some committing to local CVS branch and pulling and checkout from the common CVS repository it happened that parent snapshot referenced to by some poms and the main snapshot declared in the root pom had different versions.
Solution
The first problem can not be normally corrected using the plugin maven only. You have to install the standalone maven, add its bin to PATH, an launch in folder of your root pom (exit the Eclipse at first and stop jar that you are repairing, if you have it running):
mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false
Then, if you want to work in standalone maven, you should put the correct settings into youruserroot/.m2 folder.
After that,
mvn -U -X -e install
That will fail on every local pom with incorrect parent Snapshot. Go into every local lesser poms and correct the references. Repeat, until it stops to fail on snapshots.
I had found advice to run
mvn -U -X -e clean install -DskipTests=true
for installing all, but without test running. (I had repaired an error in the line, so don't try to google for it). But you can already use the plugin Maven at this stage, too.
So, you run Maven clean and Maven Install in the plugin... And hurra! - the classes see other classes as they are in the source code and not in some old jar, and that works in both maven and Eclipse.

Maven - See dependency tree without building project

I want to see the dependency tree of a project without actually downloading those dependencies.
I have a project whose build fails because there is some dependency which is not present in central repo, however it is not a direct dependency and I am not aware which one of my dependencies refers to it.
Now when I run mvn dependency:tree command, it builds the project and hence fails.
One way to do it is keep a dummy jar in local repo with the same name. It will not try to download the dependency and generate the entire tree. However is there any other way to do this ?
If you are using eclipse there is a "Maven POM Editor", which shows not only the maven XML, but also a dependency hierarchy view.
A working build is not necessary for it, just a correct POM XML file.
It should get installed, when you install the eclipse m2e plugin.
The update site is http://download.eclipse.org/technology/m2e/releases.
Maybe Maven dependency:analyze-only
mvn dependency:analyze-only
Analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared. This goal is intended to be used in the build lifecycle, thus it assumes that the test-compile phase has been executed - use the dependency:analyze goal instead when running standalone.
or dependency:resolve:
mvn dependency:resolve
mvn dependency:tree -DoutputFile=tree.txt

Does Maven automatically build other modules?

I couldn't really find an answer anywhere to this particular question.
I have a (Maven) project consisting of multiple modules, let's say a core module (a jar) and a webapp module (a war).
When I run mvn clean package on my webapp, does it automatically always build the core first and will it pick up any changes in it? Do I have to run mvn clean install instead? Or do I have to run mvn clean package/install on my parent pom?
Does it matter if the parent/module is a release or a snapshot?
If you are working with a so called Multi Module Build you should do everything what you like to do with your whole project from the parent level.
There you can do:
mvn clean package
than it will build all modules in the correct order (assuming you have defined the dependencies between them correct).
If you have such a multi module build all your modules incl. your parent should have the same version number. If you like to make a release of the whole you can simply start from the parent.
You have to build anything that's changed, upwards. So, if you change core, then rebuild core, then rebuild your war. If you've just changed your war, then you only need to rebuild your war. Cleaning is generally good practice. The reason not to do it would be if you're generating a bunch of entity classes, which takes a long time to redo.
I've seen a lot of mistakes because people forget to clean, and then some old piece of code is still active, even though they thought they'd deleted it.
Installing will put your latest build into your M2 repo, which is generally a good idea too. You really can't go wrong with "mvn clean install"

Triggering build of another project in maven

I just starting using maven in my new project.
I am trying to create artifacts(java files) of a project A into another project B in order to resolve their cyclic dependency.
If I run the whole build for the first time,its working fine. The jar of B contains classes of both project A and B.
However, if I make changes to only project B , and run the build, only project B is running and build is failing . Thats because sine no changes are done to project A, maven is not running it and the artifacts are also not getting generated.
Can anyone advise how can I trigger the build of a project even though no changes are done to it.
exec mvn with clean phase! i.e.
mvn clean package
Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war if the project type is - you guessed it - a WAR.
An interesting thing to note is that phases and goals may be executed in sequence.
mvn clean dependency:copy-dependencies package
This command will clean the project, copy dependencies, and package the project
mvn site
This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site.

Maven can't find parent POM

I am trying to build a maven project, but I have encountered problem.
pom has specified parent pom and maven can't find it. I actually have the parent pom, but I don't know where to place it or what should I do so maven knows about.
I'm new to maven so sorry if my question is stupid.
If you have the parent pom, you could try installing it.
When you run mvn install on a module the resulting artifact will be placed in your repository, so that it can be used from other modules.
I am using Netbeans, I ran -N versions:update-child-modules in Run Maven/Goals.
It worked! It took 5 minutes to build but, it did build the project, and that's the first time that it has.
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html Example 1 and Example 2 are for Inheritance that can be usefull.
This happens when the different POM in the project. They do not have the same version.
For example if you have:
MyProject / EAR ..... MyProject / WEB .....
you need to have the same version in three POM
Same error for me.I resolved by updating the project with a new POM file(someone form my team accidently deleted it).

Categories

Resources