Execute analysis in SonarQube from Maven but not storing the results in database - maven

I am developing a new project in Eclipse and I have to analyze it with SonarQube. I am doing a lot of changes in the code and analysing it, but I wouldn't like that SonarQube store all the results in it's database (they are temporary versions). Is there any option for it?
I have configured my pom file like sonarQube official config:
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
It connects with sonnar and execute the analysis ok, but I am storing lots of useless executions in SonarQube.
Regards

You might get some value from the SonarLint Eclipse plugin: https://www.sonarlint.org/eclipse .
With this, you might be able to iterate on local changes before submitting a full sonarqube scan.

Related

Questions about pom.xml in Jenkins to run sonarQube through maven project

I'm trying to run sonarQube through Jenkins but I have some difficulties right now. When I build a new job, I use Maven Project and inside the configuration I have to give à pom.xml path but what does it correspond to ?
Thank you in advance
You should find in any jenkins job a post action for sonarqube analyse.
The pom.xml you mention is the pom.xml for your maven project, because sometimes you can put your parent pom.xml in a subdirectory and this is the way for helping jenkins to find it.
Instead of adding Sonar Task to each project why not just configure Sonar at Global Level configuring the settings.xml for your maven configuration, just go to $HOME/someUser/.m2/settings.xml (if you don't have it created yet) with this content:
<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
After you you have done that you will be able to run sonar in all the projects this way:
mvn clean verify sonar:sonar
 
# In some situation you may want to run sonar:sonar goal as a dedicated step. Be sure to use install as first step for multi-module projects
mvn clean install
mvn sonar:sonar
 
# Specify the version of sonar-maven-plugin instead of using the latest. See also 'How to Fix Version of Maven Plugin' below.
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar
You may find more information in sonar official documentation:
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven

Maven deploy snapshot or release based off of profile

Is there a way to append the -SNAPSHOT suffix to the version of a project based on profile? I'd like the prod profile to be the only one able to deploy RELEASES to nexus.
We can use user-defined properties to accomplish this:
<profile>
<id>local</id>
<properties>
<env>local</env>
<snapshot>-SNAPSHOT</snapshot>
</properties>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
<snapshot></snapshot>
</properties>
</profile>
Then update the version to refer to our new property:
<version>0.1.0${snapshot}</version>
by configuring different profile do SNAPSHOT build or a RELEASE build is not a good idea, here you are going to make both of them to have same source, (i.e. the source in development would be in release and vice versa)
you should keep only one version at a time, RELEASE it once and increment to the next SNAPSHOT by using mvn release plugin
They better way to acomplish this is using the maven-release-plugin this will versioning your project and modules and prepare it to the release version saving you from the tedious work.

always download sources (and javadocs) from maven ant task

I am trying to get this ant-based project's init target to download all the sources and javadocs.
I added the following to my ~/.m2/settings.xml (as per Maven – Always download sources and javadocs) but it doesn't force source downloads when used from ant:
<profiles>
<profile>
<id>downloadSources</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<downloadSources>true</downloadSources>
</properties>
</profile>
</profiles>
The only way I could get the sources to download was by hacking build.xml so that all <artifact:dependencies> elements include sourcesFilesetId="sources.dependency.fileset", but this is a pretty distasteful commit that is unlikely to be accepted by the maintainers. A better solution would exist with a property file definition, preferably in the user's settings (not something that mutates the project definition)
Is there a simpler way to ensure that all the sources (and potentially javadocs) are globally downloaded in maven ant tasks?

Enable certain Maven tests by passing a command line switch

I have a single module project that has some unit tests that require an external hardware device. I don't want these tests to execute unless I indicate that the device is available.
I feel like this is solvable using Maven properties and the SureFire exclusion/inclusion configuration, but I can't quite see how to do it. A similar question shows how to disable/enable all the tests in a project based on a Maven property, but doesn't quite answer my issue.
In summary, I wish to identify a pattern (e.g. **/*ResourceTest.java) that describes the tests I don't want to run, unless I pass a Maven property to enable them.
E.g.
mvn clean install (runs the standard tests, but skips device-related tests)
mvn -Drun.device.tests=true clean install (runs all the tests)
Thanks in advance.
(Edited to remove the misleading usage of the word "resource" > replaced with "hardware device").
You also can just use the JUnit Assume methods to decide (inside the test) if a test should be executed or skipped.
The best option IMHO would however be to 'declare' the device dependend tests to be "integration tests" and let them be executed by the Maven Failsafe Plugin. I think this would be the "build in" maven solution without any profile 'magic'.
The link you provided gave the good answer.
The right way
Use a mix of Profile Management and Surefire Configuration inclusion / exlcusion is the right way.
You should ask yourself WHY you want to activate some tests dependings on a resource. The resource should always been in your classpath.
If not, you probably just want to activate some test manually, for some tricky reasons. In that case consider this is a bad use of Maven (how would you automate that on a distant server for instance ?)
What you asked
If you really really want to do that, because you have some good reasons that we are not aware of, simply use this :
This example will trigger the profile when the generated file target/generated-sources/axistools/wsdl2java/org/apache/maven is missing.
Example from Maven official doc : http://maven.apache.org/guides/introduction/introduction-to-profiles.html
<profiles>
<profile>
<activation>
<file>
<missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
</file>
</activation>
...
</profile>
</profiles>
As of Maven 2.0.9, the tags and could be interpolated. Supported variables are system properties like ${user.home} and environment variables like ${env.HOME}. Please note that properties and values defined in the POM itself are not available for interpolation here, e.g. the above example activator cannot use ${project.build.directory} but needs to hard-code the path target.
You could find more information here : http://www.sonatype.com/books/mvnref-book/reference/profiles-sect-activation.html
Hope that will help.
Don't hesitate to challenge my point of view with you own reasons (even legacy code ;) ) or experience
To expand on #Jean-Rémy answer, I have done the following in my project POM file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<excludes>
<exclude>${tests.to.skip}</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!-- This profile will be used when running tests without a device -->
<id>default-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<tests.to.skip>**/*DeviceTest.java</tests.to.skip>
</properties>
</profile>
<profile>
<id>device-profile</id>
<activation>
<property>
<name>device</name>
<value>true</value>
</property>
</activation>
<properties>
<!-- Unsure how to match nothing -->
<tests.to.skip>NOTHING</tests.to.skip>
</properties>
</profile>
This creates two profiles, the default profile will exclude the device tests, whereas the "device-profile" will execute all tests.
To execute the device profile, one can execute mvn -Ddevice=true test.

maven connecting to Sonar

I have maven installed on my local machine and I'm trying to test out Sonar installed on a remote box.
I found a few post online to configure settings.xml (maven\config\settings.xml) and append a profile entry...which I did but does not work
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- SERVER ON A REMOTE HOST -->
<sonar.host.url>http://remotebox:9000</sonar.host.url>
</properties>
</profile>
What is the cli way? I tried several options but nothing worked.
I tried: mvn sonar:sonar http://remotebox:9000
What is the correct syntax?
Thanks in advance.
Damian
PS. this works fine on the remote box where both maven and sonar are installed...i just want to try it my box to the remote box.
Running sonar with
mvn sonar:sonar -Dsonar.jdbc.url=jdbc:h2:tcp://ipaddr:9092/sonar -Dsonar.host.url=http://ipaddr:9000
,where ipaddr is your remote host, seems to work.
Up to Version 5.2 beside the sonar.host.url you also have to specify the database parameters as described here. That way it works for me.
Configuration example
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- EXAMPLE FOR MYSQL -->
<sonar.jdbc.url>
jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<!-- optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
Since Version 5.2 this not not necessary anymore:
Quote:
Scanners don't access the database
This is the biggest change of this new version: scanners (e.g. Sonar Runner) no longer access the database, they only use web services to communicate with the server. In practice, this means that the JDBC connection properties can now be removed from analysis CI jobs:
sonar.jdbc.url,
sonar.jdbc.username,
sonar.jdbc.password
Just the following works for sonar-maven-plugin:3.2:
mvn sonar:sonar -Dsonar.host.url=http://<sonarqubeserver>:<port>
Problem 1
As explained you need to specify the JDBC connection details, otherwise Sonar will attempt to talk to the embedded Derby instance, it assumes is running on localhost.
Problem 2
Are you using Derby? Well, the default configuration of Derby does not accept remote connections, but only connections from the same host.
The SONAR-1039 issue explains how to work-around this problem, but my advise would be to setup a full-blown database such as MySQL or Postgresql.
I had some problem and then realized, I was running mavn on parent pom whereas sonar configuration was in a child pom, for which I wanted analysis report anyway, so running mvn sonar:sonar with child pom worked.

Resources