Run sonar ant task using the command line - ClassDefNotFoundError - ant

I'm trying to integrate custom PMD tasks in Sonar, which I currently managed to work along with Sonar PMD plugin.
My current issue is to run sonar ant task with the command line.
In my eclipse workspace, I add additional classpath entries in Eclipse for Ant to run and it works just fine.
But when i run it in the command line, no matter how I pass the jars for ant, it just doesn't seem to be using it in the ClassLoader.
This is a big issue for my project, once I can't put the task to run in my continuous integration server.
I'm currently passing the jar's that contains all the classes need using the property sonar.libraries.
The error I'm getting is:
build.xml:121: java.lang.NoClassDefFoundError: net/sourceforge/pmd/AbstractJavaRule
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)

"sonar.libraries" references only dependencies of your application, not dependencies of Sonar execution context.
If you want to develop custom PMD tasks that will be used by Sonar, please have a look at this example plugin: https://github.com/SonarSource/sonar-examples/tree/master/plugins/sonar-pmd-extension-plugin
You just have to build the JAR of the plugin
Put it in your "/extensions/plugins" folder
And restart Sonar

Related

GRAILS --- what's the difference between commands grails war and gradle build?

I've heard you should type command
grails war
to build your project. I've thought to this point that Gradle is responsible for building the app in Grails. I've been doing the latter with conviction that my app is built. So what's the difference between
grails war
and
gradle build
?
Is it just that grails war is gradle build + create the war file?
It is not that simple to compare Grails and Gradle. Gradle is a build tool, while Grails is a web application framework.
However, Grails provides a command line tool, that's described in the docs:
Grails incorporates the powerful build system Gant, which is a Groovy wrapper around Apache Ant.
So, Grails does not use Gradle.
The basic usage of the grails command looks the following:
grails [environment]* [command name]
Where especially the command name parameter must be one out of predefined values. You can find the documentation on the war command here.
The basic usage of the gradle command looks the following:
gradle [option...] [task...]
The listed task parameters can be names of tasks created either in the build.gradle script or by plugins. All mentioned tasks and their respective task dependencies will be executed. If you use the Gradle War Plugin, it will generate a war task, which will also (transitively) be added as a task dependency of the build task. So whenever you call gradle build, a WAR file will be created. You can also call this task directly via gradle war.
EDIT
I just learned that Grails can or even does use Gradle beginning at a certain version. I even found a list on which Grails command calls which Gradle task. According to this list, calling grails war is equivalent to calling gradle assemble. The assemble task directly depends on the war task.
gradle build is a Gradle lifecycle task which usually consists of other tasks required to build a software like compileJava and other lifecycle tasks like assemble and check.
In case of Grails it delegates build to Gradle and to war task and it doesn't include check lifecycle during which unit tests will be executed.

How do I analyze Groovy code with Sonarlint for IntelliJ?

The Sonarlint plugin for IntelliJ does analysis on our Java code, but it won't analyze Groovy code in the same project.
We have a Grails project which works with sonar-runner on the command line. When I install the IntelliJ IDEA Sonarlint plugin, the plugin doesn't seem to pick up the settings from our command line config, and it doesn't show the warning highlights for issues like it does for the Java files in the editor.
Our project uses the "code-coverage" Grails plugin to generate an XML coverage report, then the sonar runner (command line tool) pulls in the metrics. In sonar-project.properties, we set:
# Project key within sonar
sonar.projectKey=com.mycompany:myapp
sonar.projectName=My Grails Project
sonar.projectVersion=1.0
# Source directories to analyze
sonar.sources=src/groovy, grails-app/domain, grails-app/services, grails-app/controllers, grails-app/utils, grails-app/taglib, grails-app/views
# Set language for the project to be Groovy
sonar.language=grvy
# Path to the Cobertura XML report
sonar.grvy.coveragePlugin=cobertura
sonar.groovy.cobertura.reportPath=target/test-reports/cobertura/coverage.xml
sonar.jdbc.username=...
sonar.jdbc.password=...
sonar.host.url=http://sonar.mycompany.com
sonar.jdbc.url=...
sonar.jdbc.driverClassName=...
The properties file has JDBC settings, the coverage report path, etc.
SonarLint is a different product than SonarQube (even if there are some links). For example SonarLint doesn't know what is sonar-project.properties. The purpose is to integrate with IntelliJ, not with some external configuration.
Regarding Groovy support, this is not planned since we don't have any Groovy analyzer (the SonarQube plugin for Groovy is simply reusing codenarc reports AFAIK). If you want to run Codenarc in IntelliJ, there is already a dedicated plugin for that.

Reusing several report files in jenkins sonar plugin

I'm generating several XML report files via ant task (checkstyle, findbugs, macker, jacoco, ncss, classycle, etc.).
I want the to reuse the generated files in jenkins sonar plugin. Is there any way to do so?
I'm planning in setting the properties in the project specific sonar-project.properties file. But I can't find any of these options at codehaus documentation site.
There's no point to reuse:
checkstyle or findbugs reports as SonarQube also executes those tools
ncss or classycle reports as it is SonarQube very job to compute those metrics
For JaCoCo, you can reuse reports if you don't want SonarQube to execute the unit tests again. See http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Unit+Tests+for+Java+Project.

is sonar multi-module broken?

trying to make a multi module project I dowloaded the samples in github: I use this folder
https://github.com/SonarSource/sonar-examples/tree/master/projects/multi-module/sonar-runner/java-sonar-runner-modules-own-configuration-file as a project base dir
In a command line in this folder, I type /opt/sonar-runner/bin/sonnar-runner
First thing I find is that sonar-project.properties has a property named sonar.sources=src, but executing throws
Exception in thread "main" org.sonar.runner.RunnerException: You must define mandatory properties: sources
Then I correct this property with sources=src and runner execution finish, adds the project to my sonar server, but no code, modules or file is detected. It is like if all project is empty.
It is exactly the same with other examples and with my own project. No matter if is java, python...
Any help is welcome
You are using an old version of Sonar Runner and/or Sonar. Please update to the latest versions (Sonar Runner 2.2 and Sonar 3.5.1).

Sonar plugin not working for projects that use ANT as a build script

Problem
I have just installed the Sonar Jenkins plugin. I went into my configured job (a free style job) that produces a WAR file artifact through an ANT build and did as follows:
Check the Sonar checkbox. (No problems here)
Configure the install dir of sonar (No problems here)
Checked the checkbox that states: "Check if this project is NOT built with maven 2" (I am confused here)
I have checked that box because I am not using maven for build, I am using ANT but it still asked me for required properties that resemble a lot MVN such as: Organization id, project id, project name, project version, source directories... etc..
So I have filled those as well. When I click the play button "Build Now" the build seems to be running fine as it always had prior to sonar installation but it fails at the very end because its trying to execute MAVEN.
See as follows:
$ mvn -f /root/.jenkins/jobs/HRDA/workspace/pom.xml -e -B sonar:sonar
FATAL: command execution failed
java.io.IOException: Cannot run program "mvn" (in directory "/root/.jenkins/jobs/HRDA/workspace"): java.io.IOException: error=2, No such file or directory
Questions
Why is Sonar trying to execute Maven if I have checked the box that said check this box if you do NOT use Maven 2?
Can I make use of this Sonar plugin if my apps are built with ANT, GANT, GRADLE?
Do I have to reconvert my apps to use MVN builds?
Thanks,
- Dario
Like Oers suggested on my question comments if you are using ANT as a build script in your CI server (Jenkins in my case) you will have to use the SONAR-ANT-TASK to generate Sonar reports, do as follows:
Download MySQL or any other Sonar supported RDBMS such as Postgres, Oracle, etc..'
Download and Install sonar server.
go to (sonar installation folder)/extras/database/mysql and run the create_database.sql script.
I had to run an extra sql statement in my case using mysql, you can see here Unable to access Sonar MySQL database Caused by: java.sql.SQLException: Access denied for user 'sonar'#'glassfishdev.ccs.local' (using password: YES)
Start Sonar by typing ./sonar.sh start
Add the sonar ant task to your build script. You can follow this template http://docs.codehaus.org/display/SONAR/Analyse+with+Ant+Task+1.0
DO NOT check the sonar check box in your Configure screen if you have the Hudson Sonar plugin for installed, as this plugin only works with Maven projects.
Click the "Build Now" button. If everything above has been done correctly you should be able to see the reports at http://ipaddressofmachinesonarisinstalled:9000/
Hope this helps,
-Dario

Resources