Skip to content

Commit

Permalink
Fix deprecation warning in ThirdpartyAuditTask (#57123) (#57230)
Browse files Browse the repository at this point in the history
  • Loading branch information
breskeby committed May 28, 2020
1 parent 2a03a13 commit 9c100b4
Show file tree
Hide file tree
Showing 30 changed files with 119 additions and 33 deletions.
24 changes: 20 additions & 4 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.gradle.util.GradleVersion
plugins {
id 'java-gradle-plugin'
id 'groovy'
id 'java-test-fixtures'
}

group = 'org.elasticsearch.gradle'
Expand Down Expand Up @@ -64,6 +65,11 @@ if (JavaVersion.current() < JavaVersion.VERSION_11) {
sourceSets {
// We have a few classes that need to be compiled for older java versions
minimumRuntime {}

integTest {
compileClasspath += sourceSets["main"].output + configurations["testRuntimeClasspath"]
runtimeClasspath += output + compileClasspath
}
}

configurations {
Expand Down Expand Up @@ -117,8 +123,10 @@ dependencies {
compile 'com.networknt:json-schema-validator:1.0.36'
compileOnly "com.puppycrawl.tools:checkstyle:${props.getProperty('checkstyle')}"
testCompile "com.puppycrawl.tools:checkstyle:${props.getProperty('checkstyle')}"
testCompile "junit:junit:${props.getProperty('junit')}"
testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${props.getProperty('randomizedrunner')}"
testFixturesApi "junit:junit:${props.getProperty('junit')}"
testFixturesApi "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${props.getProperty('randomizedrunner')}"
testFixturesApi gradleApi()
testFixturesApi gradleTestKit()
testCompile 'com.github.tomakehurst:wiremock-jre8-standalone:2.23.2'
testCompile 'org.mockito:mockito-core:1.9.5'
minimumRuntimeCompile "junit:junit:${props.getProperty('junit')}"
Expand Down Expand Up @@ -165,10 +173,11 @@ if (project != rootProject) {
// build-tools is not ready for primetime with these...
dependencyLicenses.enabled = false
dependenciesInfo.enabled = false
disableTasks('forbiddenApisMain', 'forbiddenApisMinimumRuntime', 'forbiddenApisTest')
disableTasks('forbiddenApisMain', 'forbiddenApisMinimumRuntime',
'forbiddenApisTest', 'forbiddenApisIntegTest', 'forbiddenApisTestFixtures')
jarHell.enabled = false
thirdPartyAudit.enabled = false
if (Boolean.parseBoolean(System.getProperty("tests.fips.enabled"))){
if (Boolean.parseBoolean(System.getProperty("tests.fips.enabled"))) {
test.enabled = false
}

Expand Down Expand Up @@ -237,6 +246,8 @@ if (project != rootProject) {
systemProperty 'test.version_under_test', version
onlyIf { org.elasticsearch.gradle.info.BuildParams.inFipsJvm == false }
maxParallelForks = System.getProperty('tests.jvms', org.elasticsearch.gradle.info.BuildParams.defaultParallel.toString()) as Integer
testClassesDirs = sourceSets.integTest.output.classesDirs
classpath = sourceSets.integTest.runtimeClasspath
}
check.dependsOn("integTest")

Expand All @@ -252,6 +263,11 @@ if (project != rootProject) {
afterEvaluate {
generatePomFileForPluginMavenPublication.enabled = false
}

publishing.publications.named("nebula").configure {
suppressPomMetadataWarningsFor("testFixturesApiElements")
suppressPomMetadataWarningsFor("testFixturesRuntimeElements")
}
}

// Define this here because we need it early.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
package org.elasticsearch.gradle.precommit;

import org.elasticsearch.gradle.test.GradleIntegrationTestCase;
import org.gradle.testkit.runner.BuildResult;
import org.junit.Before;

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
Expand All @@ -13,7 +7,7 @@
* 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
* 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
Expand All @@ -22,6 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.gradle.precommit;

import org.elasticsearch.gradle.test.GradleIntegrationTestCase;
import org.gradle.testkit.runner.BuildResult;
import org.junit.Before;

public class ThirdPartyAuditTaskIT extends GradleIntegrationTestCase {

@Before
Expand All @@ -41,6 +42,7 @@ public void testElasticsearchIgnored() {
"-PcompileVersion=0.0.1"
).build();
assertTaskNoSource(result, ":empty");
assertNoDeprecationWarning(result);
}

public void testWithEmptyRules() {
Expand Down Expand Up @@ -69,6 +71,7 @@ public void testViolationFoundAndCompileOnlyIgnored() {
assertTaskFailed(result, ":absurd");
assertOutputContains(result.getOutput(), "Classes with violations:", " * TestingIO", "> Audit of third party dependencies failed");
assertOutputDoesNotContain(result.getOutput(), "Missing classes:");
assertNoDeprecationWarning(result);
}

public void testClassNotFoundAndCompileOnlyIgnored() {
Expand All @@ -90,6 +93,7 @@ public void testClassNotFoundAndCompileOnlyIgnored() {
"> Audit of third party dependencies failed"
);
assertOutputDoesNotContain(result.getOutput(), "Classes with violations:");
assertNoDeprecationWarning(result);
}

public void testJarHellWithJDK() {
Expand All @@ -111,6 +115,7 @@ public void testJarHellWithJDK() {
" * java.lang.String"
);
assertOutputDoesNotContain(result.getOutput(), "Classes with violations:");
assertNoDeprecationWarning(result);
}

public void testElasticsearchIgnoredWithViolations() {
Expand All @@ -124,6 +129,7 @@ public void testElasticsearchIgnoredWithViolations() {
"-PcompileVersion=0.0.1"
).build();
assertTaskNoSource(result, ":absurd");
assertNoDeprecationWarning(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.gradle

import org.elasticsearch.gradle.dependencies.CompileOnlyResolvePlugin
import org.elasticsearch.gradle.precommit.DependencyLicensesTask
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -27,15 +28,16 @@ import org.gradle.api.tasks.TaskProvider

class DependenciesInfoPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
void apply(Project project) {
project.getPlugins().apply(CompileOnlyResolvePlugin.class);
TaskProvider<DependenciesInfoTask> depsInfo = project.getTasks().register("dependenciesInfo", DependenciesInfoTask.class);
depsInfo.configure { DependenciesInfoTask t ->
t.setRuntimeConfiguration(project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME));
t.setCompileOnlyConfiguration(project.getConfigurations().getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME));
t.setCompileOnlyConfiguration(project.getConfigurations().getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME));
t.getConventionMapping().map("mappings") { ->
TaskProvider<DependencyLicensesTask> depLic = project.getTasks().named("dependencyLicenses", DependencyLicensesTask.class);
return depLic.get().getMappings();
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.GroovyCompile;
Expand Down Expand Up @@ -96,12 +97,12 @@ public void apply(Project project) {

/**
* Makes dependencies non-transitive.
*
* <p>
* Gradle allows setting all dependencies as non-transitive very easily.
* Sadly this mechanism does not translate into maven pom generation. In order
* to effectively make the pom act as if it has no transitive dependencies,
* we must exclude each transitive dependency of each direct dependency.
*
* <p>
* Determining the transitive deps of a dependency which has been resolved as
* non-transitive is difficult because the process of resolving removes the
* transitive deps. To sidestep this issue, we create a configuration per
Expand Down Expand Up @@ -532,7 +533,6 @@ private static void configureJarManifest(Project project) {

private static void configureJavadoc(Project project) {
project.getTasks().withType(Javadoc.class).configureEach(javadoc -> {

// remove compiled classes from the Javadoc classpath:
// http://mail.openjdk.java.net/pipermail/javadoc-dev/2018-January/000400.html
javadoc.setClasspath(Util.getJavaMainSourceSet(project).get().getCompileClasspath());
Expand All @@ -544,10 +544,15 @@ private static void configureJavadoc(Project project) {
CoreJavadocOptions javadocOptions = (CoreJavadocOptions) javadoc.getOptions();
javadocOptions.addBooleanOption("html5", true);
});

TaskProvider<Javadoc> javadoc = project.getTasks().withType(Javadoc.class).named("javadoc");
javadoc.configure(doc ->
// remove compiled classes from the Javadoc classpath:
// http://mail.openjdk.java.net/pipermail/javadoc-dev/2018-January/000400.html
doc.setClasspath(Util.getJavaMainSourceSet(project).get().getCompileClasspath()));

// ensure javadoc task is run with 'check'
project.getTasks()
.named(LifecycleBasePlugin.CHECK_TASK_NAME)
.configure(t -> t.dependsOn(project.getTasks().withType(Javadoc.class)));
project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME).configure(t -> t.dependsOn(javadoc));
}

static class TestFailureReportingPlugin implements Plugin<Project> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/

package org.elasticsearch.gradle.dependencies;

import org.gradle.api.NamedDomainObjectProvider;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.plugins.JavaPlugin;

public class CompileOnlyResolvePlugin implements Plugin<Project> {
public static final String RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME = "resolveableCompileOnly";

@Override
public void apply(Project project) {
project.getConfigurations().all(configuration -> {
if (configuration.getName().equals(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME)) {
NamedDomainObjectProvider<Configuration> resolvableCompileOnly = project.getConfigurations()
.register(RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME);
resolvableCompileOnly.configure((c) -> {
c.setCanBeResolved(true);
c.setCanBeConsumed(false);
c.extendsFrom(configuration);
});
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.gradle.precommit;

import org.elasticsearch.gradle.dependencies.CompileOnlyResolvePlugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
Expand All @@ -29,13 +30,15 @@ public class DependencyLicensesPrecommitPlugin extends PrecommitPlugin {

@Override
public TaskProvider<? extends Task> createTask(Project project) {
project.getPlugins().apply(CompileOnlyResolvePlugin.class);
TaskProvider<DependencyLicensesTask> dependencyLicenses = project.getTasks()
.register("dependencyLicenses", DependencyLicensesTask.class);

// only require dependency licenses for non-elasticsearch deps
dependencyLicenses.configure(t -> {
Configuration runtimeClasspath = project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
Configuration compileOnly = project.getConfigurations().getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME);
Configuration compileOnly = project.getConfigurations()
.getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME);
t.setDependencies(
runtimeClasspath.fileCollection(dependency -> dependency.getGroup().startsWith("org.elasticsearch") == false)
.minus(compileOnly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.gradle.precommit;

import org.elasticsearch.gradle.ExportElasticsearchBuildResourcesTask;
import org.elasticsearch.gradle.dependencies.CompileOnlyResolvePlugin;
import org.elasticsearch.gradle.info.BuildParams;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand All @@ -28,19 +29,20 @@
public class ThirdPartyAuditPrecommitPlugin extends PrecommitPlugin {
@Override
public TaskProvider<? extends Task> createTask(Project project) {
project.getPlugins().apply(CompileOnlyResolvePlugin.class);
project.getConfigurations().create("forbiddenApisCliJar");
project.getDependencies().add("forbiddenApisCliJar", "de.thetaphi:forbiddenapis:2.7");

TaskProvider<ExportElasticsearchBuildResourcesTask> buildResources = project.getTasks()
.named("buildResources", ExportElasticsearchBuildResourcesTask.class);

TaskProvider<ThirdPartyAuditTask> audit = project.getTasks().register("thirdPartyAudit", ThirdPartyAuditTask.class);
audit.configure(t -> {
t.dependsOn(buildResources);
t.setSignatureFile(buildResources.get().copy("forbidden/third-party-audit.txt"));
t.dependsOn("buildResources");
t.setJavaHome(BuildParams.getRuntimeJavaHome().toString());
t.getTargetCompatibility().set(project.provider(BuildParams::getRuntimeJavaVersion));

});
project.getTasks()
.withType(ExportElasticsearchBuildResourcesTask.class)
.configureEach((br) -> { audit.get().setSignatureFile(br.copy("forbidden/third-party-audit.txt")); });
return audit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.io.output.NullOutputStream;
import org.elasticsearch.gradle.JdkJarHellCheck;
import org.elasticsearch.gradle.OS;
import org.elasticsearch.gradle.dependencies.CompileOnlyResolvePlugin;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
Expand Down Expand Up @@ -175,7 +176,7 @@ public Set<File> getJarsToScan() {
Spec<Dependency> reallyThirdParty = dep -> dep.getGroup() != null && dep.getGroup().startsWith("org.elasticsearch") == false;
Set<File> jars = getRuntimeConfiguration().getResolvedConfiguration().getFiles(reallyThirdParty);
Set<File> compileOnlyConfiguration = getProject().getConfigurations()
.getByName("compileOnly")
.getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME)
.getResolvedConfiguration()
.getFiles(reallyThirdParty);
// don't scan provided dependencies that we already scanned, e.x. don't scan cores dependencies for every plugin
Expand Down Expand Up @@ -329,7 +330,7 @@ private String runForbiddenAPIsCli() throws IOException {
spec.classpath(
getForbiddenAPIsConfiguration(),
getRuntimeConfiguration(),
getProject().getConfigurations().getByName("compileOnly")
getProject().getConfigurations().getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME)
);
spec.jvmArgs("-Xmx1g");
spec.setMain("de.thetaphi.forbiddenapis.cli.CliMain");
Expand Down Expand Up @@ -364,7 +365,7 @@ private Set<String> runJdkJarHellCheck() throws IOException {
spec.classpath(
location.toURI().getPath(),
getRuntimeConfiguration(),
getProject().getConfigurations().getByName("compileOnly")
getProject().getConfigurations().getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME)
);
} catch (URISyntaxException e) {
throw new AssertionError(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ protected void assertTaskUpToDate(BuildResult result, String... taskNames) {
}
}

protected void assertNoDeprecationWarning(BuildResult result) {
assertOutputDoesNotContain(result.getOutput(), "Deprecated Gradle features were used in this build");
}

protected void assertBuildFileExists(BuildResult result, String projectName, String path) {
Path absPath = getBuildDir(projectName).toPath().resolve(path);
assertTrue(
Expand Down
8 changes: 5 additions & 3 deletions buildSrc/src/testKit/thirdPartyAudit/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import org.elasticsearch.gradle.precommit.ThirdPartyAuditPrecommitPlugin
import org.elasticsearch.gradle.precommit.ThirdPartyAuditTask


plugins {
id 'java'
// bring in build-tools onto the classpath
id 'elasticsearch.global-build-info' apply false
}

plugins.apply(ThirdPartyAuditPrecommitPlugin)

repositories {
/**
* Local test repo contains dummy jars with different group names and versions.
Expand All @@ -23,12 +27,10 @@ repositories {
jcenter()
}

configurations.register("forbiddenApisCliJar")

dependencies {
forbiddenApisCliJar 'de.thetaphi:forbiddenapis:2.7'
compileOnly "org.${project.properties.compileOnlyGroup}:${project.properties.compileOnlyVersion}"
compile "org.${project.properties.compileGroup}:${project.properties.compileVersion}"
implementation "org.${project.properties.compileGroup}:${project.properties.compileVersion}"
}

tasks.register("empty", ThirdPartyAuditTask) {
Expand Down

0 comments on commit 9c100b4

Please sign in to comment.