18
18
*/
19
19
package org.elasticsearch.gradle.precommit
20
20
21
- import de.thetaphi.forbiddenapis.gradle.CheckForbiddenApis
22
- import de.thetaphi.forbiddenapis.gradle.ForbiddenApisPlugin
23
21
import org.elasticsearch.gradle.ExportElasticsearchBuildResourcesTask
24
- import org.gradle.api.JavaVersion
25
22
import org.gradle.api.Project
26
23
import org.gradle.api.Task
27
- import org.gradle.api.file.FileCollection
24
+ import org.gradle.api.artifacts.Configuration
28
25
import org.gradle.api.plugins.JavaBasePlugin
29
26
import org.gradle.api.plugins.quality.Checkstyle
30
- import org.gradle.api.tasks.JavaExec
31
- import org.gradle.api.tasks.StopExecutionException
32
-
33
27
/**
34
28
* Validation tasks which should be run before committing. These run before tests.
35
29
*/
@@ -38,8 +32,8 @@ class PrecommitTasks {
38
32
/* * Adds a precommit task, which depends on non-test verification tasks. */
39
33
public static Task create (Project project , boolean includeDependencyLicenses ) {
40
34
List<Task > precommitTasks = [
41
- configureForbiddenApis(project),
42
35
configureCheckstyle(project),
36
+ configureForbiddenApisCli(project),
43
37
configureNamingConventions(project),
44
38
project. tasks. create(' forbiddenPatterns' , ForbiddenPatternsTask . class),
45
39
project. tasks. create(' licenseHeaders' , LicenseHeadersTask . class),
@@ -48,9 +42,6 @@ class PrecommitTasks {
48
42
project. tasks. create(' thirdPartyAudit' , ThirdPartyAuditTask . class)
49
43
]
50
44
51
- // Configure it but don't add it as a dependency yet
52
- configureForbiddenApisCli(project)
53
-
54
45
// tasks with just tests don't need dependency licenses, so this flag makes adding
55
46
// the task optional
56
47
if (includeDependencyLicenses) {
@@ -84,77 +75,60 @@ class PrecommitTasks {
84
75
return project. tasks. create(precommitOptions)
85
76
}
86
77
87
- private static Task configureForbiddenApis (Project project ) {
88
- project. pluginManager. apply(ForbiddenApisPlugin . class)
89
- project. forbiddenApis {
90
- failOnUnsupportedJava = false
91
- bundledSignatures = [' jdk-unsafe' , ' jdk-deprecated' , ' jdk-non-portable' , ' jdk-system-out' ]
92
- signaturesURLs = [getClass(). getResource(' /forbidden/jdk-signatures.txt' ),
93
- getClass(). getResource(' /forbidden/es-all-signatures.txt' )]
94
- suppressAnnotations = [' **.SuppressForbidden' ]
95
- }
96
- project. tasks. withType(CheckForbiddenApis ) {
97
- // we do not use the += operator to add signatures, as conventionMappings of Gradle do not work when it's configured using withType:
98
- if (name. endsWith(' Test' )) {
99
- signaturesURLs = project. forbiddenApis. signaturesURLs +
100
- [ getClass(). getResource(' /forbidden/es-test-signatures.txt' ), getClass(). getResource(' /forbidden/http-signatures.txt' ) ]
101
- } else {
102
- signaturesURLs = project. forbiddenApis. signaturesURLs +
103
- [ getClass(). getResource(' /forbidden/es-server-signatures.txt' ) ]
104
- }
105
- // forbidden apis doesn't support Java 11, so stop at 10
106
- String targetMajorVersion = (project. compilerJavaVersion. compareTo(JavaVersion . VERSION_1_10 ) > 0 ?
107
- JavaVersion . VERSION_1_10 :
108
- project. compilerJavaVersion). getMajorVersion()
109
- targetCompatibility = Integer . parseInt(targetMajorVersion) >= 9 ? targetMajorVersion : " 1.${ targetMajorVersion} "
110
- }
111
- Task forbiddenApis = project. tasks. findByName(' forbiddenApis' )
112
- forbiddenApis. group = " " // clear group, so this does not show up under verification tasks
113
-
114
- return forbiddenApis
115
- }
116
-
117
78
private static Task configureForbiddenApisCli (Project project ) {
118
- project. configurations. create(" forbiddenApisCliJar" )
79
+ Configuration forbiddenApisConfiguration = project. configurations. create(" forbiddenApisCliJar" )
119
80
project. dependencies {
120
- forbiddenApisCliJar ' de.thetaphi:forbiddenapis:2.5'
81
+ forbiddenApisCliJar ( ' de.thetaphi:forbiddenapis:2.5' )
121
82
}
122
- Task forbiddenApisCli = project. tasks. create(' forbiddenApisCli ' )
83
+ Task forbiddenApisCli = project. tasks. create(' forbiddenApis ' )
123
84
124
85
project. sourceSets. forEach { sourceSet ->
125
86
forbiddenApisCli. dependsOn(
126
- project. tasks. create(sourceSet. getTaskName(' forbiddenApisCli ' , null ), JavaExec ) {
87
+ project. tasks. create(sourceSet. getTaskName(' forbiddenApis ' , null ), ForbiddenApisCliTask ) {
127
88
ExportElasticsearchBuildResourcesTask buildResources = project. tasks. getByName(' buildResources' )
128
89
dependsOn(buildResources)
129
- classpath = project. files(
130
- project. configurations. forbiddenApisCliJar,
90
+ execAction = { spec ->
91
+ spec. classpath = project. files(
92
+ project. configurations. forbiddenApisCliJar,
93
+ sourceSet. compileClasspath,
94
+ sourceSet. runtimeClasspath
95
+ )
96
+ spec. executable = " ${ project.runtimeJavaHome} /bin/java"
97
+ }
98
+ inputs. files(
99
+ forbiddenApisConfiguration,
131
100
sourceSet. compileClasspath,
132
101
sourceSet. runtimeClasspath
133
102
)
134
- main = ' de.thetaphi.forbiddenapis.cli.CliMain'
135
- executable = " ${ project.runtimeJavaHome} /bin/java"
136
- args " -b" , ' jdk-unsafe-1.8'
137
- args " -b" , ' jdk-deprecated-1.8'
138
- args " -b" , ' jdk-non-portable'
139
- args " -b" , ' jdk-system-out'
140
- args " -f" , buildResources. copy(" forbidden/jdk-signatures.txt" )
141
- args " -f" , buildResources. copy(" forbidden/es-all-signatures.txt" )
142
- args " --suppressannotation" , ' **.SuppressForbidden'
103
+
104
+ targetCompatibility = project. compilerJavaVersion
105
+ bundledSignatures = [
106
+ " jdk-unsafe" , " jdk-deprecated" , " jdk-non-portable" , " jdk-system-out"
107
+ ]
108
+ signaturesFiles = project. files(
109
+ buildResources. copy(" forbidden/jdk-signatures.txt" ),
110
+ buildResources. copy(" forbidden/es-all-signatures.txt" )
111
+ )
112
+ suppressAnnotations = [' **.SuppressForbidden' ]
143
113
if (sourceSet. name == ' test' ) {
144
- args " -f" , buildResources. copy(" forbidden/es-test-signatures.txt" )
145
- args " -f" , buildResources. copy(" forbidden/http-signatures.txt" )
114
+ signaturesFiles + = project. files(
115
+ buildResources. copy(" forbidden/es-test-signatures.txt" ),
116
+ buildResources. copy(" forbidden/http-signatures.txt" )
117
+ )
146
118
} else {
147
- args " -f " , buildResources. copy(" forbidden/es-server-signatures.txt" )
119
+ signaturesFiles + = project . files( buildResources. copy(" forbidden/es-server-signatures.txt" ) )
148
120
}
149
121
dependsOn sourceSet. classesTaskName
150
- doFirst {
151
- // Forbidden APIs expects only existing dirs, and requires at least one
152
- FileCollection existingOutputs = sourceSet. output. classesDirs
153
- .filter { it. exists() }
154
- if (existingOutputs. isEmpty()) {
155
- throw new StopExecutionException (" ${ sourceSet.name} has no outputs" )
156
- }
157
- existingOutputs. forEach { args " -d" , it }
122
+ classesDirs = sourceSet. output. classesDirs
123
+ ext. replaceSignatureFiles = { String ... names ->
124
+ signaturesFiles = project. files(
125
+ names. collect { buildResources. copy(" forbidden/${ it} .txt" ) }
126
+ )
127
+ }
128
+ ext. addSignatureFiles = { String ... names ->
129
+ signaturesFiles + = project. files(
130
+ names. collect { buildResources. copy(" forbidden/${ it} .txt" ) }
131
+ )
158
132
}
159
133
}
160
134
)
0 commit comments