【问题标题】:Sonar with Ant Setup Error带有 Ant 设置错误的声纳
【发布时间】:2014-02-18 18:22:12
【问题描述】:

尝试执行 ant 脚本时收到错误消息: 元素“sonar:sonar”的前缀“sonar”未绑定。

我知道我的 ant 设置正确,因为当我取出以下声纳部件时,它构建良好,并且声纳设置正确,因为我已经成功地使用 maven 分析项目。

在代码中添加了以下三个sn-ps:

 **<!-- 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" />

然后我创建了一个新目标并将声纳添加到我的构建订单中。

<!-- ========= 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>

错误发生在&lt;sonar:sonar /&gt; 行上,并且由于它在没有运行任何其他任务的情况下快速中断,我猜想问题出在预编译器查找库中。

我已将 sonar-ant-task-2.1.jar 放在尽可能多的地方,尝试通过环境变量添加到我的路径中,并尝试了几种不同的指定路径的方法。任何关于为什么 ant 没有使用图书馆或我如何进一步解决此问题的想法将不胜感激。

【问题讨论】:

    标签: ant sonarqube


    【解决方案1】:

    我建议不要将 jar 文件不加选择地添加到各种目录中——力求在你需要的地方准确地获得你需要的东西,仅此而已。要了解 Ant 为什么找不到您的 Sonar 任务,您可以尝试以“详细”模式运行它:

    ant -v
    

    如果你可以用 Maven 做同样的事情,你可以试试

    mvn -X
    

    比较 Maven 是如何成功的。祝你好运!

    【讨论】:

    • 我应该提到我已经在详细模式下运行,堆栈跟踪实际上是无用的。由 org.xml.sax.SAXParseException 引起...另外澄清一下,我从未将 jar 放在多个地方,而是尝试移动它并更改路径。
    【解决方案2】:

    这是一个 XML 解析错误。您需要在构建文件的顶部声明“声纳”命名空间:

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

    这就是在任务名称前启用“声纳”前缀的原因。

    <sonar:sonar/>
    

    【讨论】:

    • 您一针见血,非常感谢您,先生
    • 这是一个很常见的问题!
    【解决方案3】:

    我使用了以下代码。它工作得很好。

    <!-- 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>
    

    【讨论】:

      【解决方案4】:

      以下是使用 ant 和 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" />
      

      【讨论】:

      • 而不是声纳命名空间的全局声明(在项目根标签处),直接在 sonar:sonar 任务中声明就足够了:´´
      猜你喜欢
      • 2014-01-18
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2011-12-09
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多