Maven+TravisCI持续集成和Coveralls测试覆盖率检测

之前挖了不少开源项目的坑 ,准备慢慢填起来 .第一步当然从项目管理开始 ,把之前的maven项目弄得更加规整.

Maven

还是延续之前的parent-pom/child-jar的结构
* parent中只留配置 : dependencies/plugins 都替换成 management节点
* 补充scm/issue等节点信息 ,供其他工具集成
* 增加source/javadoc等plugin ,方便第三方使用者使用.
* 增加代码扫描工具(最好是通过IDEA的编写时工具检测更好)


<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>site.koalazoo.cutekoalagroupId>
  <artifactId>cute-koalaartifactId>
  <version>0.1.0version>
  <packaging>pompackaging>

  <name>Cute Koalaname>
  <url>https://github.com/Anddd7/cute-koalaurl>
  <description>Manage your Java Application easiler.description>

  <licenses>
    <license>
      <name>Apache License, Version 2.0name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txturl>
      <distribution>repodistribution>
    license>
  licenses>

  <issueManagement>
    <system>GitHub Issuessystem>
    <url>https://github.com/Anddd7/cute-koala/issuesurl>
  issueManagement>

  <inceptionYear>2017inceptionYear>

  <scm>
    <connection>scm:git:https://github.com/Anddd7/cute-koala.gitconnection>
    <developerConnection>scm:git:[email protected]:Anddd7/cute-koala.gitdeveloperConnection>
    <url>https://github.com/Anddd7/cute-koalaurl>
  scm>

  <developers>
    <developer>
      <id>and777id>
      <name>Eddy Liaoname>
      <email>[email protected]email>
      <roles>
        <role>ownerrole>
        <role>developerrole>
      roles>
      <timezone>+8timezone>
    developer>
  developers>

  <modules>
    <module>koala-coremodule>
    <module>koala-testmodule>
  modules>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <project.build.jdk.v>1.8project.build.jdk.v>

    <junit.v>4.12junit.v>
    <slf4j-log4j12.v>1.7.5slf4j-log4j12.v>
    <lombok.v>1.16.18lombok.v>
    <fastjson.v>1.2.33fastjson.v>
    <mysql-jdbc.v>6.0.6mysql-jdbc.v>
    <guava.v>22.0guava.v>
    <beetl.v>2.7.16beetl.v>
    <snakeyaml.v>1.18snakeyaml.v>
  properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-log4j12artifactId>
        <version>${slf4j-log4j12.v}version>
      dependency>

      <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>${junit.v}version>
        <scope>testscope>
      dependency>

      <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>${lombok.v}version>
        <scope>providedscope>
      dependency>
    dependencies>
  dependencyManagement>

  <build>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.6.1version>
        <configuration>
          <source>${project.build.jdk.v}source>
          <target>${project.build.jdk.v}target>
        configuration>
      plugin>
    plugins>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.pluginsgroupId>
          <artifactId>maven-source-pluginartifactId>
          <version>2.1.2version>
          <executions>
            <execution>
              <id>attach-sourcesid>
              <goals>
                <goal>jar-no-forkgoal>
              goals>
            execution>
          executions>
        plugin>

        <plugin>
          <groupId>org.apache.maven.pluginsgroupId>
          <artifactId>maven-javadoc-pluginartifactId>
          <version>2.10.4version>
          <configuration>
            <quiet>truequiet>
            <notimestamp>truenotimestamp>
            <additionalparam>-Xdoclint:-htmladditionalparam>
            <encoding>${project.build.sourceEncoding}encoding>
            <docencoding>${project.build.sourceEncoding}docencoding>
            <charset>${project.build.sourceEncoding}charset>
            <additionalparam>-XDignore.symbol.fileadditionalparam>
            <additionalparam>-Xdoclint:-htmladditionalparam>
            <linksource>truelinksource>
          configuration>
          <executions>
            <execution>
              <id>attach-javadocsid>
              <goals>
                <goal>jargoal>
              goals>
            execution>
          executions>
        plugin>

        <plugin>
          <groupId>org.projectlombokgroupId>
          <artifactId>lombok-maven-pluginartifactId>
          <version>1.16.18.1version>
          <executions>
            <execution>
              <phase>generate-sourcesphase>
              <goals>
                <goal>delombokgoal>
              goals>
            execution>
          executions>
        plugin>

        <plugin>
          <groupId>org.jacocogroupId>
          <artifactId>jacoco-maven-pluginartifactId>
          <version>0.7.6.201602180812version>
          <executions>
            <execution>
              <id>prepare-agentid>
              <goals>
                <goal>prepare-agentgoal>
              goals>
            execution>
          executions>
        plugin>

        <plugin>
          <groupId>org.eluder.coverallsgroupId>
          <artifactId>coveralls-maven-pluginartifactId>
          <version>4.3.0version>
        plugin>

      plugins>
    pluginManagement>
  build>
project>

持续集成

之前尝试在阿里云上搭Jenkins ,因为内存爆炸(免费机) ,以失败告终.在添加SCM节点时 ,搜索了一下集成的工具 ,发现了 Travis.Travis是一个持续集成的平台 ,自带与GitHub的集成 ,能够检测代码提交并触发继承脚本.

  • 进入官网使用GitHub登录 ,然后选择想要继承的repo
  • 在repo下新建.travis.yml文件 ,写入自动集成触发的脚本
  • push代码到github ,然后就可以到travis首页查看编译情况

coveralls是一个测试报告展示平台 ,能够集成多种测试报告 .也是在使用travis的时候检索到的 ,通过travis持续集成并生成测试报告 ,然后上传到coveralls .


  • 使用GitHub账号登录 ,选择repo
  • 复制repoToken

repoToken是你访问测试报告的凭证 ,因此不能明文上传到github上 ,但是我们又需要在CI中使用这个repoToken.

  • 加密Token
    • 使用travis的对称加密工具(需要ruby gem工具):travis encrypt COVERALLS_TOKEN=xxxxxx
    • 复制加密后的token到.travis.yml文件中
    • 使用变量名访问
  • 在travis脚本中使用token和测试报告生成工具
  • 生成并上传测试报告
  • Java项目官方推荐了一个plugin使用 ,token不能使用明文 ,所以不能直接把token配置在pom中.而是在travis集成时 ,把解密后的token拿来使用.

    <build>
        <plugins>
        
          <plugin>
            <groupId>org.jacocogroupId>
            <artifactId>jacoco-maven-pluginartifactId>
          plugin>
          <plugin>
            <groupId>org.eluder.coverallsgroupId>
            <artifactId>coveralls-maven-pluginartifactId>
          plugin>
        plugins>
      build>
    language: java
    sudo: false
    script: mvn clean verify
    after_success:
      - mvn clean test
      ## 生成测试报告
      - mvn jacoco:report
      ## 获取travis解密的token ,并调用coveralls-plugin上传报告
      - mvn coveralls:report -DrepoToken="${COVERALLS_TOKEN}"
    
    ## 加密后的token字串
    env:
      global:
        - secure: "..............................."

    最后

    就可以在主页上看到持续集成和测试报告的结果 ,还可以导入非常有逼格的budge

    Maven+TravisCI持续集成和Coveralls测试覆盖率检测_第1张图片

    Maven+TravisCI持续集成和Coveralls测试覆盖率检测_第2张图片

    你可能感兴趣的:(Java)