forked from apache/lucene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecj-lint.gradle
139 lines (118 loc) · 5.52 KB
/
ecj-lint.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This adds 'ecjLint' task.
configure(rootProject) {
configurations {
ecjDeps
}
dependencies {
ecjDeps "org.eclipse.jdt:ecj:${scriptDepVersions['ecj']}"
}
}
def resources = scriptResources(buildscript)
allprojects {
plugins.withType(JavaPlugin) {
// Create a [sourceSetName]EcjLint task for each source set
// with a non-empty java.srcDirs. These tasks are then
// attached to project's "ecjLint" task.
def lintTasks = sourceSets.collect { sourceSet ->
def srcDirs = sourceSet.java.sourceDirectories
.filter { dir -> dir.exists() }
tasks.create(sourceSet.getTaskName("ecjLint", null), JavaExec, { JavaExec task ->
// This dependency is on a configuration; technically it causes
// all dependencies to be resolved before this task executes
// (this includes scheduling tasks that compile the
// sources from other projects for example).
dependsOn sourceSet.compileClasspath
// The inputs are all source files from the sourceSet.
inputs.files sourceSet.allSource.asFileTree
// We create a task for all source sets but ignore those
// that don't have any Java source directories.
enabled = !srcDirs.isEmpty()
classpath = rootProject.configurations.ecjDeps
mainClass = "org.eclipse.jdt.internal.compiler.batch.Main"
// Don't emit any .class files.
// Hack around "-d none" still emitting package-info.class
// by running in a temporary directory.
def tmpDst = getTemporaryDir()
workingDir tmpDst
args += [ "-d", "none" ]
// Compilation environment.
// we use -source/-target as it is significantly faster than --release
args += [ "-source", project.java.sourceCompatibility ]
args += [ "-target", project.java.targetCompatibility ]
args += [ "-encoding", "UTF-8"]
args += [ "-proc:none" ]
args += [ "-nowarn" ]
args += [ "-enableJavadoc" ]
args += [ "-properties", file("${resources}/ecj.javadocs.prefs").absolutePath ]
// We depend on modular paths.
def modularPaths = sourceSet.modularPaths
dependsOn modularPaths.compileModulePathConfiguration
// Add modular dependencies and their transitive dependencies to module path.
task.argumentProviders.add(modularPaths.compilationArguments)
// Add classpath, if needed.
task.argumentProviders.add((CommandLineArgumentProvider) {
// Add classpath locations in a lazy provider (can't resolve the
// configuration at evaluation time). Filter out non-existing entries
// (output folders for non-existing input source dirs like resources).
FileCollection cpath = modularPaths.compilationClasspath.filter { p -> p.exists() }
if (!cpath.isEmpty()) {
return ["-classpath", cpath.join(File.pathSeparator)]
} else {
return []
}
})
// Place input files in an external file to dodge command line argument
// limits. We could pass a directory but ecj seems to be buggy: when it
// encounters a module-info.java file it no longer compiles other source files.
def inputsFile = file("${tmpDst}/ecj-inputs.txt")
task.argumentProviders.add((CommandLineArgumentProvider) {
return ["@" + inputsFile.absolutePath]
})
doFirst {
tmpDst.mkdirs()
// escape filename accoring to ECJ's rules:
// https://github.com/eclipse/aspectj.eclipse.jdt.core/blob/a05312e746b9bc2b48b4b039f6e7b5e061b5b393/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java#L1533-L1537
// Basically surround all whitespace by quotes:
def escapeFileName = { String s -> s.replaceAll(/ +/, /"$0"/) }
inputsFile.setText(
srcDirs.collectMany { dir ->
project.fileTree(
dir: dir,
include: "**/*.java",
// Exclude the benchmark class with dependencies on nekohtml, which causes module-classpath conflicts and breaks ecj.
exclude: "**/DemoHTMLParser.java"
).files
}
// Try to sort all input files; a side-effect of this should be that module-info.java
// is placed first on the list, which works around ECJ bug:
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=569833
.sort()
.collect {file -> escapeFileName(file.absolutePath.toString())}.join("\n"), "UTF-8")
}
})
}
task ecjLint() {
description "Lint Java sources using ECJ."
group "Verification"
dependsOn lintTasks
}
// Attach ecjLint to check.
check.dependsOn ecjLint
}
}