source: build.gradle @ 967dde4

Last change on this file since 967dde4 was 967dde4, checked in by str4d <str4d@…>, 18 months ago

Gradle: Generate reproducible archives

  • Property mode set to 100644
File size: 5.2 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    tasks.withType(AbstractArchiveTask) {
86        preserveFileTimestamps = false
87        reproducibleFileOrder = true
88    }
89
90    def i2pBootClasspath
91    // Set java7BootClasspath=/path/to/rt.jar:/path/to/jce.jar in ~/.gradle/gradle.properties if needed
92    if (java7BootClasspath) {
93        i2pBootClasspath = java7BootClasspath
94    } else {
95        def java7Home = System.getenv("JAVA7_HOME")
96        if (java7Home) {
97            i2pBootClasspath = "${java7Home}/jre/lib/jce.jar:${java7Home}/jre/lib/rt.jar"
98        }
99    }
100
101    if (i2pBootClasspath) {
102        tasks.withType(AbstractCompile, { AbstractCompile ac ->
103            ac.options.bootstrapClasspath = files(i2pBootClasspath)
104        })
105    } else {
106        if (JavaVersion.current().java8Compatible && !JavaVersion.current().java9Compatible) {
107            throw new GradleException("Set java7BootClasspath property or JAVA7_HOME environment variable to enable cross-compilation, or run Gradle with JDK 9+")
108        }
109        project.afterEvaluate {
110            tasks.withType(JavaCompile) {
111                def version = compat(sourceCompatibility)
112                logger.info("Configuring $name to use --release $version")
113                options.compilerArgs.addAll(['--release', version])
114            }
115        }
116    }
117
118    // Set up Java override if configured (used to test with Java 7).
119    def targetJavaHome = System.getenv("TARGET_JAVA_HOME")
120    if (targetJavaHome) {
121        if (JavaVersion.current().java9Compatible) {
122            throw new GradleException("Only set TARGET_JAVA_HOME with JDK 8")
123        }
124
125        project.afterEvaluate {
126            logger.info("Target Java home set to ${targetJavaHome}")
127            logger.info("Configuring Gradle to use forked compilation and testing")
128
129            tasks.withType(JavaCompile) {
130                options.fork = true
131                options.forkOptions.javaHome = file(targetJavaHome)
132            }
133
134            tasks.withType(Javadoc) {
135                executable = javaExecutable(targetJavaHome, "javadoc")
136            }
137
138            tasks.withType(Test) {
139                executable = javaExecutable(targetJavaHome, "java")
140            }
141
142            tasks.withType(JavaExec) {
143                executable = javaExecutable(targetJavaHome, "java")
144            }
145        }
146    }
147}
148
149task codeCoverageReport(type: JacocoReport) {
150    dependsOn(javaProjects.test)
151
152    jacocoClasspath = project(':core').configurations.jacocoAnt
153    additionalSourceDirs.from(files(javaProjects.sourceSets.main.allSource.srcDirs))
154    sourceDirectories.from(files(javaProjects.sourceSets.main.allSource.srcDirs))
155    classDirectories.from(files(javaProjects.sourceSets.main.output))
156    executionData.from(files(javaProjects.jacocoTestReport.executionData))
157
158    doFirst {
159        executionData = files(executionData.findAll { it.exists() })
160    }
161
162    reports {
163        xml.enabled true
164        xml.destination file("${buildDir}/reports/jacoco/report.xml")
165        html.enabled true
166        html.destination file("${buildDir}/reports/jacoco/html")
167    }
168}
169
170//apply from: file('gradle/update.gradle')
Note: See TracBrowser for help on using the repository browser.