介绍

Java 8出现了,人们所期待的革命终于发布了,我敢肯定,你们中的许多人都在想着同样的问题“我应该在项目中使用它吗?”。

好吧,几个月来我遇到了同样的问题,今天我有一个答案想与大家分享。 很多方面都在影响这一决定,但是在这篇文章中,我想特别关注一个方面:

我可以继续与Java 8和NetBeans Platform进行持续集成吗?

主要问题是进行CI所需的工具的成熟度,以及将其与NetBeans平台的ant构建脚本集成起来有多容易。

幸运的是,我们发现这是可行且容易的!

我还要感谢Alberto Requena Sanchez对本文的贡献。

技术环境

在以安全与质量为主要驱动力的项目中,CI至关重要。

因此,我从我的团队开始进行“概念验证”,以表明以下技术已准备好协同工作:

  • Java 8,NetBeans 8.0和Ant
  • JUnit 4和Jacoco 0.7.1
  • 詹金斯与声纳4.2

本文的范围是解释为使Java 8的CI服务器完全正常运行而安装和设置必要工具的所有步骤,请注意,该证明已在Windows 7的开发人员机器上完成,但很容易做到。在Linux服务器中也是如此。

下图高层次显示了将在帖子中描述的体系结构。

持续集成图

Java 8,NetBeans 8.0和Ant

Java 8已发布,可以在此处下载,安装,学习(最好)并开始使用它!

我们正在使用NetBeans Platform 8.0创建模块化应用程序。 该应用程序具有多层体系结构 ,其中每个层都是模块套件,而最终的可执行文件只是一组集成套件。

我们正在使用Ant来构建我们的项目,但是如果您正在使用Maven,那么甚至可以简化该过程,因为可以通过使用Maven的插件来完成Jenkins中的Sonar集成。

JUnit 4和Jacoco 0.7.1

自然,我们正在进行单元测试,因此,我们使用JUnit4。它在任何地方都可以很好地集成,尤其是在NetBeans中。

Jacoco是生成代码覆盖率的绝佳工具,并且自0.7.1版起,它完全支持Java 8。

詹金斯与声纳4.2

Jenkins是我们CI服务器的引擎,它将与上述所有技术集成在一起,没有任何问题。 测试的版本是1.554。

声纳正在对代码进行所有质量分析。 4.2版与Java 8完全兼容。

将Sonar与Ant一起使用需要一个小型库,其中包含要集成到Jenkins中的目标。 如果您使用的是Maven,则可以只安装Maven插件 。

开始困惑

步骤1 – NetBeans

  1. 安装Java 8和NetBeans 8.0
  2. 创建一个包含几个模块,几个类和几个jUnit测试的模块套件
  3. 将代码提交到您的源代码版本管理服务器中
  4. 深入了解NetBeans
  5. 在名为“ jacoco-0.7.1”的线束中创建一个文件夹,其中包含下载的jacoco jars
  6. 在名为“ sonar-ant-task”的线束中创建一个文件夹,并将其放入下载的声纳蚂蚁罐中
  7. 在名为sonar-jacoco-module.xml的工具中创建一个文件,并将以下代码粘贴到其中:
  8. <?xml version="1.0" encoding="UTF-8"?>
    <!---->
    <project name="sonar-jacoco-module" basedir="." xmlns:jacoco="antlib:org.jacoco.ant" xmlns:sonar="antlib:org.sonar.ant">
    <description>Builds the module suite otherSuite.</description><property name="jacoco.dir" location="${nbplatform.default.harness.dir}/jacoco-0.7.1"/>
    <property name="result.exec.file" location="${jacoco.dir}/jacoco.exec"/>
    <property name="build.test.results.dir" location="build/test/unit/results"/><property file="nbproject/project.properties"/><!-- Step 1: Import JaCoCo Ant tasks -->
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
    <classpath path="${jacoco.dir}/jacocoant.jar"/>
    </taskdef><!-- Target at the level of modules -->
    <target name="-do-junit" depends="test-init">
    <echo message="Doing testing for jacoco" />
    <macrodef name="junit-impl">
    <attribute name="test.type"/>
    <attribute name="disable.apple.ui" default="false"/>
    <sequential>
    <jacoco:coverage destfile="${build.test.results.dir}/${code.name.base}_jacoco.exec">
    <junit showoutput="true" fork="true" failureproperty="tests.failed" errorproperty="tests.failed"
    filtertrace="${test.filter.trace}" tempdir="${build.test.@{test.type}.results.dir}" timeout="${test.timeout}">
    <batchtest todir="${build.test.@{test.type}.results.dir}">
    <fileset dir="${build.test.@{test.type}.classes.dir}" includes="${test.includes}" excludes="${test.excludes}"/>
    </batchtest>
    <classpath refid="test.@{test.type}.run.cp"/>
    <syspropertyset refid="test.@{test.type}.properties"/>
    <jvmarg value="${test.bootclasspath.prepend.args}"/>
    <jvmarg line="${test.run.args}"/>
    <!--needed to have tests NOT to steal focus when running, works in latest apple jdk update only.-->
    <sysproperty key="apple.awt.UIElement" value="@{disable.apple.ui}"/>
    <formatter type="brief" usefile="false"/>
    <formatter type="xml"/>
    </junit>
    </jacoco:coverage>
    <copy file="${build.test.results.dir}/${code.name.base}_jacoco.exec" todir="${suite.dir}/build/coverage"/>
    <!--
    Copy the result of all the unit tests of all the modules into one common
    folder at the level of the suite, so that sonar could find those files to
    generate associated reports
    -->
    <copy todir="${suite.dir}/build/test-results">
    <fileset dir="${build.test.results.dir}">
    <include name="**/TEST*.xml"/>
    </fileset>
    </copy>
    <fail if="tests.failed" unless="continue.after.failing.tests">Some tests failed; see details above.</fail>
    </sequential>
    </macrodef>
    <junit-impl test.type="${run.test.type}" disable.apple.ui="${disable.apple.ui}"/>
    </target></project>

    该文件的范围是覆盖添加jacoco覆盖范围的do-junit任务,并复制套件构建中每个模块的单元测试结果,以便sonar可以一起找到所有模块以进行分析。

  9. 在名为sonar-jacoco-suite.xml的线束中创建一个文件,并将以下代码粘贴到其中
  10. <?xml version="1.0" encoding="UTF-8"?>
    <project name="sonar-jacoco-suite" basedir="." xmlns:jacoco="antlib:org.jacoco.ant" xmlns:sonar="antlib:org.sonar.ant">
    <description>Builds the module suite otherSuite.</description><property name="jacoco.dir" location="${nbplatform.default.harness.dir}/jacoco-0.7.1"/>
    <property name="result.exec.file" location="build/coverage"/>    <!-- Define the SonarQube global properties (the most usual way is to pass these properties via the command line) -->
    <property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8" />
    <property name="sonar.jdbc.username" value="sonar" />
    <property name="sonar.jdbc.password" value="sonar" />
    <!-- Define the SonarQube project properties -->
    <property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
    <property name="sonar.projectName" value="Simple Java Project analyzed with the SonarQube Ant Task" />
    <property name="sonar.projectVersion" value="1.0" />
    <property name="sonar.language" value="java" />
    <!-- Load the project properties file for retrieving the modules of the suite -->
    <property file="nbproject/project.properties"/><!-- Using Javascript functions to build the paths of the data source for sonar configuration -->
    <script language="javascript">
    <![CDATA[// getting the value
    modulesName = project.getProperty("modules");
    modulesName = modulesName.replace(":",",");
    res = modulesName.split(",");
    srcModules = "";
    binariesModules = "";
    testModules = "";
    //Build the paths
    for (var i=0; i<res.length; i++)
    {
    srcModules += res[i]+"/src,";
    binariesModules += res[i]+"/build/classes,";
    testModules += res[i]+"/test,";
    }
    //Remove the last comma
    srcModules = srcModules.substring(0, srcModules.length - 1);
    binariesModules = binariesModules.substring(0, binariesModules.length - 1);
    testModules = testModules.substring(0, testModules.length - 1);
    // store the result in a new properties
    project.setProperty("srcModulesPath",srcModules);
    project.setProperty("binariesModulesPath",binariesModules);
    project.setProperty("testModulesPath",testModules);
    ]]>
    </script>
    <!-- Display the values -->
    <property name="sonar.sources" value="${srcModulesPath}"/>
    <property name="sonar.binaries" value="${binariesModulesPath}" />
    <property name="sonar.tests" value="${testModulesPath}" />
    <!-- Define where the coverage reports are located -->
    <!-- Tells SonarQube to reuse existing reports for unit tests execution and coverage reports -->
    <property name="sonar.dynamicAnalysis" value="reuseReports" />
    <!-- Tells SonarQube where the unit tests execution reports are -->
    <property name="sonar.junit.reportsPath" value="build/test-results" />
    <!-- Tells SonarQube that the code coverage tool by unit tests is JaCoCo -->
    <property name="sonar.java.coveragePlugin" value="jacoco" />
    <!-- Tells SonarQube where the unit tests code coverage report is -->
    <property name="sonar.jacoco.reportPath" value="${result.exec.file}/merged.exec" />
    <!--  Step 1: Import JaCoCo Ant tasks  -->
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
    <classpath path="${jacoco.dir}/jacocoant.jar"/>
    </taskdef>
    <target name="merge-coverage">
    <jacoco:merge destfile="${result.exec.file}/merged.exec">
    <fileset dir="${result.exec.file}" includes="*.exec"/>
    </jacoco:merge>
    </target><target name="sonar">
    <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="${harness.dir}/sonar-ant-task-2.1/sonar-ant-task-2.1.jar" />
    </taskdef><!-- Execute the SonarQube analysis -->
    <sonar:sonar />
    </target></project>

    该文件的范围是在套件级别定义声纳配置和声纳任务。 如果您使用声纳,则某些特殊的数据库或特殊的用户必须在此处更改配置。

    定义的另一项任务是jacoco合并,该合并实际上将获取每个模块的所有生成的exec,并将它们合并到套件构建中的单个exec中,以允许声纳进行分析。

  11. 用以下内容替换每个模块的build.xml的内容:
  12. <description>Builds, tests, and runs the project com.infrabel.jacoco.</description>
    <property file="nbproject/suite.properties"/>
    <property file="${suite.dir}/nbproject/private/platform-private.properties"/>
    <property file="${user.properties.file}"/>
    <import file="${nbplatform.default.harness.dir}/sonar-jacoco-module.xml"/>
    <import file="nbproject/build-impl.xml"/>
  13. 用以下内容替换每个套件的build.xml的内容:
  14. <description>Builds the module suite otherSuite.</description>
    <property file="nbproject/private/platform-private.properties"/>
    <property file="${user.properties.file}"/>
    <import file="${nbplatform.default.harness.dir}/sonar-jacoco-suite.xml"/>
    <import file="nbproject/build-impl.xml"/>

    步骤2 –詹金斯

  15. 在“管理Jenkins->管理插件”中,进入可用列表并安装(如果尚未存在)以下插件:
  • JaCoCo
  • 水星或颠覆
  • 声纳

如果您在防火墙或代理后面,并且在配置网络设置时遇到问题,可以随时从此处手动下载并安装它们。 在这种情况下,请记住还要先下载每个插件的依赖项。

  • 在“管理Jenkins->配置系统”中,检查是否正确安装了所有插件,请参见以下屏幕截图以获取示例(将文件夹替换为适合您的文件夹):
  • 创建一个新的自由样式项目,配置您的首选项的版本控制,然后在“构建”面板中添加以下三个“ Invoce Ant”任务:
  • 最后,在“构建后操作”面板中添加新的“记录Jacoco覆盖率报告”,其配置如下:
  • 步骤3 –声纳

  • 按照此脚本创建数据库,并选择运行此查询以使连接正常工作:
  • GRANT ALL PRIVILEGES ON 'sonar'.* TO 'sonar'@'localhost';
  • 进入声纳的配置文件(sonar.properties)并启用MySQL,该文件位于安装的conf文件夹中
  • # Permissions to create tables, indices and triggers
    # must be granted to JDBC user.
    # The schema must be created first.
    sonar.jdbc.username=sonar
    sonar.jdbc.password=sonar#----- MySQL 5.x
    # Comment the embedded database and uncomment the following
    # line to use MySQL
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
  • 在声纳的配置中,如果需要与Java 8兼容,请更新Java插件
  • 如有必要,请始终在sonar.properties文件中配置代理
  • 做完了!

    现在一切都已设置好,您可以进入NetBeans,进行构建,提交代码,然后在Jenkins中启动构建,然后在确定构建之后检查Sonar中的项目。

    就这样! 希望我不会忘记任何事情,但是如果您在此过程中发现一些错误,可以随时提出评论,我将尝试找到解决方案。

    翻译自: https://www.javacodegeeks.com/2014/04/how-to-do-continuous-integration-with-java-8-netbeans-platform-8-jenkins-jacoco-and-sonar.html

如何与Java 8,NetBeans Platform 8,Jenkins,Jacoco和Sonar进行持续集成相关推荐

  1. 使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境

    前言 但凡一个略有规模的项目都需要一个持续集成环境的支撑,为什么需要持续集成环境,我们来看一个例子.假如一个项目,由A.B两位程序员来协作开发,A负责前端模块,B负责后端模块,前端依赖后端.A和B都习 ...

  2. 使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一)

    前言 但凡一个略有规模的项目都需要一个持续集成环境的支撑,为什么需要持续集成环境,我们来看一个例子.假如一个项目,由A.B两位程序员来协作开发,A负责前端模块,B负责后端模块,前端依赖后端.A和B都习 ...

  3. CI/CD——构建企业级Docker+Jenkins+Git+Harbor流水线自动化持续集成持续发布平台

    构建企业级Docker+Jenkins+Git+Harbor流水线自动化持续集成持续发布平台 CI/CD是什么? 持续集成(CI)/持续交付(CD)的优势 自动化部署流程图 Git Github Gi ...

  4. GitLab + Jenkins + Sonar Qube 持续集成

    GitLab + Jenkins + Sonar Qube 持续集成 一. 持续集成 二. 持续交付 三. 持续部署 四. 持续集成的实施流程 五. gitlab 5.1 gitlab安装配置 5.1 ...

  5. 使用Jenkins来实现内部的持续集成流程(下)

    目录 配置项目构建 添加任务 添加源代码地址和登录凭据 添加构建触发器  TFS添加WebHook  添加构建步骤 后端UI  API端  配置项目构建1添加任务 2添加源代码地址和登录凭据 添加源代 ...

  6. 「Jenkins+Git+Maven+Shell+Tomcat持续集成」经典教程

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 作者:mousycoder(极简小站) 来源地址:segmentf ...

  7. Jenkins+Maven+SVN快速搭建持续集成环境

    Jenkins是一个可扩展的持续集成引擎,Jenkins非常易于安装和配置,简单易用,下面看看我们是如何几分钟就快速搭建一个持续集成环境吧. 假设我们目前已经有2个maven项目:entities(J ...

  8. jenkins+Docker+springcloud微服务持续集成

    流程说明 本地部署 微服务是可以独立部署的,如果不借助spring-boot-maven插件,package出来的jar包是不能运行的. 借助spring-boot-maven插件package出来的 ...

  9. jenkins 新手入门安装失败_树莓派安装jenkins,安安静静做个持续集成打包机

    对于java程序来说,手动发布服务端是耗时和无聊的,jenkins很好地解决了这个问题,闲着也是闲着就在树莓派上装个jenkins帮我们打包发布吧. 这次我们需要安装三个软件: jdk:编辑java程 ...

最新文章

  1. wstring与string相互转换
  2. 生成TFrecord
  3. 计算机专业毕业后能做什么工作?
  4. 字符串转数字测试--知识备忘
  5. 清除浮动的最佳方案:clearfix
  6. 计算机与交互式白板通过USB数据线,选购交互式电子白板注意哪些事项【详细介绍】...
  7. 10位photoshop顶尖设计大师
  8. [Err] 1093 - You can't specify target table 's' for update in FROM clause
  9. Zabbix(简介和ubuntu安装步骤)
  10. aac解码器之郁闷(二)
  11. (八)Java日期工具类DateUtils详解(一)
  12. 标题组合-标题组合软件-免费标题生成组合软件
  13. Java方法变量中的final
  14. 搞懂 PEM、ANS、PFX、P12、p8、CER、X509 等证书相关文件格式 后缀
  15. Mac下安装双系统Linux,苹果电脑MAC如何安装双系统
  16. ExcelVBA收纳箱:以A1单元格内容统一插入批注
  17. 推荐新唐工业级ARM9(NUC972DF62Y),集成64M DDR2,多至11个串口
  18. 加ing形式的单词有哪些_【动词ing变化规律1、直接 ing2、以辅音字母 y结尾的单词,直接 in...-动词ing形式变化规则-英语-贾翘教同学...
  19. linux下删除以-开头的文件
  20. 阿里移动安全陈树华:安全的最高境界是无感知

热门文章

  1. Android public class MyApplication extends MultiDexApplication使用
  2. ftb测试软件,EXFO推出FTB-2光纤测试平台
  3. web项目的创建和发布
  4. Java的值传递解析
  5. ui自动化测试测试报告_您需要了解的有关UI测试的所有信息
  6. spock 集成测试_使用Spock 1.2简化对遗留应用程序的集成测试
  7. java获得电脑性能_Java:使用SingletonStream获得性能
  8. 基于docker微服务架构_使用基于微服务的流架构更好地进行大规模的复杂事件处理(第1部分)...
  9. java与java ee_Java EE 8 MVC:全局异常处理
  10. eclipse clean_Clean Sheet – Windows 10的人体工程学Eclipse主题