Skip to content

Commit 4ac62af

Browse files
committed
Build: Assert jar LICENSE and NOTICE files match
Adds tasks that check that the all jars that we build have LICENSE.txt and NOTICE.txt files and that the files are correct. Sets check to depend on these task. This is mostly there for extra parnoia because we automatically configure all Jar tasks to include the LICENSE.txt and NOTICE.txt files anyway. But it is quite possible to add configuration to those tasks that would override either file. This causes check to depend on several more things than it used to. Take, for example, javadoc: check depends on the new verifyJavadocJarNotice which depends on extractJavadocJar which depends on javadocJar which depends on javadoc, this check now depends on javadoc.
1 parent b06b9e4 commit 4ac62af

File tree

7 files changed

+80
-27
lines changed

7 files changed

+80
-27
lines changed

build.gradle

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.tools.ant.taskdefs.condition.Os
2222
import org.elasticsearch.gradle.BuildPlugin
23+
import org.elasticsearch.gradle.LoggedExec
2324
import org.elasticsearch.gradle.Version
2425
import org.elasticsearch.gradle.VersionCollection
2526
import org.elasticsearch.gradle.VersionProperties
@@ -30,6 +31,7 @@ import org.gradle.api.tasks.wrapper.Wrapper.DistributionType
3031
import org.gradle.util.GradleVersion
3132
import org.gradle.util.DistributionLocator
3233

34+
import java.nio.file.Files
3335
import java.nio.file.Path
3436
import java.security.MessageDigest
3537

@@ -463,6 +465,59 @@ gradle.projectsEvaluated {
463465

464466
}
465467

468+
static void assertLinesInFile(final Path path, final List<String> expectedLines) {
469+
final List<String> actualLines = Files.readAllLines(path)
470+
int line = 0
471+
for (final String expectedLine : expectedLines) {
472+
final String actualLine = actualLines.get(line)
473+
if (expectedLine != actualLine) {
474+
throw new GradleException("expected line [${line + 1}] in [${path}] to be [${expectedLine}] but was [${actualLine}]")
475+
}
476+
line++
477+
}
478+
}
479+
480+
/*
481+
* Check that all generated JARs have our NOTICE.txt and an appropriate
482+
* LICENSE.txt in them. We configurate this in gradle but we'd like to
483+
* be extra paranoid.
484+
*/
485+
subprojects { project ->
486+
project.tasks.withType(Jar).whenTaskAdded { jarTask ->
487+
final Task extract = project.task("extract${jarTask.name.capitalize()}", type: LoggedExec) {
488+
dependsOn jarTask
489+
ext.destination = project.buildDir.toPath().resolve("jar-extracted/${jarTask.name}")
490+
commandLine "${->new File(rootProject.compilerJavaHome, 'bin/jar')}",
491+
'xf', "${-> jarTask.outputs.files.singleFile}", 'META-INF/LICENSE.txt', 'META-INF/NOTICE.txt'
492+
workingDir destination
493+
doFirst {
494+
project.delete(destination)
495+
Files.createDirectories(destination)
496+
}
497+
}
498+
499+
final Task checkNotice = project.task("verify${jarTask.name.capitalize()}Notice") {
500+
dependsOn extract
501+
doLast {
502+
final List<String> noticeLines = Files.readAllLines(project.noticeFile.toPath())
503+
final Path noticePath = extract.destination.resolve('META-INF/NOTICE.txt')
504+
assertLinesInFile(noticePath, noticeLines)
505+
}
506+
}
507+
project.check.dependsOn checkNotice
508+
509+
final Task checkLicense = project.task("verify${jarTask.name.capitalize()}License") {
510+
dependsOn extract
511+
doLast {
512+
final List<String> licenseLines = Files.readAllLines(project.licenseFile.toPath())
513+
final Path licensePath = extract.destination.resolve('META-INF/LICENSE.txt')
514+
assertLinesInFile(licensePath, licenseLines)
515+
}
516+
}
517+
project.check.dependsOn checkLicense
518+
}
519+
}
520+
466521
/* Remove assemble on all qa projects because we don't need to publish
467522
* artifacts for them. */
468523
gradle.projectsEvaluated {

distribution/archives/build.gradle

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ subprojects {
201201
}
202202
final List<String> licenseLines = Files.readAllLines(rootDir.toPath().resolve("licenses/" + licenseFilename))
203203
final Path licensePath = archiveExtractionDir.toPath().resolve("elasticsearch-${VersionProperties.elasticsearch}/LICENSE.txt")
204-
final List<String> actualLines = Files.readAllLines(licensePath)
205-
assertLinesInFile(licensePath, actualLines, licenseLines)
204+
assertLinesInFile(licensePath, licenseLines)
206205
}
207206
}
208207
check.dependsOn checkLicense
@@ -213,8 +212,7 @@ subprojects {
213212
doLast {
214213
final List<String> noticeLines = Arrays.asList("Elasticsearch", "Copyright 2009-2018 Elasticsearch")
215214
final Path noticePath = archiveExtractionDir.toPath().resolve("elasticsearch-${VersionProperties.elasticsearch}/NOTICE.txt")
216-
final List<String> actualLines = Files.readAllLines(noticePath)
217-
assertLinesInFile(noticePath, actualLines, noticeLines)
215+
assertLinesInFile(noticePath, noticeLines)
218216
}
219217
}
220218
check.dependsOn checkNotice
@@ -304,4 +302,3 @@ configure(subprojects.findAll { it.name.contains('zip') }) {
304302
}
305303
}
306304
}
307-

distribution/build.gradle

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -460,14 +460,3 @@ subprojects {
460460
return result
461461
}
462462
}
463-
464-
static void assertLinesInFile(final Path path, final List<String> actualLines, final List<String> expectedLines) {
465-
int line = 0
466-
for (final String expectedLine : expectedLines) {
467-
final String actualLine = actualLines.get(line)
468-
if (expectedLine != actualLine) {
469-
throw new GradleException("expected line [${line + 1}] in [${path}] to be [${expectedLine}] but was [${actualLine}]")
470-
}
471-
line++
472-
}
473-
}

distribution/packages/build.gradle

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,7 @@ subprojects {
415415
"License: " + expectedLicense)
416416
final List<String> licenseLines = Files.readAllLines(rootDir.toPath().resolve("licenses/" + licenseFilename))
417417
final List<String> expectedLines = header + licenseLines.collect { " " + it }
418-
final List<String> actualLines = Files.readAllLines(copyrightPath)
419-
assertLinesInFile(copyrightPath, actualLines, expectedLines)
418+
assertLinesInFile(copyrightPath, expectedLines)
420419
}
421420
}
422421
} else {
@@ -432,8 +431,7 @@ subprojects {
432431
}
433432
final List<String> licenseLines = Files.readAllLines(rootDir.toPath().resolve("licenses/" + licenseFilename))
434433
final Path licensePath = packageExtractionDir.toPath().resolve("usr/share/elasticsearch/LICENSE.txt")
435-
final List<String> actualLines = Files.readAllLines(licensePath)
436-
assertLinesInFile(licensePath, actualLines, licenseLines)
434+
assertLinesInFile(licensePath, licenseLines)
437435
}
438436
}
439437
}
@@ -444,8 +442,7 @@ subprojects {
444442
doLast {
445443
final List<String> noticeLines = Arrays.asList("Elasticsearch", "Copyright 2009-2018 Elasticsearch")
446444
final Path noticePath = packageExtractionDir.toPath().resolve("usr/share/elasticsearch/NOTICE.txt")
447-
final List<String> actualLines = Files.readAllLines(noticePath)
448-
assertLinesInFile(noticePath, actualLines, noticeLines)
445+
assertLinesInFile(noticePath, noticeLines)
449446
}
450447
}
451448
check.dependsOn checkNotice

x-pack/build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ subprojects {
2323
ext.licenseName = 'Elastic License'
2424
ext.licenseUrl = "https://raw.githubusercontent.com/elastic/elasticsearch/${licenseCommit}/licenses/ELASTIC-LICENSE.txt"
2525

26-
plugins.withType(BuildPlugin).whenPluginAdded {
27-
project.licenseFile = rootProject.file('licenses/ELASTIC-LICENSE.txt')
28-
project.noticeFile = xpackRootProject.file('NOTICE.txt')
29-
}
26+
project.ext.licenseFile = rootProject.file('licenses/ELASTIC-LICENSE.txt')
27+
project.ext.noticeFile = xpackRootProject.file('NOTICE.txt')
3028

3129
plugins.withType(PluginBuildPlugin).whenPluginAdded {
3230
project.esplugin.licenseFile = rootProject.file('licenses/ELASTIC-LICENSE.txt')

x-pack/plugin/build.gradle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ configurations {
3131
task testJar(type: Jar) {
3232
appendix 'test'
3333
from sourceSets.test.output
34+
/*
35+
* Stick the license and notice file in the jar. This isn't strictly
36+
* needed because we don't publish it but it makes our super-paranoid
37+
* tests happy.
38+
*/
39+
metaInf {
40+
from(project.licenseFile.parent) {
41+
include project.licenseFile.name
42+
rename { 'LICENSE.txt' }
43+
}
44+
from(project.noticeFile.parent) {
45+
include project.noticeFile.name
46+
}
47+
}
3448
}
3549
artifacts {
3650
testArtifacts testJar

x-pack/plugin/sql/sql-cli/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ dependencyLicenses {
4545
* can be easilly shipped around and used.
4646
*/
4747
jar {
48-
from {
48+
from({
4949
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
5050
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
51+
}) {
52+
// We don't need the META-INF from the things we bundle. For now.
53+
exclude 'META-INF/*'
5154
}
5255
manifest {
5356
attributes 'Main-Class': 'org.elasticsearch.xpack.sql.cli.Cli'

0 commit comments

Comments
 (0)