situations where clean install is a must not just install? - java

I have a question which is pretty straight forward, but hard to find an exact answer.
When we are using mvn build, what are the exact situation where we must go for clean install in order to see latest code changes in our built artifact. i.e. just install would not do that for us.
Thanks in advance.

Basically
mvn clean install
is same as
mvn clean && mvn install
so as to answer your question, its required when cleaning the files and directories generated by Maven is the primary requirement prior to doing another install.
Related to the changes in code, I would assume this is more of cleaning the target folder generated by Maven, hence mostly the files compiled would be overwritten when there is not much of a change.
In cases where one has removed/refactored a class from the previous build. There might be references still left if you don't clean and that would get packaged into the jar created thereafter.
From the official documentation(formatting mine) shared by ernest,
But if we try to build the project with mvn install without any clean lifecycle, then we see 2 bugs
the maven build still succeeds to compile the project maven even
generates a jar which contains broken classes
moduleB does not get recompiled and is thus broken as well.
Also, a preferably suggested way is to instead use
mvn verify

Related

Maven - how to rebuild .xml files only on a Java EE project

As the title states.
I've made changes to .xml file only. Do I need to mvn clean install the whole project or a simple mvn install? Or maybe mvn generate-sources will do the stuff?
Can anyone elaborate?
To build the artifact, you need to call mvn package. If you do not do a clean, beware that remainders of the previous build might still exist. Changing the code of a class is generally fine, while changing the dependencies in the POM is not (you might end up with both versions of an artifact if you change the version in the POM).
Your hibernate case is somewhere in between, so you probably need to make experiments. Maven does not make any guarantees.

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.

Perform a maven plugin execution only when a file changed since last build

I want to execute a maven plugin during mvn clean install whenever a file is changed since the last build. If the file is not changed since the last build then plugin execution should be skipped during mvn clean install.
Is it possible to achieve this in maven 3.5.0?
Maven doesn't keep record of all modules it ever built. However, this would be necessary if Maven would have to know if some (source) files changed.
Some plugins, like the maven-compiler-plugin, compare timestamps of source-files with timestamps of corresponding, generated class-files, which allows to skip compilation if classfile is newer. However, if you execute mvn clean (as mentioned in the question), class files are removed and compilation thus has to be executed anyway.
So to conculde: your request cannot be fulfilled by maven without major changes in maven itself.

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"

why sonar:sonar needs mvn install before?

official documentation http://docs.sonarqube.org/display/SONAR/Analyzing+with+Maven says that the proper way of invoking sonar is:
mvn clean install -DskipTests=true
mvn sonar:sonar
but doesn't say why. how does sonar work? does it need compiled classes? so why not just mvn clean compile? or does it need a jar file? so why not just mvn clean package? what exactly does sonar plugin?
Explanation from a SonarSource team member:
In a multi-module build an aggregator plugin can't resolve dependencies from target folder. So you have two options:
mvn clean install && mvn sonar:sonar as two separate processes
mvn clean package sonar:sonar as a single reactor
I was surprised too, so I made a tweet an received the following answer from the official Maven account:
If the plugin is not designed to use the target/classes folder as a substitute, then yes you would need to have installed to get the jar when running *in a different session*. Complain to the plugin author if they force you to use install without foo reason [ed - #connolly_s]
The SonarQube analyzer indeed needs compiled classes (e.g for Findbugs rules, coverage). And since by default it executes tests itself, the compile phase can skip tests.
You can run SonarQube as part of a single Maven command if you meet some requirements:
As Mithfindel mentions, some SonarQube plugins need to analyze .class files. And if you run unit tests outside of SonarQube, then of course the testing plugins must read output from the test phase.
Got integration tests? Then you need to run after the integration-test phase.
If you want to run SonarQube as a true quality gate then you absolutely must run it before the deploy phase.
One solution is to just attach SonarQube to run after the package phase. Then you can get a full build with a simple clean install or clean deploy. Most people do not do this because SonarQube is time-consuming, but the incremental mode added in 4.0 and greatly improved in the upcoming 4.2 solves this.
As far as the official documentation goes, it's a lot easier to say "build and then run sonar:sonar" then it is to say, "open your POM, add a build element for the sonar-maven-plugin, attach it to verify, etc".
One caveat. SonarQube requires Java 6, so if you're building against JDK 1.5 (still common in large organizations), the analysis will have to happen in a separate Maven invocation with a newer JDK selected. We solved this issue with custom Maven build wrapper.

Categories

Resources