Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

第 29 章 JaCoCo - Java code coverage tool

目錄

29.1. Maven
29.2. Gradle

29.1. Maven

		
mvn help:describe -Dplugin=org.jacoco:jacoco-maven-plugin -Ddetail		
		
		
		
<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.0</modelVersion>

	<groupId>demo</groupId>
	<artifactId>junit4</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>junit4</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.1</version>
				<configuration>
					<useSystemClassLoader>false</useSystemClassLoader>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>0.8.2</version>
				<executions>
					<execution>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<execution>
						<id>report</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
				</executions>

			</plugin>
		</plugins>
	</build>
	<reporting>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-report-plugin</artifactId>
				<version>2.22.1</version>
			</plugin>
		</plugins>
	</reporting>
</project>
		
		
		

運行 mvn test 便會生成 jacoco 報告

		
neo@MacBook-Pro ~/workspace/junit4 % find target/site/jacoco 
target/site/jacoco
target/site/jacoco/jacoco.csv
target/site/jacoco/demo.junit4
target/site/jacoco/demo.junit4/index.source.html
target/site/jacoco/demo.junit4/index.html
target/site/jacoco/demo.junit4/App.java.html
target/site/jacoco/demo.junit4/App.html
target/site/jacoco/index.html
target/site/jacoco/jacoco-sessions.html
target/site/jacoco/jacoco-resources
target/site/jacoco/jacoco-resources/branchnc.gif
target/site/jacoco/jacoco-resources/sort.js
target/site/jacoco/jacoco-resources/greenbar.gif
target/site/jacoco/jacoco-resources/sort.gif
target/site/jacoco/jacoco-resources/source.gif
target/site/jacoco/jacoco-resources/report.gif
target/site/jacoco/jacoco-resources/prettify.js
target/site/jacoco/jacoco-resources/branchpc.gif
target/site/jacoco/jacoco-resources/branchfc.gif
target/site/jacoco/jacoco-resources/class.gif
target/site/jacoco/jacoco-resources/prettify.css
target/site/jacoco/jacoco-resources/report.css
target/site/jacoco/jacoco-resources/group.gif
target/site/jacoco/jacoco-resources/package.gif
target/site/jacoco/jacoco-resources/redbar.gif
target/site/jacoco/jacoco-resources/up.gif
target/site/jacoco/jacoco-resources/session.gif
target/site/jacoco/jacoco-resources/down.gif
target/site/jacoco/jacoco-resources/method.gif
target/site/jacoco/jacoco-resources/bundle.gif
target/site/jacoco/jacoco.xml