2015-04-28 70 views
2

我正在使用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聲納命令來啓動分析,我得到這樣

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

工作你開始聲納服務器?它運行的默認端口爲9000. 嘗試點擊'http:// localhost:9000 /' 然後開始分析。 –

+0

我已經開始了,我通過StartSonar.bat啓動它,我可以打開url http:// localhost:9000,但我無法像我在問題描述 中提到的那樣分析感謝您的幫助 – abderrahim

回答

-2

在你sonar.properties文件,確保您有評論這一行:

「#sonar.web.context =」

這對我來說

相關問題