Configuration of build.xml file for Sonar modules

2019-07-20 20:33发布

问题:

I am setting up a Sonar project (using the delphi plugin), for simplicity sake assume there are two modules I want to report on.

Each module is in it's own sub-folder and each has it's own build.xml file.

At this point I can successfully run the sonar tasks and generate reports for each module as an independent project.

My problem is with configuring the "master" build.xml file.

The module build.xml file looks like this:

<?xml version="1.0"?>
<project name = "CRM" default = "sonar" basedir = ".">
    <!-- Add the Sonar task -->
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <classpath path="c:/ANT/lib" />
    </taskdef>

    <target name="sonar">
        <property name="sonar.projectKey" value="EXO:CRM" />
        <property name="sonar.host.url" value="http://localhost:9000" />
        <sonar:sonar workDir="." key="CRM.key" version="0.1" xmlns:sonar="antlib:org.sonar.ant">
            <property key="sonar.sources" value="." />      <!-- project sources directories (required) -->
            <property key="sonar.language" value="delph" />                             <!-- project language -->
            <property key="sonar.delphi.codecoverage.excluded" value=".\tests" />           <!-- code coverage excluded directories -->
            <property key="sonar.importSources" value="true" />                         <!-- should we show sources or not? -->     
            <property key="sonar.delphi.sources.excluded" value="" />                       <!-- excluded directories -->
            <property key="sonar.delphi.sources.include" value=".\includes" />              <!-- include directories, "," separated -->
            <property key="sonar.delphi.sources.include.extend" value="true" />         <!-- should we extend includes in files? -->
        </sonar:sonar>
    </target>
</project>

The "Master" build.xml looks like this:

<?xml version="1.0"?>
<project name = "EXO" default = "sonar" basedir = ".">
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <classpath path="c:/ANT/lib" />
    </taskdef>

    <target name="sonar">
        <property name="sonar.modules" value="exonet6000/build.xml,CRM/build.xml" />
        <sonar:sonar workDir="." key="EXO.key" version="0.1" xmlns:sonar="antlib:org.sonar.ant">
            <!-- project sources directories (required) --> 
            <property key="sonar.sources" value="." />      
            <property key="sonar.language" value="delph" />
            <property key="sonar.importSources" value="true" /> 
            <property key="sonar.delphi.sources.excluded" value="" />                       
            <property key="sonar.delphi.sources.include" value=".\includes" />      
            <property key="sonar.delphi.sources.include.extend" value="true" />         
        </sonar:sonar>
    </target>
</project>

It is always scanning all sources (required value) i.e. it is not respecting my modules. The only way I can get this to work currently is by limiting the source code like this

<property key="sonar.sources" value="./crm/,./exonet6000/" />   

I'm sure I must be mis-configuring something obvious here.

EDIT: I have now what I believe is a more consistent set of files based on examples here https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/java-ant-modules

Master build file:

<?xml version="1.0" encoding="UTF-8"?>
<project name = "EXO" default = "sonar" basedir = "." xmlns:sonar="antlib:org.sonar.ant">
    <echo>Root Project</echo>

    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <classpath path="c:/ANT/lib" />
    </taskdef>

    <property name="sonar.host.url" value="http://localhost:9000" />
    <property name="sonar.modules" value="exonet6000/build.xml,CRM/build.xml" />

    <target name="sonar">
        <sonar:sonar key="EXO.key" version="0.1">   
        </sonar:sonar>
    </target>
</project>

and one of the submodule files

<?xml version="1.0" encoding="UTF-8"?>
<project name="CRM" default="all" basedir=".">
    <echo>CRM Module</echo>

    <property name="sonar.language" value="delph" />
    <property name="sonar.projectKey" value="EXO:CRM" />
    <property name="sonar.sources" value="." /> 

    <target name="all" />
</project>

At this point the sonar process is completing successfully BUT no actual anlysis is being done. A key point is that I am not seeing the echo of the submodule so I suspect these build tasks are not actually running.

回答1:

If you look at this sample project using Ant and multimodules, I'd say that you should not specify any property inside the tag in your master build.xml file, and let the submodules specify those properties.



回答2:

I've got a project with many submodules. My sonar target looks like this:

<target name="sonar" depends="build.dependencies" description="collect code metrics">
    <property name="m1" value="a/build.xml,b/build.xml,c/build.xml,d/build.xml"/>
    ... more properties defining modules...
    <property name="sonar.modules" value="${m1},${m2},${m3},${m4},${m5},${m6}..."/>
    <property name="sonar.projectName" value="MyProject"/>
    <sonar:sonar key="my:project" version="${version}" xmlns:sonar="antlib:org.sonar.ant">
    </sonar:sonar>
</target>

No property definitions in the sonar:sonar task at all, and it is working as I want it to.

Sources are defined in the submodule build.xml files. (Actually, in a base-build.xml that all our build.xml files include... but that's an Ant thing not directly related to Sonar.)