Skip to content

Commit 668e8b1

Browse files
committed
Remove additional configuration
1 parent e39bed9 commit 668e8b1

File tree

4 files changed

+83
-37
lines changed

4 files changed

+83
-37
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/PrecommitTasks.groovy

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,18 @@ class PrecommitTasks {
7676
}
7777

7878
private static Task configureForbiddenApisCli(Project project) {
79-
project.configurations.create("forbiddenApisCliJar")
80-
project.dependencies {
81-
forbiddenApisCliJar ('de.thetaphi:forbiddenapis:2.5') {
82-
// FIXME
83-
transitive = false
84-
}
85-
}
8679
Task forbiddenApisCli = project.tasks.create('forbiddenApis')
87-
8880
project.sourceSets.forEach { sourceSet ->
8981
forbiddenApisCli.dependsOn(
9082
project.tasks.create(sourceSet.getTaskName('forbiddenApis', null), ForbiddenApisCliTask) {
9183
ExportElasticsearchBuildResourcesTask buildResources = project.tasks.getByName('buildResources')
9284
dependsOn(buildResources)
9385
execAction = { spec ->
94-
spec.classpath = project.files(
95-
project.configurations.forbiddenApisCliJar,
96-
sourceSet.compileClasspath,
97-
sourceSet.runtimeClasspath
98-
)
86+
spec.classpath(sourceSet.compileClasspath)
87+
spec.classpath(sourceSet.runtimeClasspath)
9988
spec.executable = "${project.runtimeJavaHome}/bin/java"
10089
}
101-
102-
targetCompatibility = JavaVersion.VERSION_1_8 // FIXME
90+
targetCompatibility = JavaVersion.VERSION_1_8 // FIXME: change to min(compilerVersion, 10)
10391
bundledSignatures = [
10492
"jdk-unsafe", "jdk-deprecated", "jdk-non-portable", "jdk-system-out"
10593
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.gradle;
20+
21+
import java.net.URISyntaxException;
22+
import java.net.URL;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
27+
public class ClassPathUtils {
28+
29+
public static Path getJar(Class<?> theClass) {
30+
URL location = theClass.getProtectionDomain().getCodeSource().getLocation();
31+
if (location.getProtocol().equals("file") == false) {
32+
throw new IllegalArgumentException(
33+
"Unexpected location for " + theClass.getName() + ": "+ location
34+
);
35+
}
36+
final Path path;
37+
try {
38+
path = Paths.get(location.toURI());
39+
} catch (URISyntaxException e) {
40+
throw new AssertionError(e);
41+
}
42+
if (Files.exists(path) == false) {
43+
throw new AssertionError("Bath to class source does not exist: " + path);
44+
}
45+
return path;
46+
}
47+
48+
}

buildSrc/src/main/java/org/elasticsearch/gradle/precommit/ForbiddenApisCliTask.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.elasticsearch.gradle.precommit;
2020

2121
import de.thetaphi.forbiddenapis.cli.CliMain;
22+
import org.elasticsearch.gradle.ClassPathUtils;
2223
import org.gradle.api.Action;
2324
import org.gradle.api.DefaultTask;
2425
import org.gradle.api.JavaVersion;
@@ -32,12 +33,16 @@
3233

3334
import java.io.File;
3435
import java.io.IOException;
36+
import java.nio.charset.StandardCharsets;
3537
import java.nio.file.Files;
38+
import java.nio.file.Path;
39+
import java.nio.file.Paths;
3640
import java.util.ArrayList;
3741
import java.util.Collections;
3842
import java.util.LinkedHashSet;
3943
import java.util.List;
4044
import java.util.Set;
45+
import java.util.stream.Collectors;
4146

4247
public class ForbiddenApisCliTask extends DefaultTask {
4348

@@ -69,7 +74,7 @@ public void setExecAction(Action<JavaExecSpec> execAction) {
6974
@OutputFile
7075
public File getMarkerFile() {
7176
return new File(
72-
new File(getProject().getBuildDir(), "precommit"),
77+
new File(getProject().getBuildDir(), this.getClass().getSimpleName()),
7378
getName()
7479
);
7580
}
@@ -121,12 +126,25 @@ public void setSuppressAnnotations(Set<String> suppressAnnotations) {
121126
}
122127

123128
@TaskAction
124-
public void writeMarker() throws IOException {
129+
public void runCheck() throws IOException {
130+
Path inlineSignatures = Paths.get(
131+
getProject().getBuildDir().getAbsolutePath(),
132+
this.getClass().getSimpleName(),
133+
getName() + ".inline.sig"
134+
);
135+
Files.write(
136+
inlineSignatures,
137+
signatures.stream().collect(Collectors.joining("\n")).getBytes(StandardCharsets.UTF_8)
138+
);
139+
125140
getProject().javaexec((JavaExecSpec spec) -> {
126141
execAction.execute(spec);
142+
// This works because forbidden apis has no transitive dependencies.
143+
spec.classpath(ClassPathUtils.getJar(CliMain.class));
127144
spec.setMain(CliMain.class.getName());
128145
// build the command line
129146
getSignaturesFiles().forEach(file -> spec.args("-f", file.getAbsolutePath()));
147+
spec.args("-f", inlineSignatures.toAbsolutePath());
130148
getSuppressAnnotations().forEach(annotation -> spec.args("--suppressannotation", annotation));
131149
getBundledSignatures().forEach(bundled -> {
132150
// there's no option for target compatibility so we have to interpret it

buildSrc/src/main/java/org/elasticsearch/gradle/precommit/NamingConventionsTask.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.elasticsearch.gradle.precommit;
22

33
import groovy.lang.Closure;
4+
import org.elasticsearch.gradle.ClassPathUtils;
45
import org.elasticsearch.gradle.LoggedExec;
56
import org.elasticsearch.test.NamingConventionsCheck;
67
import org.gradle.api.GradleException;
@@ -16,8 +17,6 @@
1617
import java.io.File;
1718
import java.io.FileWriter;
1819
import java.io.IOException;
19-
import java.net.URISyntaxException;
20-
import java.net.URL;
2120
import java.util.Objects;
2221

2322
/**
@@ -33,24 +32,17 @@ public NamingConventionsTask() {
3332

3433
SourceSetContainer sourceSets = getJavaSourceSets();
3534
final FileCollection classpath;
36-
try {
37-
URL location = NamingConventionsCheck.class.getProtectionDomain().getCodeSource().getLocation();
38-
if (location.getProtocol().equals("file") == false) {
39-
throw new GradleException("Unexpected location for NamingConventionCheck class: "+ location);
40-
}
41-
classpath = project.files(
42-
// This works because the class only depends on one class from junit that will be available from the
43-
// tests compile classpath. It's the most straight forward way of telling Java where to find the main
44-
// class.
45-
location.toURI().getPath(),
46-
// the tests to be loaded
47-
checkForTestsInMain ? sourceSets.getByName("main").getRuntimeClasspath() : project.files(),
48-
sourceSets.getByName("test").getCompileClasspath(),
49-
sourceSets.getByName("test").getOutput()
50-
);
51-
} catch (URISyntaxException e) {
52-
throw new AssertionError(e);
53-
}
35+
classpath = project.files(
36+
// This works because the class only depends on one class from junit that will be available from the
37+
// tests compile classpath. It's the most straight forward way of telling Java where to find the main
38+
// class.
39+
ClassPathUtils.getJar(NamingConventionsCheck.class).toFile(),
40+
// the tests to be loaded
41+
checkForTestsInMain ? sourceSets.getByName("main").getRuntimeClasspath() : project.files(),
42+
sourceSets.getByName("test").getCompileClasspath(),
43+
sourceSets.getByName("test").getOutput()
44+
);
45+
5446
dependsOn(project.getTasks().matching(it -> "testCompileClasspath".equals(it.getName())));
5547
getInputs().files(classpath);
5648

0 commit comments

Comments
 (0)