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.
Related
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
I have a large project that I am working on. I recently checked out our evolution branch, did a git pull and tried to deploy the app locally. It doesn't seem to recognize some libraries or jars in one Java class, so subsequently errors halt me from running. Basically, the import statements go unrecognized in the class.
Turns out I forgot to rebuild maven. When I ran mvn clean install from the command prompt, the build fails (even when I do mvn clean install -fn) as there are tests that fail. I don't often work with maven, or the command line, but here is my full stack trace when I run mvn clean install -e:
I'm running my project in the IntelliJ environment.
When I ran mvn clean install -fn, 'talent-app' was successful, but talent-core still failed and I still got
[INFO] BUILD FAILURE
Please let me know if you have any input, I appreciate it!
I'm not sure I understand your question correctly.
Basically, regarding your first paragraph, you said you had library issues but that after a clean rebuild - of your project, I suppose not of Maven itself - everything is fine?
Regarding the rest of your post, your build is failing because of a failing test case. This is shown by the line:
talent-core ............................... FAILURE
and by the output:
Failed to execute goal [...]. There are test failures.
If you go into the target/surefire-reports folder, you will find some files containing the output and error traces of each test, including the one that failed.
By scrolling up in your terminal, you should also be able to see which test was failing for talent-core.
From then on, by order of preference:
either look at the test reports as mentioned in the output, and attempt to figure our why the test is failing, and either fix the test or the code;
or skip the tests, you can add -DskipTests to the command-line. But you shouldn't skip your tests, really.
I have a multi-module maven project. In every module there are unit tests. When I make clean install tests run before every module and if all tests in one module are success it build successfully. If one test failure all other tests in that module run successfully (or some run successfully, other failed). The build of module in what the first failure unit test is placed failed. Other modules are skipped.
I want such thing: first to run all unit tests in all modules, and after that if there is no failed tests build all modules, or if there is one or more failed tests in one or mode modules skip building of all modules. Can you help me with it?
run:
mvn clean test
mvn install -Dmaven.test.skip=true
note, if you have inter-module dependencies (which i assume you do), you probably can't really do this, as you will need to build the dependent jars before you can run the tests in the other module.
AFAIK its impossible in maven. You are trying to change a maven build lifecycle which is not allowed in maven. However there are a couple of configuration parameters you can pass to maven and this will affect the testing.
mvn install -Dmaven.test.skip
This won't run unit tests at all
mvn install -Dmaven.test.failure.ignore=true
This will cause maven to not stop and proceed the module building process even if there were failures during the test phase.
Hope, this helps
The problem is:
the modules probably have dependencies upon each other, and to resolve those dependencies, you have to build the modules in order, or they won't compile. So there's no sane solution to your problem.
Insane solutions would somehow aggregate the sources (and external dependencies) from all child projects and run compile and test on that conglomerate, but it would be such a monstrous hack that I'm glad they didn't do it.
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.
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.