apply plugin: 'java' apply plugin: 'groovy' apply from: "$rootDir/gradle/checkstyle.gradle" apply from: "$rootDir/gradle/codenarc.gradle" apply from: "$rootDir/gradle/jacoco.gradle" sourceCompatibility = 1.7 targetCompatibility = 1.7 apply plugin: "io.franzbecker.gradle-lombok" lombok { // optional: values below are the defaults version = versions.lombok sha256 = "" } apply plugin: "net.ltgt.errorprone" tasks.withType(JavaCompile) { options.compilerArgs += ['-Xep:FutureReturnValueIgnored:OFF'] // workaround for: https://github.com/google/error-prone/issues/780 options.compilerArgs += ['-Xep:ParameterName:OFF'] options.compilerArgs += ['-XepDisableWarningsInGeneratedCode'] } apply plugin: "eclipse" eclipse { classpath { downloadSources = true downloadJavadoc = true } } if (configurations.find { it.name == 'jmh' }) { eclipse.classpath.plusConfigurations += [configurations.jmh] } jar { /* Make Jar build fail on duplicate files By default Gradle Jar task can put multiple files with the same name into a Jar. This may lead to confusion. For example if auto-service annotation processing creates files with same name in `scala` and `java` directory this would result in Jar having two files with the same name in it. Which in turn would result in only one of those files being actually considered when that Jar is used leading to very confusing failures. Instead we should 'fail early' and avoid building such Jars. */ duplicatesStrategy = 'fail' } task packageSources(type: Jar) { classifier = 'sources' from sourceSets.main.allSource } artifacts.archives packageSources repositories { mavenLocal() jcenter() mavenCentral() } dependencies { testCompile deps.junit testCompile group: 'org.assertj', name: 'assertj-core', version: '2.9.+' testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.0' testCompile deps.spock testCompile deps.groovy testCompile deps.testLogging testCompile 'info.solidsoft.spock:spock-global-unroll:0.5.1' testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.17.1' testCompile group: 'com.anotherchrisberry', name: 'spock-retry', version: '0.6.4' } tasks.withType(Javadoc) { options.encoding = "utf-8" options.docEncoding = "utf-8" options.charSet = "utf-8" options.addStringOption('Xdoclint:none', '-quiet') doFirst { if (project.ext.has("apiLinks")) { options.links(*project.apiLinks) } } } javadoc { source = sourceSets.main.allJava classpath = configurations.compileClasspath options { setMemberLevel JavadocMemberLevel.PUBLIC setAuthor true links "https://docs.oracle.com/javase/8/docs/api/" } } task sourceJar(type: Jar) { from sourceSets.main.allJava classifier = 'sources' } task javaDocJar(type: Jar, dependsOn: javadoc) { from javadoc.destinationDir classifier = 'javadoc' } artifacts { archives sourceJar archives javaDocJar } project.afterEvaluate { if (project.plugins.hasPlugin('org.unbroken-dome.test-sets') && configurations.hasProperty("latestDepTestRuntime")) { tasks.withType(Test) { doFirst { def testArtifacts = configurations.testRuntime.resolvedConfiguration.resolvedArtifacts def latestTestArtifacts = configurations.latestDepTestRuntime.resolvedConfiguration.resolvedArtifacts assert testArtifacts != latestTestArtifacts: "latestDepTest dependencies are identical to test" } } } } if (project.plugins.hasPlugin('com.github.johnrengelman.shadow')) { // Remove the no-deps jar from the archives to prevent publication configurations.archives.with { artifacts.remove artifacts.find { if (it.hasProperty("delegate")) { it.delegate.archiveTask.is jar } else { it.archiveTask.is jar } } } artifacts { archives shadowJar } } if (project.hasProperty("removeJarVersionNumbers") && removeJarVersionNumbers) { tasks.withType(AbstractArchiveTask) { version = null } } if (project.parent && project.parent.hasProperty("javaExecutableVersionCache")) { project.ext.javaExecutableVersionCache = project.parent.ext.javaExecutableVersionCache } else { project.ext.javaExecutableVersionCache = [:] } def getJavaExecutableVersion(String path) { def cache = project.ext.javaExecutableVersionCache if (cache.containsKey(path)) { return cache.get(path) } new ByteArrayOutputStream().withStream { stream -> exec { commandLine = [path, "-version"] errorOutput = stream } def matcher = stream.toString() =~ /^(?:java|openjdk) version "([^"]+)"/ if (matcher) { def version = JavaVersion.toVersion(matcher.group(1)) cache.put(path, version) return version } else { throw new GradleScriptException("Cannot determine java version: ${stream.toString}") } } } def isJavaVersionAllowed(JavaVersion version) { if (project.hasProperty('minJavaVersionForTests') && project.getProperty('minJavaVersionForTests').compareTo(version) > 0) { return false } if (project.hasProperty('maxJavaVersionForTests') && project.getProperty('maxJavaVersionForTests').compareTo(version) < 0) { return false } return true } // JVM names we would like to run complete test suite on // Note: complete test suite is always run on JVM used for compilation // Note2: apparently there is no way to have a 'global' variable, so instead we have per project // attribute that has same value in all projects project.ext.majorSupportedJVMs = ["7", "11"] def isTestingEnabled(String javaName) { if (javaName in project.majorSupportedJVMs) { return true } if (project.findProperty("coreJavaInstrumentation") || project.findProperty("integrationTests")) { return true } return false } // Disable default test tasks if current JVM doesn't match version requirements tasks.withType(Test) { if (name.endsWith("Generated")) { return } // Always run all tests that are runnable on JVM used for compilation onlyIf { isJavaVersionAllowed(JavaVersion.current()) } } // Generate tests tasks for all provided JVMs for (def env : System.getenv().entrySet()) { def matcher = env.key =~ /JAVA_([^_]+)_HOME/ if (!matcher) { continue } def javaName = matcher.group(1) def javaHome = env.value def javaPath = "$javaHome/bin/java" def javaVersion = getJavaExecutableVersion(javaPath) def parentTask = task "testJava${javaName}"() { group = 'Verification' description = "Run tests for Java ${javaName}" } tasks.check.dependsOn parentTask tasks.withType(Test) { if (name.endsWith("Generated")) { return } def clonedTask = it def newTask = task "${clonedTask.name}Java${javaName}Generated"(type: clonedTask.class) { description "Runs $clonedTask.name under java ${javaName}" executable = javaPath onlyIf { isJavaVersionAllowed(javaVersion) && isTestingEnabled(javaName) } jacoco { // Disable jacoco for additional JVM tests to speed things up a bit enabled = false } } parentTask.dependsOn newTask } } // Disable all tests if skipTests property was specified if (project.hasProperty("skipTests")) { tasks.withType(Test) { onlyIf { false } } } plugins.withType(BasePlugin) { project.afterEvaluate { def deleteTasks = tasks.withType(Delete) + project.tasks.findByPath('clean') def otherTasks = tasks - deleteTasks otherTasks*.mustRunAfter deleteTasks } }