1import org.gradle.api.JavaVersion 2import org.gradle.api.Plugin 3import org.gradle.api.Project 4import org.gradle.api.artifacts.maven.MavenDeployment 5import org.gradle.api.tasks.bundling.Jar 6import org.gradle.api.tasks.compile.JavaCompile 7 8class RoboJavaModulePlugin implements Plugin<Project> { 9 Boolean deploy = false; 10 11 Closure doApply = { 12 apply plugin: "java" 13 sourceCompatibility = JavaVersion.VERSION_1_8 14 targetCompatibility = JavaVersion.VERSION_1_8 15 16 tasks.withType(JavaCompile) { task -> 17 sourceCompatibility = JavaVersion.VERSION_1_8 18 targetCompatibility = JavaVersion.VERSION_1_8 19 20 // Show all warnings except boot classpath 21 configure(options) { 22 if (System.properties["lint"] != null && System.properties["lint"] != "false") { 23 compilerArgs << "-Xlint:all" // Turn on all warnings 24 } 25 compilerArgs << "-Xlint:-options" // Turn off "missing" bootclasspath warning 26 encoding = "utf-8" // Make sure source encoding is UTF-8 27 incremental = true 28 } 29 30 def noRebuild = System.getenv('NO_REBUILD') == "true" 31 if (noRebuild) { 32 println "[NO_REBUILD] $task will be skipped!" 33 task.outputs.upToDateWhen { true } 34 task.onlyIf { false } 35 } 36 } 37 38 // it's weird that compileOnly deps aren't included for test compilation; fix that: 39 project.sourceSets { 40 test.compileClasspath += project.configurations.compileOnly 41 } 42 43 ext.mavenArtifactName = project.path.substring(1).split(/:/).join("-") 44 45 task('provideBuildClasspath', type: ProvideBuildClasspathTask) { 46 File outDir = project.sourceSets['test'].output.resourcesDir 47 outFile = new File(outDir, 'robolectric-deps.properties') 48 } 49 50 test { 51 dependsOn provideBuildClasspath 52 53 exclude "**/*\$*" // otherwise gradle runs static inner classes like TestRunnerSequenceTest$SimpleTest 54 testLogging { 55 exceptionFormat "full" 56 showCauses true 57 showExceptions true 58 showStackTraces true 59 showStandardStreams true 60 events = ["failed", "skipped"] 61 } 62 63 minHeapSize = "1024m" 64 maxHeapSize = "3172m" 65 66 if (System.env['GRADLE_MAX_PARALLEL_FORKS'] != null) { 67 maxParallelForks = Integer.parseInt(System.env['GRADLE_MAX_PARALLEL_FORKS']) 68 } 69 70 def forwardedSystemProperties = System.properties 71 .findAll { k,v -> k.startsWith("robolectric.") } 72 .collect { k,v -> "-D$k=$v" } 73 jvmArgs = ["-XX:MaxPermSize=1024m"] + forwardedSystemProperties 74 75 doFirst { 76 if (!forwardedSystemProperties.isEmpty()) { 77 println "Running tests with ${forwardedSystemProperties}" 78 } 79 } 80 81 rootProject.tasks['aggregateTestReports'].reportOn binResultsDir 82 finalizedBy ':aggregateTestReports' 83 } 84 85 if (owner.deploy) { 86 project.apply plugin: "signing" 87 project.apply plugin: "maven" 88 project.apply plugin: 'ch.raffael.pegdown-doclet' 89 90 task('sourcesJar', type: Jar, dependsOn: classes) { 91 classifier "sources" 92 from sourceSets.main.allJava 93 } 94 95 javadoc { 96 failOnError = false 97 source = sourceSets.main.allJava 98 } 99 100 task('javadocJar', type: Jar, dependsOn: javadoc) { 101 classifier "javadoc" 102 from javadoc.destinationDir 103 } 104 105 signing { 106 required { !version.endsWith("SNAPSHOT") && gradle.taskGraph.hasTask("uploadArchives") } 107 sign configurations.archives 108 } 109 110 def skipJavadoc = System.getenv('SKIP_JAVADOC') == "true" 111 artifacts { 112 archives jar 113 archives sourcesJar 114 if (!skipJavadoc) { 115 archives javadocJar 116 } 117 } 118 119 uploadArchives { 120 repositories { 121 mavenDeployer { 122 pom.artifactId = mavenArtifactName 123 pom.project { 124 name project.name 125 description = "An alternative Android testing framework." 126 url = "http://robolectric.org" 127 128 licenses { 129 license { 130 name "The MIT License" 131 url "https://opensource.org/licenses/MIT" 132 } 133 } 134 135 scm { 136 url "git@github.com:robolectric/robolectric.git" 137 connection "scm:git:git://github.com/robolectric/robolectric.git" 138 developerConnection "scm:git:https://github.com/robolectric/robolectric.git" 139 } 140 141 developers { 142 developer { 143 name "Christian Williams" 144 email "christianw@google.com" 145 organization = "Google Inc." 146 organizationUrl "http://google.com" 147 } 148 149 developer { 150 name "Jonathan Gerrish" 151 email "jongerrish@google.com" 152 organization = "Google Inc." 153 organizationUrl "http://google.com" 154 } 155 } 156 } 157 158 def url = project.version.endsWith("-SNAPSHOT") ? 159 "https://oss.sonatype.org/content/repositories/snapshots" : 160 "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 161 repository(url: url) { 162 authentication( 163 userName: System.properties["sonatype-login"] ?: System.env['sonatypeLogin'], 164 password: System.properties["sonatype-password"] ?: System.env['sonatypePassword'] 165 ) 166 } 167 168 beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 169 } 170 } 171 } 172 } 173 } 174 175 @Override 176 void apply(Project project) { 177 doApply.delegate = project 178 doApply.resolveStrategy = Closure.DELEGATE_ONLY 179 doApply() 180 } 181}