본문 바로가기

개발

JacCoCo With Java Maven, Gradle 설정

728x90

JaCoCo는 Java 로 작성된 Code 의 Coverage를 구현하는 데에 사용되는 오픈 소스 라이브러리로 Java, Android 프로젝트에서 Code Coverage 를 측정하는데 사용 한다.

 

과거 EMMA 라는 Coda Coverage 측정 라이브러리의 업데이트가 중단 된 이후에 EclEmma 이클립스 프로젝트에서 EMMA의 대안으로 개발된 라이브러리다.

 

JacCoCo 는 일반적으로 측정하는 Line, Branch Code Coverage 외에 특이하게도 Method Coverage 를 제공한다.

 

Code Coverage : Line, Branch + Method Coverage 측정 가능

JacCoCo 로 Code Coverage 를 측정 하기 위한 Gradle Build, Maven Pom 파일 설정은 아래 내용을 참고하여 구성하면 된다.

 

 

Maven Goal 설명

  • gradlew jacocoTestReport ⇒ Unit 테스트 실행 후 JaCoCo Code Coverage 측정
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"

    classDirectories = fileTree(
            dir: "${project.buildDir}/intermediates/classes/debug/ml/jjeaby/",
            includes: [
                    '**/**/*',
            ],
            excludes: ['**/ApplicationModule.class',
                       '**/ReceptionApplication.class',
                       '**/ObjectGraphWrapper.class',
                       '**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/*$ModuleAdapter*.*',
                       '**/*$InjectAdapter*.*',
                       '**/*$ViewBinder*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']
    )

    def coverageSourceDirs = ["../app/src/main/java"]
    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("${project.buildDir}/jacoco/testDebugUnitTest.exec")

    reports {
        xml {
            enabled true
            destination "${project.buildDir}/reports/jacoco/jacoco.xml"
        }
        csv.enabled false
        html {
            enabled true
            destination "${project.buildDir}/reports/jacoco/html"
        }
    }
}

 

 

 

Maven Goal 설명

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.springframework.samples</groupId>
	<artifactId>spring-petclinic</artifactId>
	<version>1.0.0-SNAPSHOT</version>

	<name>petclinic</name>
	<packaging>war</packaging>

	<properties>

		<!-- Generic properties -->
		<java.version>1.7</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

		<!-- Spring -->
		<spring-io-platform.version>2.0.1.RELEASE</spring-io-platform.version>
		<spring-data-jdbc.version>1.1.0.RELEASE</spring-data-jdbc.version>


		<!-- Java EE / Java SE dependencies -->
		<tomcat.version>7.0.59</tomcat.version>

		<!-- Test -->
		<assertj.version>2.2.0</assertj.version>

		<!-- Dates -->
		<jodatime-hibernate.version>1.3</jodatime-hibernate.version>
		<jodatime-jsptags.version>1.1.1</jodatime-jsptags.version>
		<jadira-usertype-core.version>3.2.0.GA</jadira-usertype-core.version>

		<!-- Others -->
		<mysql-driver.version>5.1.36</mysql-driver.version>

		<!-- Web dependencies -->
		<webjars-bootstrap.version>2.3.0</webjars-bootstrap.version>
		<webjars-jquery-ui.version>1.10.3</webjars-jquery-ui.version>
		<webjars-jquery.version>2.0.3-1</webjars-jquery.version>
		<dandelion.version>1.1.1</dandelion.version>
		<dandelion.datatables.version>1.1.0</dandelion.datatables.version>

		<!-- test properties -->
		<selenium.version>2.48.2</selenium.version>
		<jacoco.version>0.7.5.201505241946</jacoco.version>

		<skip.integration.tests>false</skip.integration.tests>
		<skip.unit.tests>false</skip.unit.tests>



	</properties>

	<dependencyManagement>
		<!-- Import the maven Spring IO Platform Bill Of Materials (BOM) -->
		<dependencies>
			<dependency>
				<groupId>io.spring.platform</groupId>
				<artifactId>platform-bom</artifactId>
				<version>${spring-io-platform.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.jadira.usertype</groupId>
			<artifactId>usertype.core</artifactId>
			<version>${jadira-usertype-core.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-servlet-api</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>


		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jasper-el</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>javax.servlet.jsp.jstl-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.taglibs</groupId>
			<artifactId>taglibs-standard-jstlel</artifactId>
		</dependency>
		<!-- JSon -->
		<dependency>
			<groupId>com.jayway.jsonpath</groupId>
			<artifactId>json-path</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- SPRING, SPRING, SPRINGITY SPRING -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jdbc-core</artifactId>
			<version>${spring-data-jdbc.version}</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>*</artifactId>
				</exclusion>
				<!-- because Spring Data usually comes with a slightly older version 
					of Spring -->
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<!-- used for EhCacheCacheManager -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!-- Database connection pool See here for more details on commons-dbcp 
			versus tomcat-jdbc: http://blog.ippon.fr/2013/03/13/improving-the-performance-of-the-spring-petclinic-sample-application-part-3-of-5/ -->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jdbc</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!-- Logging with SLF4J & LogBack -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!-- Date and Time -->
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
		</dependency>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time-hibernate</artifactId>
			<version>${jodatime-hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time-jsptags</artifactId>
			<version>${jodatime-jsptags.version}</version>
		</dependency>

		<!-- Databases - Uses HSQL by default -->
		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!-- For MySql only -->
		<!--dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> 
			<version>${mysql-driver.version}</version> </dependency -->

		<!-- HIBERNATE -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- Webjars (static dependencies distributed as JAR files) -->
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>bootstrap</artifactId>
			<version>${webjars-bootstrap.version}</version>
		</dependency>
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>jquery-ui</artifactId>
			<version>${webjars-jquery-ui.version}</version>
		</dependency>
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>jquery</artifactId>
			<version>${webjars-jquery.version}</version>
		</dependency>

		<!-- Test Artifacts -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.assertj</groupId>
			<artifactId>assertj-core</artifactId>
			<version>${assertj.version}</version>
			<scope>test</scope>
		</dependency>

		<!-- Dandelion -->
		<dependency>
			<groupId>com.github.dandelion</groupId>
			<artifactId>dandelion-jsp</artifactId>
			<version>${dandelion.version}</version>
		</dependency>
		<dependency>
			<groupId>com.github.dandelion</groupId>
			<artifactId>datatables-jsp</artifactId>
			<version>${dandelion.datatables.version}</version>
		</dependency>
		<dependency>
			<groupId>com.github.dandelion</groupId>
			<artifactId>datatables-export-itext</artifactId>
			<version>${dandelion.datatables.version}</version>
		</dependency>
		<!-- temporary fix: below dependency should not be necessary as it is a 
			transitive dependency. For some reason we can't build the app unless it is 
			a first level dependency -->
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
		</dependency>

		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>${selenium.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-remote-driver</artifactId>
			<version>${selenium.version}</version>
		</dependency>
		<dependency>
			<groupId>xml-apis</groupId>
			<artifactId>xml-apis</artifactId>
			<version>1.4.01</version>
		</dependency>
	</dependencies>

	<!-- Maven plugin versions are mentioned in order to guarantee the build 
		reproducibility in the long term -->
	<build>
		<defaultGoal>install</defaultGoal>
		<testResources>
			<testResource>
				<!-- declared explicitly so Spring config files can be placed next to 
					their corresponding JUnit test class -->
				<directory>${project.basedir}/src/test/java</directory>
			</testResource>
			<testResource>
				<directory>${project.basedir}/src/test/resources</directory>
			</testResource>
		</testResources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<encoding>${project.build.sourceEncoding}</encoding>
					<compilerArguments>
						<Xlint />
					</compilerArguments>
					<verbose>true</verbose>
					<source>${java.version}</source>
					<target>${java.version}</target>
					<showWarnings>true</showWarnings>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.13</version>
				<configuration>
					<includes>
						<include>**/*Tests.java</include>
					</includes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<warName>petclinic</warName>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>

			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
					<wtpversion>2.0</wtpversion>
					<sourceIncludes>
						<sourceInclude>**/*.*</sourceInclude>
					</sourceIncludes>
					<additionalBuildcommands>
						<buildCommand>
							<name>org.springframework.ide.eclipse.core.springbuilder</name>
						</buildCommand>
						<buildCommand>
							<name>org.eclipse.m2e.core.maven2Builder</name>
						</buildCommand>
					</additionalBuildcommands>
					<additionalProjectnatures>
						<projectnature>org.eclipse.jdt.core.javanature</projectnature>
						<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
						<projectnature>org.eclipse.m2e.core.maven2Nature</projectnature>
					</additionalProjectnatures>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<server>tomcat-development-server</server>
					<port>9966</port>
					<path>/petclinic</path>
				</configuration>

				<executions>
					<execution>
						<id>start-tomcat</id>
						<phase>pre-integration-test</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<fork>true</fork>
						</configuration>
					</execution>
					<execution>
						<id>stop-tomcat</id>
						<phase>post-integration-test</phase>
						<goals>
							<goal>shutdown</goal>
						</goals>
					</execution>
				</executions>

			</plugin>


			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>${jacoco.version}</version>
				<executions>
					<!-- Prepares the property pointing to the JaCoCo runtime agent which 
						is passed as VM argument when Maven the Surefire plugin is executed. -->
					<execution>
						<id>pre-unit-test</id>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
						<configuration>
							<!-- Sets the path to the file which contains the execution data. -->
							<destFile>${jacoco.ut.execution.data.file}</destFile>
							<!-- Sets the name of the property containing the settings for JaCoCo 
								runtime agent. -->
							<propertyName>surefireArgLine</propertyName>
						</configuration>
					</execution>
					<!-- Ensures that the code coverage report for unit tests is created 
						after unit tests have been run. -->
					<execution>
						<id>post-unit-test</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
						<configuration>
							<!-- Sets the path to the file which contains the execution data. -->
							<dataFile>${jacoco.ut.execution.data.file}</dataFile>
							<!-- Sets the output directory for the code coverage report. -->
							<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
						</configuration>
					</execution>
					<!-- Prepares the property pointing to the JaCoCo runtime agent which 
						is passed as VM argument when Maven the Failsafe plugin is executed. -->
					<execution>
						<id>pre-integration-test</id>
						<phase>pre-integration-test</phase>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
						<configuration>
							<!-- Sets the path to the file which contains the execution data. -->
							<destFile>${jacoco.it.execution.data.file}</destFile>
							<!-- Sets the name of the property containing the settings for JaCoCo 
								runtime agent. -->
							<propertyName>failsafeArgLine</propertyName>
						</configuration>
					</execution>
					<!-- Ensures that the code coverage report for integration tests after 
						integration tests have been run. -->
					<execution>
						<id>post-integration-test</id>
						<phase>post-integration-test</phase>
						<goals>
							<goal>report</goal>
						</goals>
						<configuration>
							<!-- Sets the path to the file which contains the execution data. -->
							<dataFile>${jacoco.it.execution.data.file}</dataFile>
							<!-- Sets the output directory for the code coverage report. -->
							<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<!-- Used for unit tests -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.15</version>
				<configuration>
					<!-- Sets the VM argument line used when unit tests are run. -->
					<argLine>${surefireArgLine}</argLine>
					<!-- Skips unit tests if the value of skip.unit.tests property is true -->
					<skipTests>${skip.unit.tests}</skipTests>
					<!-- Include unit tests are run. -->
					<includes>
						<include>**/*Tests.java</include>
					</includes>
				</configuration>
			</plugin>


			<!-- Used for integration tests -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-failsafe-plugin</artifactId>
				<version>2.15</version>
				<executions>
					<!-- Ensures that both integration-test and verify goals of the Failsafe 
						Maven plugin are executed. -->
					<execution>
						<id>integration-tests</id>
						<goals>
							<goal>integration-test</goal>
							<goal>verify</goal>
						</goals>
						<configuration>

							<!-- Sets the VM argument line used when integration tests are run. -->
							<argLine>${failsafeArgLine} -XX:MaxPermSize=512m</argLine>

							<!-- Skips integration tests if the value of skip.integration.tests 
								property is true -->
							<skipTests>${skip.integration.tests}</skipTests>

							<!-- Include Integration tests are run. -->
							<includes>
								<include>**/*IT.java</include>
							</includes>
						</configuration>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>

</project>