如何使用声纳分析我的简单项目?

时间:2015-04-28 07:50:23

标签: java ant sonarqube

我正在使用Ant工作,他们需要为它添加声纳。我想从一个使用声纳的项目的简单示例开始。我必须遵循以下步骤将声纳添加到这个简单的项目java

这是我的简单课程主要

package com.sonar.test;

public class Test {

    public Test() {
        exp01();
    }

    public void exp01() {

        System.out.println("Test test");

    }

    public static void main(String[] args) {

        new Test();
    }

}

这是我的build.xml文件

<project name="Test analyzed with sonar" basedir="." default="all" xmlns:sonar="antlib:org.sonar.ant">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="com.sonar.test.Test"/>

    <!-- 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="src" />
    <property name="sonar.binaries" value="target" />
    <property name="sonar.sourceEncoding" value="UTF-8" />

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

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <!-- ========= 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="path/to/sonar/ant/task/lib/sonar-ant-task-*.jar" />
        </taskdef>

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

    <!--
    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>
    -->
    <target name="all" depends="clean,compile,sonar" />
</project>

但是当我从项目基础目录运行ant sonar命令启动分析时,我遇到了类似

的问题
C:\SAN_SDK_Test_Sonar\Test\build.xml:46: java.lang.IllegalStateException: Fail to request server version
        at org.sonar.runner.impl.ServerVersion.downloadVersion(ServerVersion.java:49)
        at org.sonar.runner.impl.ServerVersion.version(ServerVersion.java:38)
        at org.sonar.runner.impl.ServerVersion.is37Compatible(ServerVersion.java:58)
        at org.sonar.runner.impl.JarDownloader.checkVersionAndDownload(JarDownloader.java:36)
        at org.sonar.runner.impl.BatchLauncher$1.run(BatchLauncher.java:71)
        at java.security.AccessController.doPrivileged(AccessController.java:202)
        at org.sonar.runner.impl.BatchLauncher.doExecute(BatchLauncher.java:69)
        at org.sonar.runner.impl.BatchLauncher.execute(BatchLauncher.java:50)
        at org.sonar.runner.api.EmbeddedRunner.doExecute(EmbeddedRunner.java:102)
        at org.sonar.runner.api.Runner.execute(Runner.java:100)
        at org.sonar.ant.SonarTask.launchAnalysis(SonarTask.java:53)
        at org.sonar.ant.SonarTask.execute(SonarTask.java:48)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
        at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
        at java.lang.reflect.Method.invoke(Method.java:611)
        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:435)
        at org.apache.tools.ant.Target.performTasks(Target.java:456)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
        at org.apache.tools.ant.Main.runBuild(Main.java:851)
        at org.apache.tools.ant.Main.startAnt(Main.java:235)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.io.IOException: Status returned by url : 'http://localhost:9000/api/server/version' is invalid : 404
        at org.sonar.runner.impl.ServerConnection.downloadString(ServerConnection.java:94)
        at org.sonar.runner.impl.ServerVersion.downloadVersion(ServerVersion.java:47)
        ... 27 more

Total time: 5 seconds

感谢任何能帮助我的人。

1 个答案:

答案 0 :(得分:-2)

在你的sonar.properties文件中,请务必注释掉这一行:

&#34;#sonar.web.context =&#34;

这对我有用