Git Product home page Git Product logo

java-web-app's Introduction

java-web-app - sonarqube

Description Jenkins - SonarQube Integration: Build App. Generate Report.

Prerequisities & Tools

  • Maven/Java App with Unit Tests
  • AWS EC2 Instance - in this case CentOS 8 instance that serves as a host machine for SQ, Jenkins, Docker..
  • Jenkins - CI
  • SonarQube - Static code analysis
  • JaCoCo Plugin - Java code coverage library
  • Docker - Run Jenkins and SQ in Docker containers

How to integrate SQ with Jenkins?

Steps

  1. Install SonarQube Scanner Plugin
  2. Generate Token in SonarQube Server
  • Create user jenkins Administration/Security/Users
  • Generate Token for jenkins user - this token will be used as a Jenkins credential
  1. Configure SQ in Jenkins
  • Manage Jenkins/Configure System -> SonarQube Servers/Add Server URL -> Add Jenkins credential (Server auth token_
  1. Configure SonarQube Scanner
  • Manage Jenkins/Global configuration Tools -> SonarQube Scanner/Install from Maven central

Project Build with Maven

Configuring Plug-ins & Dependencies

  • Using JaCoCo Plugin for code coverage analysis in Java, allows basic report creation.
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <executions>
       <execution>
          <id>jacoco-init</id>
           <goals>
                <goal>prepare-agent</goal>
           </goals>
       </execution>
       <execution>
          <id>report</id>
          <phase>test</phase>
          <goals>
              <goal>report</goal>
          </goals>
       </execution>
    </executions>
</plugin>

In this case, The JaCoCo Maven plug-in defines the following goals:

  1. prepare-agent - initialization phase, to add JaCoCo agent during build
  2. report - generate code coverage report

Issues & How to fix?

  1. Report not generated due to maven surefire plugin & jacoco conflict.

Explained: The prepare-agent goal will add the jacoco agent during build, but will not cause the actual report to be generated after the tests. As a result, the coverage will always be reported as zero.

Reference:

Solution: pom.xml

<properties>
   <surefireArgLine> </surefireArgLine>
</properties>

...

<plugin>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>3.0.0-M5</version>
   <configuration>
       <argLine>${surefireArgLine}</argLine>
   </configuration>
</plugin>
  1. Error "Skipping JaCoCo execution due to missing classes directory" while generating report

Explained Not generating classes directory in target directory during build. The only class in the project is the unit test class. test-classes directory is generated. Nothing to compile.

Solution Copy resources. Bad approach?? Probably

<plugin>
   <artifactId>maven-resources-plugin</artifactId>
   <version>3.2.0</version>
   <executions>
       <execution>
           <id>copy-resources</id>
           <phase>pre-integration-test</phase>
           <goals>
               <goal>copy-resources</goal>
           </goals>
           <configuration>
               <outputDirectory>${basedir}/target/classes</outputDirectory>
               <resources>
                   <resource>
                        <directory>${basedir}/target/test-classes</directory>
                       <filtering>false</filtering>
                   </resource>
               </resources>
           </configuration>
       </execution>
   </executions>
</plugin>

java-web-app's People

Contributors

vmilovanovicc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.