简体   繁体   中英

Sonar with Ant Setup Error

Receiving an error message when trying to execute the ant script: The prefix "sonar" for element "sonar:sonar" is not bound.

I know my ant is setup correctly, because when I take out the following sonar parts it builds fine, and sonar is set up correctly because I have been successfully analyzing projects with maven.

Added the following three snippets to the code:

 **<!-- Define the Sonar properties -->
<property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
<property name="sonar.projectName" value="Simple Java Project analyzed with the Sonar Ant Task" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.language" value="java" />
<property name="sonar.sources" value="${source.dir}" />
<property name="sonar.binaries" value="${libs.dir}" /> 
<property name="sonar.sourceEncoding" value="UTF-8" />


<!-- Add your basic Sonar configuration below: sonar.jdbc.url, sonar.jdbc.username, etc. properties -->
<property name="sonar.jdbc.url" value="jdbc:sqlserver://server;databaseName=Sonar;selectMethod=cursor;" />
<property name="sonar.jdbc.username" value="sonar" />
<property name="sonar.jdbc.password" value="sonarPass" />

Then I created a new target and added sonar to my build order.

<!-- ========= Define Sonar target ========= -->
<target name="sonar" depends="compile">
    <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="C:\sonarqube-4.1\lib\sonar-ant-task-2.1.jar" />
    </taskdef>

    <!-- Execute Sonar -->
    <sonar:sonar />
</target>

The error occurs on the <sonar:sonar /> line, and since it breaks so quickly without running any of the other tasks, I would guess that the problem is in the precompiler finding the library.

I have placed sonar-ant-task-2.1.jar in as many places possible, tried adding to my path through environment variables, and tried a few different ways of specifying the path. Any idea of why ant is not picking up on the library or how I could troubleshoot this further would be greatly appreciated.

This is an XML parsing error. You need to declare the "sonar" namespace at the top of your build file:

<project .... xmlns:sonar="antlib:org.sonar.ant">

This is what enables the "sonar" prefix before the task name.

<sonar:sonar/>

I would recommend against adding jar files indiscriminately to various directories--strive to have exactly what you need exactly where you need it and nothing more. To understand why Ant does not find your Sonar task, you might try to run it in "verbose" mode:

ant -v

If you can do the same with Maven, you can try

mvn -X

to compare how Maven is able to succeed. Good luck!

I have used the below code. It worked fine.

<!-- Define the SonarQube target -->
<target name="sonar"  xmlns:sonar="antlib:org.sonar.ant">

    <property name="mysonar.organizationName" value="myProject"/>
    <property name="sonar.projectName" value="SonarDemo" />
    <property name="sonar.projectKey" value="${mysonar.organizationName}:${sonar.projectName}" />
    <!-- project version--> 
    <property name="sonar.projectVersion" value="1.0" />
    <!-- location source files --> 
    <property name="sonar.sources" value="${src}" /> 

    <!-- location of binaries after compilation--> 
    <property name="sonar.binaries" value="${build}"/>

    <!-- location of sonar library-->  
    <sonar:sonar xmlns:sonar="antlib:org.sonar.ant">
    </sonar:sonar>

</target>

Below is code worked using ant and sonarqube

<project name="abcPrj" basedir="." xmlns:sonar="antlib:org.sonar.ant">
<property name="src.dir" value="src" />
<property name="build.dir" value="target" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="lib.dir" value="web/WEB-INF/lib" />
<property name="webClasses.dir" value="web/WEB-INF/classes" />
<property name="tomcat.home" value="C:\apache-tomcat-8.0.28" />

<path id="tomcat.classpath">
    <fileset dir="${tomcat.home}/bin">
        <include name="**/*.jar" />
    </fileset>
    <fileset dir="${tomcat.home}/lib">
        <include name="**/*.jar" />
    </fileset>
</path>

<target name="clean">
    <delete dir="${build.dir}" />
</target>

<target name="init">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${classes.dir}" />
</target>

<target name="compile" depends="init">
    <path id="java.classpath">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <javac destdir="${classes.dir}" fork="true" memoryInitialSize="512m" memoryMaximumSize="1024m" debug="true"
        srcdir="${src.dir}">
        <classpath refid="java.classpath" />
        <classpath refid="tomcat.classpath" />
    </javac>

    <echo message="build done done done" />
</target>

<target name="sonar" depends="compile">
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <classpath path="C:\dev\ant-libs\sonar-ant-task-2.2.jar" />
    </taskdef>

    <property name="sonar.host.url" value="http://localhost:9000" />
    <property name="sonar.projectKey" value="com.abc:xyz" />
    <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.dir}" />
    <property name="sonar.java.binaries" value="${webClasses.dir}" />
    <property name="sonar.java.libraries" value="${lib.dir}/*.jar" />

    <!-- Execute SonarQube Scanner for Ant Analysis -->
    <sonar:sonar />
</target>

<target name="all" depends="clean, init, compile, sonar" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM