代码拉取完成,页面将自动刷新
// jacoco插件
apply plugin: 'jacoco'
// jpa-entity-generator-enhance中生成JPA Entity的插件
apply plugin: 'entitygen'
// 单元测试配置
ext {
// 单元测试配置文件路径
testConfigFilePath = "src/test/resources/groovyConfig/unit_test_config.groovy"
// 获取测试模式的参数Key
testModeKey = "testMode"
// 获取测试模式
testMode = System.getProperty(testModeKey, "default_mode")
configSlurper = new ConfigSlurper()
configSlurper.registerConditionalBlock("test_mode", testMode)
// 当前使用的测试参数
configObject = configSlurper.parse(file(testConfigFilePath).toURI().toURL())
}
test {
doFirst {
// 设置测试范围
def testInclude = configObject.get("test_include")
if (testInclude != null) {
setIncludes(testInclude)
}
// 设置排除的测试范围
def testExclude = configObject.get("test_exclude")
if (testExclude != null) {
setExcludes(testExclude)
}
minHeapSize = "256m"
maxHeapSize = "1g"
// 指定同时执行的测试类数量,默认为1
maxParallelForks = 1
/*
指定Gradle每个测试进程执行多少个测试类,超过该值后会创建新的进程
默认值为0,即一直使用一个进程执行测试类
*/
forkEvery = 5
jvmArgs "-XX:MetaspaceSize=64m", "-XX:MaxMetaspaceSize=256m"
// 日志配置
testLogging {
// 指定需要记录日志的事件
events "PASSED", "STARTED", "FAILED", "SKIPPED"
}
}
}
processResources {
setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
if (gradleArgsContains("build", "jar", "war")) {
include 'com/**'
}
}
// 在processTestResources任务中可对配置文件内容进行替换
processTestResources {
setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
// 当Gradle执行参数包含test时,执行后续操作
if (gradleArgsContains("test")) {
if (configObject == null) {
println "Unsupported test mode: " + testMode
throw new RuntimeException("Unsupported test mode: " + testMode)
}
println "testMode: " + testMode
println "test configObject: " + configObject
//替换配置文件中的测试参数
from(sourceSets.test.resources) {
include "applicationContext_replace.xml", "base_replace.properties"
filter(org.apache.tools.ant.filters.ReplaceTokens, "tokens": configObject.toProperties())
}
}
doLast {
// 使用替换参数后的配置文件进行覆盖
file('build/resources/test/base.properties').renameTo(file('build/resources/test/base.properties.bak'))
file('build/resources/test/base_replace.properties').renameTo(file('build/resources/test/base.properties'))
file('build/resources/test/applicationContext.xml').renameTo(file('build/resources/test/applicationContext.xml.bak'))
file('build/resources/test/applicationContext_replace.xml').renameTo(file('build/resources/test/applicationContext.xml'))
}
}
jacocoTestReport {
// 当Gradle执行参数包含jacocoTestReport时,执行后续操作
if (gradleArgsContains(getName())) {
def jacocoInclude = configObject.get("jacoco_include")
def jacocoExclude = configObject.get("jacoco_exclude")
// 指定需要输出jacoco代码覆盖率报告的类范围
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree('dir': it,
'include': jacocoInclude,
'exclude': jacocoExclude)
}))
}
reports {
xml.enabled = false
csv.enabled = false
html.enabled = true
html.destination = file("${buildDir}/jacocoReport")
}
}
// jpa-entity-generator-enhance中生成JPA Entity的配置
entityGen {
configPath = 'src/test/resources/entityGenConfig/entityGenConfig.yml'
}
//显示支持的测试模式
task showTestMode {
doFirst {
ConfigObject allConfigObject = new ConfigSlurper().parse(file(testConfigFilePath).toURI().toURL()).get("test_mode")
println "Usage:"
for (Map.Entry entry : allConfigObject.entrySet()) {
println "gradlew test -D" + testModeKey + "=" + entry.getKey()
}
}
}
// 判断Gradle执行的任务是否包含指定任务
boolean gradleArgsContains(String... expectedArgs) {
StartParameter startParameter = gradle.getStartParameter()
if (startParameter.isDryRun()) {
return false
}
List<TaskExecutionRequest> taskExecutionRequestList = startParameter.getTaskRequests()
for (TaskExecutionRequest taskExecutionRequest : taskExecutionRequestList) {
List<String> args = taskExecutionRequest.getArgs()
for (String arg : args) {
for (String expectedArg : expectedArgs) {
if (arg.equals(expectedArg)) {
return true
}
}
}
}
return false
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。