source: build.gradle @ f04b41c

Last change on this file since f04b41c was fd311c7, checked in by str4d <str4d@…>, 14 months ago

Gradle: Fix getBuiltBy() to work when override.properties does not exist

  • Property mode set to 100644
File size: 6.8 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 getBuiltBy() {
36    def builtBy = ""
37    def overrideProps = file("override.properties")
38    if (overrideProps.exists()) {
39        overrideProps.readLines().findAll({ line ->
40            line.contains("build.built-by")
41        }).first().eachMatch('.*=(.*)', {
42            builtBy = it[1]
43        })
44    }
45    builtBy
46}
47
48boolean haveMonotone() {
49    file("_MTN").exists()
50}
51
52String getWorkspaceVersion() {
53    if (haveMonotone()) {
54        def stdout = new ByteArrayOutputStream()
55        exec {
56            executable 'mtn'
57            args 'automate', 'get_base_revision_id'
58            standardOutput = stdout
59        }
60        stdout.toString().trim()
61    } else {
62        'unknown'
63    }
64}
65
66String compat(String src) {
67    if (src.contains('.')) {
68        src.substring(src.lastIndexOf('.') + 1)
69    } else {
70        src
71    }
72}
73
74String javaExecutable(String targetJavaHome, String execName) {
75    def javaExecutablesPath = new File(targetJavaHome, "bin")
76    def executable = new File(javaExecutablesPath, execName)
77    if (!executable.exists()) {
78        throw new IllegalArgumentException("There is no ${execName} executable in ${javaExecutablesPath}")
79    }
80    executable.toString()
81}
82
83def releaseVersion = getReleaseVersion()
84def buildVersion = getBuildVersion()
85def buildExtra = getBuildExtra()
86def fullVersion = "$releaseVersion-$buildVersion$buildExtra"
87
88def builtBy = getBuiltBy()
89def workspaceVersion = getWorkspaceVersion()
90
91// Exclude apps/ dir itself, but include its subdirs
92def javaProjects = subprojects - project(':apps')
93
94configure(javaProjects) {
95    apply plugin: 'java'
96    apply plugin: 'jacoco'
97    apply plugin: 'eclipse'
98    apply plugin: 'idea'
99
100    repositories {
101        jcenter()
102    }
103
104    dependencies {
105        testCompile 'junit:junit:4.+'
106        testCompile 'org.hamcrest:hamcrest-library:1.3'
107        testCompile 'org.mockito:mockito-core:2.5.0'
108    }
109
110    sourceCompatibility = 1.7
111    targetCompatibility = 1.7
112
113    jar {
114        // Empty attributes are set by each project. They are initialized
115        // here in order to create a defined ordering of the attributes.
116        manifest {
117            attributes 'Specification-Title': ''
118            attributes 'Specification-Version': "$releaseVersion"
119            attributes 'Specification-Vendor': 'The I2P Project https://geti2p.net/'
120            attributes 'Implementation-Title': ''
121            attributes 'Implementation-Version': "$fullVersion"
122            attributes 'Implementation-Vendor': 'The I2P Project https://geti2p.net/'
123            attributes 'Built-By': "$builtBy"
124            attributes 'Build-Date': 'reproducible'
125            attributes 'Base-Revision': "$workspaceVersion"
126            attributes 'Workspace-Changes': ''
127            attributes 'X-Compile-Source-JDK': "$sourceCompatibility"
128            attributes 'X-Compile-Target-JDK': "$targetCompatibility"
129        }
130    }
131
132    tasks.withType(AbstractArchiveTask) {
133        preserveFileTimestamps = false
134        reproducibleFileOrder = true
135    }
136
137    def i2pBootClasspath
138    // Set java7BootClasspath=/path/to/rt.jar:/path/to/jce.jar in ~/.gradle/gradle.properties if needed
139    if (java7BootClasspath) {
140        i2pBootClasspath = java7BootClasspath
141    } else {
142        def java7Home = System.getenv("JAVA7_HOME")
143        if (java7Home) {
144            i2pBootClasspath = "${java7Home}/jre/lib/jce.jar:${java7Home}/jre/lib/rt.jar"
145        }
146    }
147
148    if (i2pBootClasspath) {
149        tasks.withType(AbstractCompile, { AbstractCompile ac ->
150            ac.options.bootstrapClasspath = files(i2pBootClasspath)
151        })
152    } else {
153        if (JavaVersion.current().java8Compatible && !JavaVersion.current().java9Compatible) {
154            throw new GradleException("Set java7BootClasspath property or JAVA7_HOME environment variable to enable cross-compilation, or run Gradle with JDK 9+")
155        }
156        project.afterEvaluate {
157            tasks.withType(JavaCompile) {
158                def version = compat(sourceCompatibility)
159                logger.info("Configuring $name to use --release $version")
160                options.compilerArgs.addAll(['--release', version])
161            }
162        }
163    }
164
165    // Set up Java override if configured (used to test with Java 7).
166    def targetJavaHome = System.getenv("TARGET_JAVA_HOME")
167    if (targetJavaHome) {
168        if (JavaVersion.current().java9Compatible) {
169            throw new GradleException("Only set TARGET_JAVA_HOME with JDK 8")
170        }
171
172        project.afterEvaluate {
173            logger.info("Target Java home set to ${targetJavaHome}")
174            logger.info("Configuring Gradle to use forked compilation and testing")
175
176            tasks.withType(JavaCompile) {
177                options.fork = true
178                options.forkOptions.javaHome = file(targetJavaHome)
179            }
180
181            tasks.withType(Javadoc) {
182                executable = javaExecutable(targetJavaHome, "javadoc")
183            }
184
185            tasks.withType(Test) {
186                executable = javaExecutable(targetJavaHome, "java")
187            }
188
189            tasks.withType(JavaExec) {
190                executable = javaExecutable(targetJavaHome, "java")
191            }
192        }
193    }
194}
195
196task codeCoverageReport(type: JacocoReport) {
197    dependsOn(javaProjects.test)
198
199    jacocoClasspath = project(':core').configurations.jacocoAnt
200    additionalSourceDirs.from(files(javaProjects.sourceSets.main.allSource.srcDirs))
201    sourceDirectories.from(files(javaProjects.sourceSets.main.allSource.srcDirs))
202    classDirectories.from(files(javaProjects.sourceSets.main.output))
203    executionData.from(files(javaProjects.jacocoTestReport.executionData))
204
205    doFirst {
206        executionData = files(executionData.findAll { it.exists() })
207    }
208
209    reports {
210        xml.enabled true
211        xml.destination file("${buildDir}/reports/jacoco/report.xml")
212        html.enabled true
213        html.destination file("${buildDir}/reports/jacoco/html")
214    }
215}
216
217//apply from: file('gradle/update.gradle')
Note: See TracBrowser for help on using the repository browser.