1818 */
1919package org.elasticsearch.gradle.precommit
2020
21- import de.thetaphi.forbiddenapis.gradle.CheckForbiddenApis
22- import de.thetaphi.forbiddenapis.gradle.ForbiddenApisPlugin
23- import org.elasticsearch.gradle.ExportElasticsearchBuildResourcesTask
24- import org.elasticsearch.gradle.VersionProperties
25- import org.elasticsearch.gradle.info.BuildParams
26- import org.elasticsearch.gradle.util.Util
27- import org.gradle.api.JavaVersion
21+
2822import org.gradle.api.Project
29- import org.gradle.api.artifacts.Configuration
30- import org.gradle.api.file.FileCollection
31- import org.gradle.api.plugins.JavaBasePlugin
32- import org.gradle.api.plugins.quality.Checkstyle
33- import org.gradle.api.tasks.SourceSet
34- import org.gradle.api.tasks.TaskProvider
3523
3624/**
3725 * Validation tasks which should be run before committing. These run before tests.
@@ -40,42 +28,21 @@ class PrecommitTasks {
4028
4129 /* * Adds a precommit task, which depends on non-test verification tasks. */
4230
43- public static TaskProvider create (Project project , boolean includeDependencyLicenses ) {
44- project. configurations. create(" forbiddenApisCliJar" )
45- project. dependencies {
46- forbiddenApisCliJar(' de.thetaphi:forbiddenapis:2.7' )
47- }
48-
49- Configuration jarHellConfig = project. configurations. create(" jarHell" )
50- if (BuildParams . internal && project. path. equals(" :libs:elasticsearch-core" ) == false ) {
51- // External plugins will depend on this already via transitive dependencies.
52- // Internal projects are not all plugins, so make sure the check is available
53- // we are not doing this for this project itself to avoid jar hell with itself
54- project. dependencies {
55- jarHell project. project(" :libs:elasticsearch-core" )
56- }
57- }
31+ public static void create (Project project , boolean includeDependencyLicenses ) {
5832
59- List<TaskProvider > precommitTasks = [
60- configureCheckstyle(project),
61- configureForbiddenApisCli(project),
62- project. tasks. register(' forbiddenPatterns' , ForbiddenPatternsTask ),
63- project. tasks. register(' licenseHeaders' , LicenseHeadersTask ),
64- project. tasks. register(' filepermissions' , FilePermissionsTask ),
65- configureJarHell(project, jarHellConfig),
66- configureThirdPartyAudit(project),
67- configureTestingConventions(project)
68- ]
33+ project. pluginManager. apply(CheckstylePrecommitPlugin )
34+ project. pluginManager. apply(ForbiddenApisPrecommitPlugin )
35+ project. pluginManager. apply(JarHellPrecommitPlugin )
36+ project. pluginManager. apply(ForbiddenPatternsPrecommitPlugin )
37+ project. pluginManager. apply(LicenseHeadersPrecommitPlugin )
38+ project. pluginManager. apply(FilePermissionsPrecommitPlugin )
39+ project. pluginManager. apply(ThirdPartyAuditPrecommitPlugin )
40+ project. pluginManager. apply(TestingConventionsPrecommitPlugin )
6941
7042 // tasks with just tests don't need dependency licenses, so this flag makes adding
7143 // the task optional
7244 if (includeDependencyLicenses) {
73- TaskProvider<DependencyLicensesTask > dependencyLicenses = project. tasks. register(' dependencyLicenses' , DependencyLicensesTask )
74- precommitTasks. add(dependencyLicenses)
75- // we also create the updateShas helper task that is associated with dependencyLicenses
76- project. tasks. register(' updateShas' , UpdateShasTask ) {
77- it. parentTask = dependencyLicenses
78- }
45+ project. pluginManager. apply(DependencyLicensesPrecommitPlugin )
7946 }
8047 if (project. path != ' :build-tools' ) {
8148 /*
@@ -88,195 +55,7 @@ class PrecommitTasks {
8855 * use the NamingConventionsCheck we break the circular dependency
8956 * here.
9057 */
91- precommitTasks. add(configureLoggerUsage(project))
92- }
93-
94- // We want to get any compilation error before running the pre-commit checks.
95- project. sourceSets. all { sourceSet ->
96- precommitTasks. each { provider ->
97- provider. configure {
98- shouldRunAfter(sourceSet. getClassesTaskName())
99- }
100- }
101- }
102-
103- TaskProvider precommit = project. tasks. register(' precommit' ) {
104- group = JavaBasePlugin . VERIFICATION_GROUP
105- description = ' Runs all non-test checks.'
106- dependsOn = precommitTasks
107- }
108-
109- // not all jar projects produce a pom (we don't ship all jars), so a pom validation
110- // task is only added on some projects, and thus we can't always have a task
111- // here to add to precommit tasks explicitly. Instead, we apply our internal
112- // pom validation plugin after the precommit task is created and let the
113- // plugin add the task if necessary
114- project. plugins. apply(PomValidationPlugin )
115-
116- return precommit
117- }
118-
119- static TaskProvider configureTestingConventions (Project project ) {
120- return project. getTasks(). register(" testingConventions" , TestingConventionsTasks ) {
121- naming {
122- Tests {
123- baseClass " org.apache.lucene.util.LuceneTestCase"
124- }
125- IT {
126- baseClass " org.elasticsearch.test.ESIntegTestCase"
127- baseClass ' org.elasticsearch.test.rest.ESRestTestCase'
128- }
129- }
130- }
131- }
132-
133- private static TaskProvider configureJarHell (Project project , Configuration jarHellConfig ) {
134- return project. tasks. register(' jarHell' , JarHellTask ) { task ->
135- task. classpath = project. sourceSets. test. runtimeClasspath + jarHellConfig
136- task. dependsOn(jarHellConfig)
137- }
138- }
139-
140- private static TaskProvider configureThirdPartyAudit (Project project ) {
141- ExportElasticsearchBuildResourcesTask buildResources = project. tasks. getByName(' buildResources' )
142- return project. tasks. register(' thirdPartyAudit' , ThirdPartyAuditTask ) { task ->
143- task. dependsOn(buildResources)
144- task. signatureFile = buildResources. copy(" forbidden/third-party-audit.txt" )
145- task. javaHome = BuildParams . runtimeJavaHome
146- task. targetCompatibility. set(project. provider({ BuildParams . runtimeJavaVersion }))
147- }
148- }
149-
150- private static TaskProvider configureForbiddenApisCli (Project project ) {
151- project. pluginManager. apply(ForbiddenApisPlugin )
152- ExportElasticsearchBuildResourcesTask buildResources = project. tasks. getByName(' buildResources' )
153- project. tasks. withType(CheckForbiddenApis ). configureEach {
154- dependsOn(buildResources)
155-
156- assert name. startsWith(ForbiddenApisPlugin . FORBIDDEN_APIS_TASK_NAME )
157- String sourceSetName
158- if (ForbiddenApisPlugin . FORBIDDEN_APIS_TASK_NAME . equals(name)) {
159- sourceSetName = " main"
160- } else {
161- // parse out the sourceSetName
162- char [] chars = name. substring(ForbiddenApisPlugin . FORBIDDEN_APIS_TASK_NAME . length()). toCharArray()
163- chars[0 ] = Character . toLowerCase(chars[0 ])
164- sourceSetName = new String (chars)
165- }
166-
167- SourceSet sourceSet = project. sourceSets. getByName(sourceSetName)
168- classpath = project. files { sourceSet. runtimeClasspath. plus(sourceSet. compileClasspath) }
169-
170- targetCompatibility = BuildParams . runtimeJavaVersion. majorVersion
171- if (BuildParams . runtimeJavaVersion > JavaVersion . VERSION_14 ) {
172- // TODO: forbidden apis does not yet support java 15, rethink using runtime version
173- targetCompatibility = JavaVersion . VERSION_14 . majorVersion
174- }
175- bundledSignatures = [
176- " jdk-unsafe" , " jdk-deprecated" , " jdk-non-portable" , " jdk-system-out"
177- ]
178- signaturesFiles = project. files(
179- buildResources. copy(" forbidden/jdk-signatures.txt" ),
180- buildResources. copy(" forbidden/es-all-signatures.txt" )
181- )
182- suppressAnnotations = [' **.SuppressForbidden' ]
183- if (name. endsWith(' Test' )) {
184- signaturesFiles + = project. files(
185- buildResources. copy(" forbidden/es-test-signatures.txt" ),
186- buildResources. copy(" forbidden/http-signatures.txt" )
187- )
188- } else {
189- signaturesFiles + = project. files(buildResources. copy(" forbidden/es-server-signatures.txt" ))
190- }
191- ext. replaceSignatureFiles = { String ... names ->
192- signaturesFiles = project. files(
193- names. collect { buildResources. copy(" forbidden/${ it} .txt" ) }
194- )
195- }
196- ext. addSignatureFiles = { String ... names ->
197- signaturesFiles + = project. files(
198- names. collect { buildResources. copy(" forbidden/${ it} .txt" ) }
199- )
200- }
201- }
202- TaskProvider forbiddenApis = project. tasks. named(" forbiddenApis" )
203- forbiddenApis. configure {
204- group = " "
205- }
206- return forbiddenApis
207- }
208-
209- private static TaskProvider configureCheckstyle (Project project ) {
210- // Always copy the checkstyle configuration files to 'buildDir/checkstyle' since the resources could be located in a jar
211- // file. If the resources are located in a jar, Gradle will fail when it tries to turn the URL into a file
212- URL checkstyleConfUrl = PrecommitTasks . getResource(" /checkstyle.xml" )
213- URL checkstyleSuppressionsUrl = PrecommitTasks . getResource(" /checkstyle_suppressions.xml" )
214- File checkstyleDir = new File (project. buildDir, " checkstyle" )
215- File checkstyleSuppressions = new File (checkstyleDir, " checkstyle_suppressions.xml" )
216- File checkstyleConf = new File (checkstyleDir, " checkstyle.xml" );
217- TaskProvider copyCheckstyleConf = project. tasks. register(" copyCheckstyleConf" )
218-
219- // configure inputs and outputs so up to date works properly
220- copyCheckstyleConf. configure {
221- outputs. files(checkstyleSuppressions, checkstyleConf)
222- }
223- if (" jar" . equals(checkstyleConfUrl. getProtocol())) {
224- JarURLConnection jarURLConnection = (JarURLConnection ) checkstyleConfUrl. openConnection()
225- copyCheckstyleConf. configure {
226- inputs. file(jarURLConnection. getJarFileURL())
227- }
228- } else if (" file" . equals(checkstyleConfUrl. getProtocol())) {
229- copyCheckstyleConf. configure {
230- inputs. files(checkstyleConfUrl. getFile(), checkstyleSuppressionsUrl. getFile())
231- }
232- }
233-
234- copyCheckstyleConf. configure {
235- doLast {
236- checkstyleDir. mkdirs()
237- // withStream will close the output stream and IOGroovyMethods#getBytes reads the InputStream fully and closes it
238- new FileOutputStream (checkstyleConf). withStream {
239- it. write(checkstyleConfUrl. openStream(). getBytes())
240- }
241- new FileOutputStream (checkstyleSuppressions). withStream {
242- it. write(checkstyleSuppressionsUrl. openStream(). getBytes())
243- }
244- }
245- }
246-
247- TaskProvider checkstyleTask = project. tasks. register(' checkstyle' ) {
248- dependsOn project. tasks. withType(Checkstyle )
249- }
250- // Apply the checkstyle plugin to create `checkstyleMain` and `checkstyleTest`. It only
251- // creates them if there is main or test code to check and it makes `check` depend
252- // on them. We also want `precommit` to depend on `checkstyle`.
253- project. pluginManager. apply(' checkstyle' )
254- project. checkstyle {
255- configDir = checkstyleDir
256- }
257- project. dependencies {
258- checkstyle " com.puppycrawl.tools:checkstyle:${ VersionProperties.versions.checkstyle} "
259- checkstyle project. files(Util . buildSrcCodeSource)
260- }
261-
262- project. tasks. withType(Checkstyle ). configureEach { task ->
263- task. dependsOn(copyCheckstyleConf)
264- task. reports {
265- html. enabled false
266- }
267- }
268-
269- return checkstyleTask
270- }
271-
272- private static TaskProvider configureLoggerUsage (Project project ) {
273- Object dependency = BuildParams . internal ? project. project(' :test:logger-usage' ) :
274- " org.elasticsearch.test:logger-usage:${ VersionProperties.elasticsearch} "
275-
276- project. configurations. create(' loggerUsagePlugin' )
277- project. dependencies. add(' loggerUsagePlugin' , dependency)
278- return project. tasks. register(' loggerUsageCheck' , LoggerUsageTask ) {
279- classpath = project. configurations. loggerUsagePlugin
58+ project. pluginManager. apply(LoggerUsagePrecommitPlugin )
28059 }
28160 }
28261}
0 commit comments