ant sonar not working - sonarqube

<target name="sonar">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
</taskdef>
<sonar:sonar />
</target>
I also have the SONAR_HOME environment variable set but when i execute "ant sonar" i get the following error am i missing something
BUILD FAILED
/root/.jenkins/jobs/Project/workspace/dev/archive/build.xml:108: java.lang.IllegalArgumentException:
The following mandatory information is missing:
- task attribute 'key'
- task attribute 'version'
at org.sonar.ant.SonarTask.checkMandatoryProperties(SonarTask.java:212)
at org.sonar.ant.SonarTask.execute(SonarTask.java:190)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:811)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Total time: 0 seconds

You are trying to use the "syntaxt" of the latest version of the Ant task (2.0) but it seems that this is not the version you have in your classpath.

Looks like you're missing the mandatory SonarQube property values that belong in your build.xml file.
From the docs
<!-- Define the SonarQube global properties (the most usual way is to pass
these properties via the command line) -->
<property name="sonar.host.url" value="http://localhost:9000" />
...
<!-- Define the SonarQube project properties -->
<property name="sonar.projectKey" value="org.sonarqube:sonarqube-scanner-ant" />
<property name="sonar.projectName" value="Example of SonarQube Scanner for Ant Usage" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.sources" value="src" />
<property name="sonar.java.binaries" value="build" />
<property name="sonar.java.libraries" value="lib/*.jar" />
Alternatively, you could also pass these values into ANT as arguments.
ant sonar -Dsonar.projectVersion=1.1

Related

SonarQube not able to read jacoco.exec file

We are using below versions to get JaCoCo Code Coverage report on SonarQube.
SonarQube 5.6.6
JaCoCo 0.7.9
Apache Ant 1.10.1
JUnit 4.12
JMockIt 1.20
We execute Jenkins job nightly to perform code analysis using SonarQube. We execute JUnit cases and gather code coverage of application.
We perform below steps as part of Sonar snapshot execution as part of Ant build.
1) Build the application
2) Execute JUnit Cases
3) Code Coverage
4) Sonar analysis
Now we frequently show failure in Jenkins sonar job due to SonarQube analysis failed and not able to application snapshot on Sonar server. If we execute Ant sonar target manually then we it was successful and snapshot generate without any problem in code coverage.
[sonar:sonar] Sensor SurefireSensor
[sonar:sonar] parsing D:\Projects\Collaborate\Branches\R4_3\deployment\latest
[sonar:sonar] Resource not found: junit.framework.JUnit4TestCaseFacade
[sonar:sonar] Sensor SurefireSensor (done) | time=3343ms
[sonar:sonar] Sensor JaCoCoSensor
[sonar:sonar] Analysing D:\Projects\Collaborate\Branches\R4_3\deployment\latest\jacoco.exec
BUILD FAILED
D:\Projects\Collaborate\Branches\R4_3\deployment\build.xml:2246:
org.sonar.squidbridge.api.AnalysisException: Unable to read
D:\Projects\Collaborate\Branches\R4_3\deployment\latest\jacoco.exec
at org.sonar.plugins.jacoco.JacocoReportReader.readJacocoReport(JacocoReportReader.java:78)
at org.sonar.plugins.jacoco.AbstractAnalyzer.readExecutionData(AbstractAnalyzer.java:140)
at org.sonar.plugins.jacoco.AbstractAnalyzer.analyse(AbstractAnalyzer.java:114)
at org.sonar.plugins.jacoco.JaCoCoSensor.analyse(JaCoCoSensor.java:57)
at org.sonar.batch.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:58)
at org.sonar.batch.phases.SensorsExecutor.execute(SensorsExecutor.java:50)
at org.sonar.batch.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:83)
at org.sonar.batch.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:192)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.batch.scan.ProjectScanContainer.scan(ProjectScanContainer.java:241)
at org.sonar.batch.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:236)
at org.sonar.batch.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:226)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.batch.task.ScanTask.execute(ScanTask.java:47)
at org.sonar.batch.task.TaskContainer.doAfterStart(TaskContainer.java:86)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.batch.bootstrap.GlobalContainer.executeTask(GlobalContainer.java
Total time: 21 minutes 19 seconds
Following is the Ant "sonar" target configuration.
<target name="sonar" depends="init" xmlns:sonar="antlib:org.sonar.ant">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath>
<pathelement path="../../SONARlib/sonarqube-ant-task-2.4.jar" />
</classpath>
</taskdef>
<tstamp>
<format property="sonar_starttime" pattern="dd_MM_yyyy_HH_mm" />
</tstamp>
<echo>Sonar Scan Start Time:${sonar_starttime}</echo>
<property name="sonar.projectBaseDir"
value="<project_dir>" />
<property name="sonar.host.url" value="<sonar_server>" />
<property name="sonar.login"
value="<sonar_token>" />
<property name="sonar.projectKey" value="<project_key>" />
<property name="sonar.projectName" value="<project_name>" />
<property name="sonar.projectVersion" value="(R4_3)_${sonar_starttime}" />
<property name="sonar.analysis.mode" value="publish" />
<property name="sonar.language" value="java" />
<property name="sonar.java.source" value="1.8" />
<property name="sonar.sourceEncoding" value="UTF-8" />
<property name="sonar.sources" value="src/java,src/gradleSource" />
<property name="sonar.inclusions" value="**/*.java" />
<property name="sonar.tests" value="src/tests" />
<property name="sonar.test.inclusions" value="**/*Test.java" />
<property name="sonar.test.exclusions"
value="**/*SuiteTest.java,**/*Expectation.java" />
<property name="sonar.java.binaries" value="${build.root}/${appname}/WEB-
INF/classes" />
<property name="sonar.java.test.binaries"
value="${build.root}/${appname}/WEB-INF/classes" />
<property name="sonar.java.libraries"
value="${env.JAVA_HOME}/jre/lib/*.jar,${build.root}/${appname}/WEB-
INF/lib/*.jar,${tomcat.root}/lib/*.jar,JUnitlib/*.jar,
ANTlib/*.jar,SVNlib/*.jar,JAXBlib/*.jar,../SONARlib/*.jar" />
<property name="sonar.java.test.libraries"
value="${env.JAVA_HOME}/jre/lib/*.jar,${build.root}/${appname}/WEB-
INF/lib/*.jar,${tomcat.root}/lib/*.jar,JUnitlib/*.jar,
ANTlib/*.jar,SVNlib/*.jar,JAXBlib/*.jar,../SONARlib/*.jar" />
<property name="sonar.scm.disabled" value="false" />
<property name="sonar.scm.provider" value="svn" />
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.jacoco.reportPath"
value="${junit.xml.exec.latest.dir}/jacoco.exec" />
<property name="sonar.junit.reportsPath"
value="${junit.xml.exec.latest.dir}" />
<sonar:sonar />
<tstamp>
<format property="sonar_endtime" pattern="dd_MM_yyyy_HH_mm" />
</tstamp>
<echo>Sonar Scan End Time:${sonar_endtime}</echo>
Please let me know if you need further details.
Could you please help why this JaCoCo exception occur while sonar analysis?
In a lot of cases, jacoco.exec is just not uet generated.
So i suggest that you check the reasons of generation fail exp :
Project build failed.
Build not even triggered.

Failed to create task for type sonar

I have written ant build script to run sonar. But, am getting below issue while building:
Error Message :
BUILD FAILED
C:\sonar\workspace\Sample\build.xml:12: Problem: failed to create task or type sonar
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any / declarations have taken place.
build.xml:
<project name="SampleSonar" default="sonar" basedir=".">
<target name="cleanclasses">
<javac srcdir="src" destdir="bin/temp" classpath="" debug="on"/>
</target>
<target name="sonar" depends="cleanclasses">
<!-- Add the Sonar task -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="C:\apache-ant-1.7.1\lib\sonar-ant-task-2.2.jar" />
</taskdef>
<sonar>
<property name="sonar.sources" value="src" />
<property name="sonar.projectKey" value="Sample" />
<property name="sonar.projectName" value="Sonarqube Sample Project" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.language" value="java" />
<property name="sonar.sources" value="src" />
<property name="sonar.sourceEncoding" value="UTF-8" />
<property name="sonar.host.url" value="http://localhost:9000" />
<property name="temp.dir" value="/src/temp" />
</sonar>
</target>
</project>
I am using :
java: 1.6
sonar ant task : 2.2
sonarqube : 4 ant : 1.7.1
How can I create ant task to run sonar ?
According to documentation, task is <sonar:sonar> but not <sonar>.
There are two errors in your script which can be solved as following:
add a namespace to the sonar task (e.g. ´sonarns´ or just ´sonar´)
define the ant properties outside the the sonar task tag
Your sonar target should look like that:
<target name="sonar" depends="cleanclasses">
<!-- Add the Sonar task -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="C:\apache-ant-1.7.1\lib\sonar-ant-task-2.2.jar" />
</taskdef>
<property name="sonar.sources" value="src" />
<property name="sonar.projectKey" value="Sample" />
...
<sonarns:sonar xmlns:sonarns="antlib:org.sonar.ant" />
</target>

Sonar Ant Task running in compatibility mode

I have the following ant project settings for sonar:
<!-- list of Sonar project related properties -->
<property name="sonar.projectName" value="bla bla" />
<property name="sonar.projectKey" value="com.my.project" />
<property name="sonar.projectVersion" value="7.3" />
<property name="sonar.language" value="java" />
<property name="sonar.modules" value="admin-api" />
<!-- all of the sub-modules -->
<property name="admin-api.sonar.projectName" value="admin-api" />
<property name="admin-api.sonar.projectBaseDir" location="server/admin/admin-api"/>
<property name="admin-api.sonar.sources" value="src" />
<property name="admin-api.sonar.binaries" value="build" />
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant">
<!-- path to libraries (optional). These libraries are for example used by the Java Findbugs plugin -->
<libraries>
<path>
<fileset dir="server/third-party-jars" includes="*.jar" />
</path>
</libraries>
</sonar:sonar>
I am getting 1 warning :
[sonar:sonar] *****************************************************************************************************************************************
[sonar:sonar] /!\ Sonar Ant Task running in compatibility mode: please refer to the documentation to udpate your scripts to comply with the standards.
[sonar:sonar] *****************************************************************************************************************************************
And also The following error:
The following mandatory information is missing:
- task attribute 'key'
- task attribute 'version'
I understand that there were changes migrating to the new sonar ant task 2.0 but couldn't find any place where the changes were specified.
Also, I understand that all of the modules settings now have to be specified in the parent build.xml only and not in the sub-modules build.xml files. So why do I get the errors ? I followed the example in https://github.com/SonarSource/sonar-examples/tree/master/projects/multi-module/ant . Am I missing something ?
Thanks
You're in the compatibility mode because you have defined "libraries" inside the "sonar:sonar" tag. Instead, you should define "sonar.libraries" outside of this tag, by joining all the JAR file paths with a comma.
Then the multi-module mechanism will work as expected.
I am facing the same issue. In my case, the build.xml looks like belows:
<target depends="build" name="sonar-runner">
<!-- Define the SonarQube properties -->
<property name="sonar.host.url" value="http://sonar-server:9000/sonar" />
<property name="sonar.jdbc.url" value="jdbc:jtds:sqlserver://comp-sql-db-server/sonar;SelectMethod=Cursor" />
<property name="sonar.jdbc.username" value="XXXXXX" />
<property name="sonar.jdbc.password" value="*******" />
<property name="sonar.projectKey" value="asdfsaf:2550" />
<property name="sonar.projectName" value="Proj 1" />
<property name="sonar.projectVersion" value="4.0.1" />
<property name="sonar.language" value="java" />
<property name="sonar.profile" value="Sonar Way" />
<property name="sonar.sources" value="src" />
<property name="sonar.sourceEncoding" value="UTF-8" />
<!-- run sonar for this project -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<!-- Update the following line, or put the "sonar-ant-task-*.jar" file
in your "$HOME/.ant/lib" folder -->
<classpath path="${lib-dist}/sonar/sonar-ant-task-2.1.jar" />
</taskdef>
<!-- Execute Sonar -->
<sonar:sonar />
</target>
This means that I have setup ant for single project but don't know why its requiring for Key and Version even its defined in properties even I didn't provide any thing inside the sonar:sonar tag.
Thanks

Mandatory properties missing when migrating to new 2.0 ant task sonar

I have a multi module project (with many sub-modules) defined in Sonar and it was working great until i've upgraded sonar to the newer version.
My previous settings were:
a single parent build.xml that contain all of the general project properties like jdbc connection etc. Also it contained the <sonar:sonar /> task to run Sonar. This parent project does not contain any source / binaries, just the instruction to build the sub-modules.
many sub-modules build.xml files that contained only the sub-module specific properties like sonar.projectKey, sonar.sources and sonar.binaries
Now in the new Sonar ant task I need to define everything in the parent buid.xml. I have defined it in the following way (I only show one sub-module here, I assume it should work the same if I add more modules later) :
<target name="sonar">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="build-utils/lib/sonar-ant-task-2.0.jar" />
</taskdef>
<!-- list of Sonar database related properties -->
<property name="sonar.jdbc.url" value="jdbc:oracle:thin:#localhost/DB11g" />
<property name="sonar.jdbc.driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<!-- for security reasons, pass these parameters from outside -->
<property name="sonar.jdbc.username" value="" />
<property name="sonar.jdbc.password" value="" />
<property name="sonar.host.url" value="http://localhost:9000" />
<!-- list of Sonar project related properties -->
<property name="sonar.projectName" value="My Project" />
<property name="sonar.projectKey" value="com.my.project" />
<property name="sonar.projectVersion" value="7.3" />
<property name="sonar.language" value="java" />
<property name="sonar.libraries" value="server/third-party-jars"/>
<property name="sonar.modules" value="admin-api" />
<!-- all of the sub-modules -->
<property name="admin-api.sonar.projectName" value="admin-api" />
<property name="admin-api.sonar.projectBaseDir" location="server/admin/admin-api"/>
<property name="admin-api.sonar.sources" value="src" />
<property name="admin-api.sonar.binaries" value="build" />
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant" />
</target>
When I run the target I get the following error:
You must define the following mandatory properties for 'com.orca.rightv': sonar.sources
Another question, is there any way to keep the specific definition of the sub-modules in each module's build.xml like I did in the previous version ? That would save me a lot of work.
Thanks
There's a bug in the current version of the Ant Task, which is related to the version of the Sonar Runner API embedded. This will be fixed in the next version: http://jira.codehaus.org/browse/SONARPLUGINS-2818
Meanwhile, you can define "sonar.sources" at the top, its value will be inherited in to modules (and obviously you can still override it in modules).
As for the definition of properties at module level, you can define those properties in a "sonar-project.properties" file that you put at the root of the module, like:
sonar.projectName=My Project
# following needed only to override the values defined in top build.xml
sonar.sources=src
sonar.binaries=build
, and just leave "admin-api.sonar.projectBaseDir" property (like you did) in the top build.xml file.

Entity manager has not been injected:is the Spring Aspects JAR configured as an AJC/AJDT aspects library?

I'm trying write a simple spring rest web service, since I'm not familiar with maven(and because maven usually can't et things done--connection issue), so I'm trying ant build now. Now, the build is ok and the service can run. But if the persistent function is triggered, it will prompt:
01:48:39 Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'uncaughtException'; model is {exception=java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)} -org.springframework.web.servlet.DispatcherServlet
java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
at com.books.domain.UserInfo.entityManager(UserInfo.java:96)
at com.books.domain.UserInfo.findUserByNamePassword(UserInfo.java:157)
at com.books.web.UserController.register(UserController.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
My ant build.xml:
<target name="compile" depends="init" description="compile the source ">
<!--<javac srcdir="${src}"
debug="on"
destdir="${build}"
classpathref="compile.classpath"
includeantruntime="false"/>-->
<iajc source ="1.6" target="1.6" sourceroots="src/main/java" destDir="src/main/webapp/WEB-INF/classes" showweaveinfo="true" verbose="true">
<classpath>
<path refid="compile.classpath"/>
<path refid="aspectj.classpath"/>
</classpath>
</iajc>
</target>
My applicationContext.xml:
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
</bean>
Any help would be highly appreciated. Thanks!
I found the solution :
This is the correct iajc task
<iajc source ="1.6" target="1.6" sourceroots="src/main/java" destDir="src/main/webapp/WEB-INF/classes" showweaveinfo="true" verbose="true">
<classpath>
<path refid="compile.classpath"/>
<path refid="aspectj.classpath"/>
</classpath>
<aspectpath>
<pathelement location="${workdir}/WEB-INF/lib/spring-aspects-3.2.6.RELEASE.jar" />
</aspectpath>
</iajc>
Resolved this issue by using ROO and maven pom. Too less info on how to use Ant build.
Considering switching to Roo is not really a solution but a workaround, I want to share my experiences about this issue.
There are 2 things you need to check:
You need to compile your classes via aspectj. Maven aspectj plugin provides this requirement.
Your entity class must be annotated with #Configurable

Resources