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

qlvxas9a  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(294)

我正在使用ant进行一个项目,他们需要在其中添加声纳。我想从一个简单的使用sonar的项目开始,我必须遵循哪些步骤才能将sonar添加到这个简单的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>

但是,当我从projectbase目录运行antsonar命令来启动分析时,会遇到如下问题

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

谢谢任何能帮助我的人。

inkz8wg9

inkz8wg91#

在sonar.properties文件中,请确保已对以下行进行了注解:
“#sonar.web.context=”
这对我很有用

相关问题