source: build.gradle @ 10bae6a

Last change on this file since 10bae6a was 4f6b58e, checked in by str4d <str4d@…>, 17 months ago

Gradle: Configure —release compiler flag after project evaluation

  • Property mode set to 100644
File size: 5.1 KB
Line 
1plugins {
2    id 'idea'
3}
4
5String getReleaseVersion() {
6    def releaseVersion
7    file("core/java/src/net/i2p/CoreVersion.java").readLines().findAll({ line ->
8        line.contains("public final static String VERSION")
9    }).first().eachMatch('.*"([^"]+)";', {
10        releaseVersion = it[1]
11    })
12    releaseVersion
13}
14
15String getBuildVersion() {
16    def buildVersion
17    file("router/java/src/net/i2p/router/RouterVersion.java").readLines().findAll({ line ->
18        line.contains("public final static long BUILD")
19    }).first().eachMatch('.*=\\s+([0-9]+);', {
20        buildVersion = it[1]
21    })
22    buildVersion
23}
24
25String getBuildExtra() {
26    def buildExtra
27    file("router/java/src/net/i2p/router/RouterVersion.java").readLines().findAll({ line ->
28        line.contains("public final static String EXTRA")
29    }).first().eachMatch('.*"(.*)";', {
30        buildExtra = it[1]
31    })
32    buildExtra
33}
34
35String compat(String src) {
36    if (src.contains('.')) {
37        src.substring(src.lastIndexOf('.') + 1)
38    } else {
39        src
40    }
41}
42
43String javaExecutable(String targetJavaHome, String execName) {
44    def javaExecutablesPath = new File(targetJavaHome, "bin")
45    def executable = new File(javaExecutablesPath, execName)
46    if (!executable.exists()) {
47        throw new IllegalArgumentException("There is no ${execName} executable in ${javaExecutablesPath}")
48    }
49    executable.toString()
50}
51
52def releaseVersion = getReleaseVersion()
53def buildVersion = getBuildVersion()
54def buildExtra = getBuildExtra()
55def fullVersion = "$releaseVersion-$buildVersion$buildExtra"
56
57// Exclude apps/ dir itself, but include its subdirs
58def javaProjects = subprojects - project(':apps')
59
60configure(javaProjects) {
61    apply plugin: 'java'
62    apply plugin: 'jacoco'
63    apply plugin: 'eclipse'
64    apply plugin: 'idea'
65
66    repositories {
67        jcenter()
68    }
69
70    dependencies {
71        testCompile 'junit:junit:4.+'
72        testCompile 'org.hamcrest:hamcrest-library:1.3'
73        testCompile 'org.mockito:mockito-core:2.11.0'
74    }
75
76    jar {
77        manifest {
78            attributes 'Implementation-Version': "$fullVersion"
79        }
80    }
81
82    sourceCompatibility = 1.7
83    targetCompatibility = 1.7
84
85    def i2pBootClasspath
86    // Set java7BootClasspath=/path/to/rt.jar:/path/to/jce.jar in ~/.gradle/gradle.properties if needed
87    if (java7BootClasspath) {
88        i2pBootClasspath = java7BootClasspath
89    } else {
90        def java7Home = System.getenv("JAVA7_HOME")
91        if (java7Home) {
92            i2pBootClasspath = "${java7Home}/jre/lib/jce.jar:${java7Home}/jre/lib/rt.jar"
93        }
94    }
95
96    if (i2pBootClasspath) {
97        tasks.withType(AbstractCompile, { AbstractCompile ac ->
98            ac.options.bootstrapClasspath = files(i2pBootClasspath)
99        })
100    } else {
101        if (JavaVersion.current().java8Compatible && !JavaVersion.current().java9Compatible) {
102            throw new GradleException("Set java7BootClasspath property or JAVA7_HOME environment variable to enable cross-compilation, or run Gradle with JDK 9+")
103        }
104        project.afterEvaluate {
105            tasks.withType(JavaCompile) {
106                def version = compat(sourceCompatibility)
107                logger.info("Configuring $name to use --release $version")
108                options.compilerArgs.addAll(['--release', version])
109            }
110        }
111    }
112
113    // Set up Java override if configured (used to test with Java 7).
114    def targetJavaHome = System.getenv("TARGET_JAVA_HOME")
115    if (targetJavaHome) {
116        if (JavaVersion.current().java9Compatible) {
117            throw new GradleException("Only set TARGET_JAVA_HOME with JDK 8")
118        }
119
120        project.afterEvaluate {
121            logger.info("Target Java home set to ${targetJavaHome}")
122            logger.info("Configuring Gradle to use forked compilation and testing")
123
124            tasks.withType(JavaCompile) {
125                options.fork = true
126                options.forkOptions.javaHome = file(targetJavaHome)
127            }
128
129            tasks.withType(Javadoc) {
130                executable = javaExecutable(targetJavaHome, "javadoc")
131            }
132
133            tasks.withType(Test) {
134                executable = javaExecutable(targetJavaHome, "java")
135            }
136
137            tasks.withType(JavaExec) {
138                executable = javaExecutable(targetJavaHome, "java")
139            }
140        }
141    }
142}
143
144task codeCoverageReport(type: JacocoReport) {
145    dependsOn(javaProjects.test)
146
147    jacocoClasspath = project(':core').configurations.jacocoAnt
148    additionalSourceDirs = files(javaProjects.sourceSets.main.allSource.srcDirs)
149    sourceDirectories = files(javaProjects.sourceSets.main.allSource.srcDirs)
150    classDirectories = files(javaProjects.sourceSets.main.output)
151    executionData = files(javaProjects.jacocoTestReport.executionData)
152
153    doFirst {
154        executionData = files(executionData.findAll { it.exists() })
155    }
156
157    reports {
158        xml.enabled true
159        xml.destination file("${buildDir}/reports/jacoco/report.xml")
160        html.enabled true
161        html.destination file("${buildDir}/reports/jacoco/html")
162    }
163}
164
165//apply from: file('gradle/update.gradle')
Note: See TracBrowser for help on using the repository browser.