Triggering build of another project in maven - java

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.

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"

How is "mvn clean install" different from "mvn install"?

What is the difference between mvn clean install and mvn install?
clean is its own build lifecycle phase (which can be thought of as an action or task) in Maven. mvn clean install tells Maven to do the clean phase in each module before running the install phase for each module.
What this does is clear any compiled files you have, making sure that you're really compiling each module from scratch.
Maven lets you specify either goals or lifecycle phases on the command line (or both).
clean and install are two different phases of two different lifecycles, to which different plugin goals are bound (either per default or explicitly in your pom.xml)
The clean phase, per convention, is meant to make a build reproducible, i.e. it cleans up anything that was created by previous builds. In most cases it does that by calling clean:clean, which deletes the directory bound to ${project.build.directory} (usually called "target")
You can call more than one target goal with maven. mvn clean install calls clean first, then install. You have to clean manually, because clean is not a standard target goal and not executed automatically on every install.
clean removes the target folder - it deletes all class files, the java docs, the jars, reports and so on. If you don't clean, then maven will only "do what has to be done", like it won't compile classes when the corresponding source files haven't changed (in brief).
we call it target in ant and goal in maven
To stick with the Maven terms:
"clean" is a phase of the clean
lifecycle
"install" is a phase of the
default lifecycle
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference
Ditto for #Andreas_D, in addition if you say update Spring from 1 version to another in your project without doing a clean, you'll wind up with both in your artifact. Ran into this a lot when doing Flex development with Maven.

multi-module maven project

If I have 6 modules in my project is it possible to build only one out of six ? without commenting out others ?
EDIT
Submodule will not work itselft because or parent tags. I need to install the parent first to make it build. how can I do it without installing parent
is it possible to build only one out of six ? without commenting out others ?
My understanding is that you want to launch maven from the aggregating project (i.e. a reactor build) but only build one module. This is possible using the -pl, --projects project list option (see advanced reactor options):
mvn --projects my-submodule install
This is a very powerful option, especially when combined with --aslo-make (to also build the projects on which the listed modules depend) or the --also-make-dependents (to also build the projects that depends on the listed modules). On the basis of your update, you might want this actually:
mvn --projects my-submodule --also-make install
Launching Maven from the directory of the module you want to build is of course an option but this won't allow you to do the things mentioned above nor to build a subset of all modules. For such use cases, the advanced reactor options are the way to go.
Opening a command shell, navigating to the submodule directory and executing mvn install (or whatever your preferred lifecycle is) should do the trick.
You can simply build the module by going in this module directory and run the mvn clean install.
However, note that with this method, the dependencies with the others modules will be taken from your local repository (or the entreprise repository).
Let's take a simple example:
project
+ commons
+ business
Now, imagine that you build, on the root directory the whole project, using the mvn clean install command. Consider that all your modules are in version 1.0.
Now, you move to version 1.1. If you run the mvn clean install on the business project only, it will try to get the 1.1 of module commons. You will then have an error, as Maven will not find any version 1.1 in your local repository.
Within Eclipse, assuming you have m2eclipse installed: Right-click on the module in question and choose Run As -> Maven package.

Categories

Resources