Sonar missing dependencies between Maven modules - java

I've searched through several questions related to multi-module maven projects and sonar, but those were mostly about aggregating code coverage metrics.
I have a multi-module maven project being analyzed by sonar.
If I run the analysis straight from the terminal using mvn sonar:sonar the resulting analysis will display the dependencies between the maven modules in the Design page.
However, if I run the analysis by invoking sonar-runner via Jenkins the resulting analysis will have missed the dependencies between the maven modules.
Here's a snippet of the top-level pom where I set sonar parameters:
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>
jdbc:mysql://myhost:3306/sonar?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<sonar.host.url>http://myhost:9999</sonar.host.url>
<sonar.java.source>1.7</sonar.java.source>
<sonar.login>jenkins</sonar.login>
<sonar.password>jenkins</sonar.password>
</properties>
</profile>
On Jenkins I configured the job to do a clean install -DskipTests=true in the build step followed by a post-build action to run Sonar.
I'm using Maven version 3.0.5, SonarQube version 3.7, Sonar Runner 2.3 and Java 1.7.0_45.
Any idea of what can I be missing in my configuration?

Indeed the SonarQube Maven bootstrapper relies on Maven to get the dependencies (either on modules or on external libraries). But SonarRunner doesn't have access to such kind of information and that's why when you analyse a project with SonarRunner you don't get any information about external dependencies.

Ok if you are using Jenkins, you need to install Sonar Plugin and set parameters. Then, you need to create a job and run maven target:
clean install
After that, mark Sonar checkbok. Run your job and you should see your code coverage.
PS: If you are using module you must have pom-root with all your modules.

Related

How can i do a gradle publish on teamcity to publish a gradle build that uses the maven plugin?

I have teamcity currently configured to use the maven mojo, to publish the gradle jar as a nexus snapshot with just the gav.
I observe that if i use the maven plugin and do a gradle install in the IDE, i am able to see the generated pom.
1) Can i use this pom to publish the jar in nexus repo in teamcity ? I know that i can do it for a pure maven build by using it's pom.
2) Is there a way to not use this pom, and istead configure teamcity build steps to publish from gradle build directly ?
Gradle can of course take care of the publication. It will leverage the build information to produce a POM file that represents best what is declared in your project.
It will then be trivial to invoke that Gradle task from the Teamcity build.
Have a look at the publishing documentation for details on how to set it up.

Does maven sonarqube plugin reads failsafe reports?

I am using 3.6 maven sonar plugin . While seeing with mvn sonar:sonar -X , I did not see sonar searching for failsafe report. (Finding it or not finding it is different thing )
According to this doc , I think it cannot be configured for failsafe reports for integration test.
Does mvn sonar:sonar reads failsafe reports ? If yes , how ?
I have mentioned these properties in my POM:
<sonar.jacoco.reportPath>/Users/xxx/site-order/service/target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.jacoco.reportPaths>/Users/xxx/site-order/service/target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.itReportsPath>/Users/xxx/site-order/service/target/failsafe-reports</sonar.junit.itReportsPath>
I asked the same question on sonarqube forum and I got the reply that there is no such direct support in sonarqube maven plugin. Also, look here.

Eclipse - Maven dependency is being ignored

I have a problem with adding maven dependencies to eclipse.
What should be OK:
pom.xml already contains all the dependencies and compilation and running tests using maven finished with success
all the source folders were recognized by eclipse
eclipse has Maven plugin and project is managed as maven project (see figure below)
pom.xml has maven-compiler-plugin, so the eclipse should know it is a maven project
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
Issue:
eclipse does not resolve dependencies defined in pom.xml (guava, juint and other libraries are in pom.xml but not in eclipse class path)
any right click > Maven > Update project has no any effect (see figure below)
I noticed that libraries does not contain any Maven Managed Dependencies - by adding as described below has no any effect
My attempt to add Maven Managed Dependencies to Eclipse manually
1) Add library
1)
This attempt has no any effect and "Java Library Path" remains the same even after this action.
2)
Command
mvn eclipse:eclipse
Fails with
[ERROR] Failed to execute goal on project pmml-model: Could not resolve dependencies for project org.jpmml:pmml-model:jar:1.3-SNAPSHOT: The following artifacts could not be resolved: org.jpmml:pmml-agent:jar:1.3-SNAPSHOT, org.jpmml:pmml-schema:jar:1.3-SNAPSHOT: Could not find artifact org.jpmml:pmml-agent:jar:1.3-SNAPSHOT in sonatype-nexus-snapshots (https://oss.sonatype.org/content/repositories/snapshots) -> [Help 1]
The jar does not exist in the desired version on the server your maven script is referencing. The most recent version is 1.2.9. The snapshot version is not present. You should reference
<dependency>
<groupId>org.jpmml</groupId>
<artifactId>pmml-model</artifactId>
<version>1.2.9</version>
</dependency>
in your pom.xml.
The below jars are not present in your remote repo.
org.jpmml:pmml-agent:jar:1.3-SNAPSHOT,
org.jpmml:pmml-schema:jar:1.3-SNAPSHOT
Open the url https://oss.sonatype.org/content/repositories/snapshots in your browser to see the available snapshots/
Apparently, you are trying to set up a project that makes use of the JPMML-Evaluator library. In that case, you should only depend on the latest stable org.jpmml:pmml-evaluator dependency (which is version 1.2.13 at the moment). In other words, don't try to manage the associated org.jpmml:pmml-model, org.jpmml:pmml-schema dependencies manually.
You could base your work on the JPMML-Evaluator-Bootstrap project instead.

How to trigger mvn install during package of a different project?

I have a "commons" library that I install to my local repo using mvn install. That library provides common company stuff used in different, not connected projects.
So I include that library as a maven dependency. And when I run mvn package on the final projects, there have been days where I forgot that I have to explicit install the commons library to get the changes picked up during package of the implementation projects.
Is there any chance I could trigger/invoke install of a different project during package?
You will have to use Antrun maven plugin, and execute shell command using ant while running properr lifecycles of maven build.
Here you have some details on configuration
http://maven.apache.org/plugins/maven-antrun-plugin/
If repository manager or external scripting (Ant) is not an option you could tweak your project structure. You could introduce an aggregator maven project which will run modules in a predefined order:
<packaging>pom</packaging>
...
<modules>
<module>[path to commons project]/commons</module>
<module>[path to your module project]/module</module>
</modules>
So whenever you want to build your project you build this aggregator project instead.

Is there a way to configure the version of the Maven POM from command line?

Is there a way to change the version number without editing the POM?
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0</version>
We have a CI system where we want to release nightly builds, but without using the -SNAPSHOT solution of Maven, so if 1.0.0 is the current version, we just want to have CI-NIGHTLY-BIULD-20120426.
I suggested this would be possible with something like mvn deploy -Dversion=CI-NIGHTLY-BIULD-20120426, but obviously not. The bad solution would be to let the CI server edit the pom.xml every time, but I think this is very unhandy.
Thank you!
I suggest to use classifier.
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
<properties>
<!-- default classifier is empty -->
<my.project.classifier></my.project.classifier>
</properties>
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<classifier>${my.project.classifier}</classifier>
</configuration>
<executions>...</executions>
</plugin>
</plugins>
</build>
and
mvn package -Dmy.project.classifier=NIGHTLY-2012-04-26_02-30
Maven documentation says about classifier:
classifier: You may occasionally find a fifth element on the
coordinate, and that is the classifier. We will visit the classifier
later, but for now it suffices to know that those kinds of projects
are displayed as groupId:artifactId:packaging:classifier:version.
and
The classifier allows to distinguish artifacts that were built from
the same POM but differ in their content. It is some optional and
arbitrary string that - if present - is appended to the artifact name
just after the version number. As a motivation for this element,
consider for example a project that offers an artifact targeting JRE
1.5 but at the same time also an artifact that still supports JRE 1.4. The first artifact could be equipped with the classifier jdk15 and the
second one with jdk14 such that clients can choose which one to use.
Another common use case for classifiers is the need to attach
secondary artifacts to the project's main artifact. If you browse the
Maven central repository, you will notice that the classifiers sources
and javadoc are used to deploy the project source code and API docs
along with the packaged class files.
I think you could also use versions maven plugin. I find it quite useful for things like this.
You could do it in 2 steps:
set necessary version: mvn versions:set -DnewVersion=CI-NIGHTLY-BIULD-20120426
deploy: mvn deploy
in case you need to revert back the changes, use mvn versions:revert (as Mark suggests)
I highly recommend reading Maven Releases on Steroids (part 2, part 3) by Axel Fontaine. It is great, and I'm quite happy using it.
It not only details how you con do what you ask, but also contains good advice how you can tie your build versions with your CI server.
In a nutshell, here are the main points:
Maven Release is slow, needs to be done faster
You parametarize your project version like
<version>${VERSION_NUMBER}</version>
...
<properties>
...
<VERSION_NUMBER>1.0-SNAPSHOT</VERSION_NUMBER>
...
</properties>
Local builds get that version: 1.0-SNAPSHOT
Release builds are done only from your CI server
In your Jenkins/Hudson project configuration you use
clean deploy scm:tag -DVERSION_NUMBER=${BUILD_NUMBER}
That way you get a new release with each Jenkins build, not only nightly.
You can change the configuration to use
clean deploy scm:tag -DVERSION_NUMBER=1.0.0-CI-NIGHTLY-BIULD-${BUILD_ID}
and you would get versions like 1.0.0-CI-NIGHTLY-BIULD-2012-04-26_12-20-24
You could parameterize the version number as
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>${my.project.version}</version>
<properties>
<my.project.version>1.0</my.project.version>
</properties>
and drive the version number from command line using
mvn package -Dmy.project.version=NIGHTLY
Although this is possible, Maven 3 discourages it.

Categories