使用 Ant 将 JaCoCo 集成到声纳中

2024-03-15

我陷入了使用 Ant 将 JaCoCo 与声纳集成的困境。 对于这项任务非常陌生,并且是第一次集成。 我浏览过很多链接,例如

  • https://github.com/SonarSource/sonar-examples/blob/master/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/build.xml https://github.com/SonarSource/sonar-examples/blob/master/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/build.xml
  • http://docs.sonarqube.org/display/SONAR/Code+Coverage+by+Unit+Tests+for+Java+Project http://docs.sonarqube.org/display/SONAR/Code+Coverage+by+Unit+Tests+for+Java+Project
  • 还有很多。

我正在尝试在另一个虚拟机上显示结果,代码位于我的虚拟机中。 出现与 JDK6、VM 相关的各种错误,并且无法分析我的包结构 我的问题是,除了上述虚拟机等链接中给出的之外,我是否还需要定义任何特定的项目属性 我还想包括我的 checkstyle 和 findbugs 工作得非常顺利。 只需要集成JaCoCo即可。 任何信息都会有很大帮助。

如果发现我的描述不符合我的要求,请询问

感谢帮助


以下是配置为运行 junit 单元测试并使用 jacoco 进行代码覆盖率报告的 ANT 构建。结果上传到 Sonar,最后使用 ivy 下载第 3 方 jar 并管理类路径。

Example

├── build.properties
├── build.xml
├── ivy.xml
└── src
    ├── main
    │   └── java
    │       └── org
    │           └── demo
    │               └── App.java
    └── test
        └── java
            └── org
                └── demo
                    └── AppTest.java

构建属性

# Build properties
build.dir=build

src.dir=src/main/java
test.src.dir=src/test/java

classes.dir=${build.dir}/classes
test.classes.dir=${build.dir}/test-classes

reports.dir=${build.dir}/reports

# Sonar properties
sonar.projectKey=org.demo:demo
sonar.projectName=Demo project
sonar.projectVersion=1.0
sonar.projectDescription=This is my demo Sonar project

sonar.host.url=http://localhost:9000

sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
sonar.jdbc.driverClassName=org.h2.Driver
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

sonar.working.directory=${build.dir}/sonar

sonar.language=java
sonar.sources=${src.dir}
sonar.binaries=${classes.dir}
sonar.tests=${test.src.dir}

sonar.dynamicAnalysis=reuseReports
sonar.surefire.reportsPath=${reports.dir}/junit
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=${build.dir}/jacoco.exec

构建.xml

<project name="demo" default="test" xmlns:ivy="antlib:org.apache.ivy.ant">

    <property file="build.properties"/>

    <target name="bootstrap" description="Install ivy">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
    </target>

    <target name="resolve" description="Download dependencies and setup classpaths">
        <ivy:resolve/>
        <ivy:report todir='${reports.dir}/ivy' graph='false' xml='false'/>

        <ivy:cachepath pathid="compile.path" conf="compile"/>
        <ivy:cachepath pathid="test.path"    conf="test"/>
        <ivy:cachepath pathid="build.path"   conf="build"/>
    </target>

    <target name="init" depends="resolve" description="Create build directories">
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${test.classes.dir}"/>
        <mkdir dir="${reports.dir}/junit"/>
    </target>

    <target name="compile" depends="init" description="Compile source code">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
    </target>

    <target name="compile-tests" depends="compile" description="Compile test source code">
        <javac srcdir="${test.src.dir}" destdir="${test.classes.dir}" includeantruntime="false" debug="true">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
            </classpath>
        </javac>
    </target>

    <target name="test" depends="compile-tests" description="Run unit tests and code coverage reporting">
        <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml" classpathref="build.path"/>

        <jacoco:coverage destfile="${build.dir}/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
            <junit haltonfailure="yes" fork="true">
                <classpath>
                    <path refid="test.path"/>
                    <pathelement path="${classes.dir}"/>
                    <pathelement path="${test.classes.dir}"/>
                </classpath>
                <formatter type="plain" usefile="false" />
                <formatter type="xml"/>
                <batchtest fork="yes" todir="${reports.dir}/junit">
                    <fileset dir="${test.src.dir}">
                        <include name="**/*Test*.java"/>
                        <exclude name="**/AllTests.java"/>
                    </fileset>
                </batchtest>
            </junit>
        </jacoco:coverage>
    </target>

    <target name="sonar" depends="test" description="Upload metrics to Sonar">
        <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpathref="build.path"/>

        <ivy:cachepath pathid="sonar.libraries" conf="compile"/> 

        <sonar:sonar xmlns:sonar="antlib:org.sonar.ant"/>
    </target>

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

    <target name="clean-all" depends="clean" description="Additionally purge ivy cache">
        <ivy:cleancache/>
    </target>

</project>

ivy.xml

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations defaultconfmapping="compile->default">
        <conf name="compile" description="Required to compile application"/>
        <conf name="test"    description="Required for test only" extends="compile"/>
        <conf name="build"   description="Build dependencies"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.10" conf="test->default"/>

        <!-- build dependencies -->
        <dependency org="org.codehaus.sonar-plugins" name="sonar-ant-task" rev="2.1" conf="build->default"/>
        <dependency org="org.jacoco" name="org.jacoco.ant" rev="0.6.3.201306030806" conf="build->default"/>

        <!-- Global exclusions -->
        <exclude org="org.apache.ant"/>
    </dependencies>

</ivy-module>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Ant 将 JaCoCo 集成到声纳中 的相关文章

  • JNI 和 Java:ant 调用 make 还是 make 调用 ant?

    我即将第一次进入 JNI Java 本机接口 的世界 以提供从平台特定的 C C 代码到 Java 的文件系统更改通知 除非有人推荐一些我错过的出色的图书馆来做这件事 作为 JNI 的新手 我已经设法找到了很多关于 JNI 接口方面和库生成
  • 将环境变量传递给ant任务,不带ANT_OPTS

    我正在调用 Jasper ant 任务 并且我想设置org apache jasper compiler Parser STRICT QUOTE ESCAPING环境变量 我可以将 ANT OPTS 设置为 Dorg apache jasp
  • Android Ant 包含 Java 库项目

    我正在尝试使用 Ant 编译一个 Android 项目 以便它可以与构建机器一起工作 但我遇到了问题 我总共有五个项目 三个只是 java 库项目 另外两个是实际的 Android 项目 实际上只有一个项目被编译和安装 但它从其他项目中提取
  • SonarQube - 重复的块,如何更改配置

    SonarQube 向我展示了这种重复 1 package pl com bernas ioz user domain 2 3 import java io Serializable 这不是期望的行为 我可以禁用这种重复吗 但我根本不想禁用
  • 如何在ivy中强制执行HTTPS?

    这是我的 ivy xml
  • Ant 条件 - 'if' 或 'unless' 哪个先出现

    Question 如果 ant 目标同时使用if and unless 首先评估哪个 Example 先有鸡还是先有蛋
  • 无法从 sonarqube 服务器更新数据

    当 sonarlint eclipse 要求我刷新我的 sonarqube 数据 更新所有项目绑定 时 我收到以下错误 Unable to update data from server cerbere Unable to move C w
  • 从 ANT 运行 BAT 文件

    我浏览了论坛上的许多帖子 但无法整理出来 我正在尝试从 ANT 脚本运行 BAT 文件 文件夹层次结构是这样的 Project build xml build C test bat 我编写的 ANT 文件是
  • 未找到指定的 VM 安装:类型标准 VM,名称 jre7

    未找到指定的 VM 安装 类型标准 VM 名称 jre7 您在 Eclipse 中构建 ant 文件时遇到过这个问题吗 那么这篇文章适合您 删除并重新创建工作区并不是解决方案 有一个简单的解决方案可以解决此问题 而无需重新创建工作区 右键单
  • Ant javac 任务出错:[javac] 警告:[选项] 引导类路径未与 -source 1.6 一起设置

    我正在尝试运行一个使用的 ant 任务axis2 ant plugin 1 6 0 jar org apache axis2 tool ant AntCodegenTask执行一个WSDL2Java手术 在ant脚本的顶部 我定义了java
  • Sonarqube C# 扫描仪异常:“java.lang.IllegalArgumentException:不是指针的有效行偏移量”

    有谁知道这是怎么回事 在Windows服务器上使用Sonarqube v5 6 c 扫描仪v5 5 0 479 似乎是在多个文件中发现这一点 而不总是相同的文件 这是最令人担忧的 有人有主意吗 16 46 03 INFO Importing
  • 更改 ant junit 任务中的工作目录

    我有一个运行 JUnits 测试的 ant 文件 这些测试依赖于某些配置文件的相对路径 我尝试设置批量测试的工作目录 但失败 我希望工作目录是 plugins dir name ant 脚本的 JUnit 部分
  • 如何从同一项目生成两个声纳报告?

    我想从同一项目创建两组声纳报告 一种会涵盖所有内容 另一种会排除一些包裹 这可能吗 如果可以 该怎么做 编辑 设置排除不是问题 但有两个报告才是问题 在 Maven 中创建新的配置文件 并为每个配置文件添加带有新分支的呼叫声纳 mvn cl
  • 本地分析需要 Sonar 3.6 版本

    遇到 执行本地分析需要声纳版本 3 6 消息 当尝试在 eclipse 中使用声纳本地分析时 我刚刚安装了最新的CodeHaus http docs codehaus org display SONAR Installing SonarQu
  • 声纳+MS SQL数据库

    我正在尝试使用 mssql 服务器数据库初始化声纳 但我不能 使用 Microsoft SQL Server Management Studio 我创建了用户名 sonar 和密码 sonar 的用户 并向同时创建的 sonar 数据库授予
  • 使用 Proguard 混淆 ActionBarSherlock

    我正在尝试使用混淆我的 Android 应用程序proguard and ant eclipse proguard从来不工作 总是失败error 1 但我越来越class file unexpectedly contains class 到
  • 如何从 Ant 构建文件设置 Eclipse 构建路径和类路径?

    关于 Ant 和 Eclipse 有很多讨论 但之前的答案似乎对我没有帮助 事情是这样的 我正在尝试构建一个可以从命令行使用 Ant 成功编译的 Java 程序 更令人困惑的是 我尝试编译的程序是 Ant 本身 我真正想做的是将这个项目引入
  • 在 Java 构建过程中更改常量的最佳方法

    我继承了一个在 Tomcat 下运行的 Java 应用程序 servlet 由于历史原因 根据应用程序的部署位置 本质上是品牌问题 代码具有不同的 外观和感觉 选项 有几个常量控制这个品牌过程 它们具有不同的功能 不应压缩为单个常量 即 B
  • 运行 java -jar 时出现 java.lang.ClassNotFoundException

    我正在使用 ant 来构建我的build xml文件 它编译正常 但随后得到运行时java lang NoClassDefFoundError通过 运行生成的 jar 时java jar my jar jar 似乎这个问题出现了很多 但没有
  • 声纳中的代码覆盖率和线路覆盖率有什么区别

    我知道行覆盖率和分支覆盖率有什么区别 但是代码覆盖率和行覆盖率有什么区别 以前的指令覆盖吗 覆盖范围是线路覆盖范围和分支覆盖范围的微妙组合 您可以在我们的网站上找到公式指标描述页面 https docs sonarqube org late

随机推荐