Skip to content

Commit 3c70818

Browse files
committed
Fix interdependency to Licenses task
1 parent bf567c5 commit 3c70818

File tree

14 files changed

+94
-53
lines changed

14 files changed

+94
-53
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal;
11+
12+
import org.gradle.api.DefaultTask;
13+
import org.gradle.api.InvalidUserDataException;
14+
import org.gradle.api.provider.MapProperty;
15+
import org.gradle.api.tasks.Input;
16+
import org.gradle.api.tasks.Optional;
17+
18+
import java.util.Map;
19+
20+
public abstract class AbstractDependenciesTask extends DefaultTask {
21+
22+
@Input
23+
@Optional
24+
public abstract MapProperty<String, String> getMappings();
25+
26+
/**
27+
* Add a mapping from a regex pattern for the jar name, to a prefix to find
28+
* the LICENSE and NOTICE file for that jar.
29+
*/
30+
public void mapping(Map<String, String> props) {
31+
String from = props.get("from");
32+
if (from == null) {
33+
throw new InvalidUserDataException("Missing \"from\" setting for license name mapping");
34+
}
35+
String to = props.get("to");
36+
if (to == null) {
37+
throw new InvalidUserDataException("Missing \"to\" setting for license name mapping");
38+
}
39+
if (props.size() > 2) {
40+
throw new InvalidUserDataException("Unknown properties for mapping on dependencyLicenses: " + props.keySet());
41+
}
42+
getMappings().put(from, to);
43+
}
44+
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DependenciesInfoTask.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,23 @@
1717
import org.gradle.api.file.ConfigurableFileCollection;
1818
import org.gradle.api.file.DirectoryProperty;
1919
import org.gradle.api.file.ProjectLayout;
20-
import org.gradle.api.internal.ConventionTask;
2120
import org.gradle.api.model.ObjectFactory;
22-
import org.gradle.api.provider.MapProperty;
2321
import org.gradle.api.provider.Property;
2422
import org.gradle.api.provider.Provider;
2523
import org.gradle.api.provider.ProviderFactory;
24+
import org.gradle.api.tasks.CacheableTask;
25+
import org.gradle.api.tasks.Classpath;
2626
import org.gradle.api.tasks.Input;
2727
import org.gradle.api.tasks.InputDirectory;
2828
import org.gradle.api.tasks.InputFiles;
2929
import org.gradle.api.tasks.Internal;
3030
import org.gradle.api.tasks.Optional;
3131
import org.gradle.api.tasks.OutputFile;
32+
import org.gradle.api.tasks.PathSensitive;
33+
import org.gradle.api.tasks.PathSensitivity;
3234
import org.gradle.api.tasks.TaskAction;
3335
import org.gradle.internal.component.external.model.ModuleComponentArtifactIdentifier;
36+
import org.gradle.model.Path;
3437

3538
import java.io.File;
3639
import java.io.IOException;
@@ -56,7 +59,8 @@
5659
* <li>license: <a href="https://spdx.org/licenses/">SPDX license</a> identifier, custom license or UNKNOWN.</li>
5760
* </ul>
5861
*/
59-
public abstract class DependenciesInfoTask extends ConventionTask {
62+
@CacheableTask
63+
public abstract class DependenciesInfoTask extends AbstractDependenciesTask {
6064

6165
@Inject
6266
public abstract ProviderFactory getProviderFactory();
@@ -87,7 +91,7 @@ public Provider<Set<ModuleComponentIdentifier>> getCompileOnlyModules() {
8791
* artifact transforms that might be applied and fail due to missing task dependency to jar
8892
* generating tasks.
8993
* */
90-
@InputFiles
94+
@Classpath
9195
abstract ConfigurableFileCollection getClasspath();
9296

9397
private Provider<Set<ModuleComponentIdentifier>> mapToModuleComponentIdentifiers(ArtifactCollection artifacts) {
@@ -113,6 +117,7 @@ private Provider<Set<ModuleComponentIdentifier>> mapToModuleComponentIdentifiers
113117
* Directory to read license files
114118
*/
115119
@Optional
120+
@PathSensitive(PathSensitivity.RELATIVE)
116121
@InputDirectory
117122
public File getLicensesDir() {
118123
File asFile = licensesDir.get().getAsFile();
@@ -165,6 +170,8 @@ public void generateDependenciesInfo() throws IOException {
165170
}
166171

167172
final String url = createURL(dep.getGroup(), moduleName, dep.getVersion());
173+
System.out.println("moduleName = " + moduleName);
174+
System.out.println("mappings = " + mappings);
168175
final String dependencyName = DependencyLicensesTask.getDependencyName(mappings, moduleName);
169176
getLogger().info("mapped dependency " + dep.getGroup() + ":" + moduleName + " to " + dependencyName + " for license info");
170177

@@ -175,10 +182,6 @@ public void generateDependenciesInfo() throws IOException {
175182
Files.writeString(outputFile.toPath(), output.toString(), StandardOpenOption.CREATE);
176183
}
177184

178-
@Input
179-
@Optional
180-
public abstract MapProperty<String, String> getMappings();
181-
182185
/**
183186
* Create an URL on <a href="https://repo1.maven.org/maven2/">Maven Central</a>
184187
* based on dependency coordinates.

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
*/
99
package org.elasticsearch.gradle.internal.precommit;
1010

11+
import org.elasticsearch.gradle.internal.AbstractDependenciesTask;
1112
import org.elasticsearch.gradle.internal.precommit.LicenseAnalyzer.LicenseInfo;
12-
import org.gradle.api.DefaultTask;
1313
import org.gradle.api.GradleException;
14-
import org.gradle.api.InvalidUserDataException;
1514
import org.gradle.api.artifacts.Configuration;
1615
import org.gradle.api.artifacts.component.ComponentIdentifier;
1716
import org.gradle.api.file.Directory;
@@ -39,7 +38,6 @@
3938
import java.util.Arrays;
4039
import java.util.HashMap;
4140
import java.util.HashSet;
42-
import java.util.LinkedHashMap;
4341
import java.util.LinkedHashSet;
4442
import java.util.List;
4543
import java.util.Map;
@@ -94,7 +92,7 @@
9492
* comply with the license terms.
9593
*/
9694
@CacheableTask
97-
public abstract class DependencyLicensesTask extends DefaultTask {
95+
public abstract class DependencyLicensesTask extends AbstractDependenciesTask {
9896

9997
private final Pattern regex = Pattern.compile("-v?\\d+.*");
10098

@@ -112,11 +110,6 @@ public abstract class DependencyLicensesTask extends DefaultTask {
112110
*/
113111
private final DirectoryProperty licensesDir;
114112

115-
/**
116-
* A map of patterns to prefix, used to find the LICENSE and NOTICE file.
117-
*/
118-
private Map<String, String> mappings = new LinkedHashMap<>();
119-
120113
/**
121114
* Names of dependencies whose shas should not exist.
122115
*/
@@ -128,24 +121,7 @@ public abstract class DependencyLicensesTask extends DefaultTask {
128121
private LinkedHashSet<String> ignoreFiles = new LinkedHashSet<>();
129122
private ProjectLayout projectLayout;
130123

131-
/**
132-
* Add a mapping from a regex pattern for the jar name, to a prefix to find
133-
* the LICENSE and NOTICE file for that jar.
134-
*/
135-
public void mapping(Map<String, String> props) {
136-
String from = props.get("from");
137-
if (from == null) {
138-
throw new InvalidUserDataException("Missing \"from\" setting for license name mapping");
139-
}
140-
String to = props.get("to");
141-
if (to == null) {
142-
throw new InvalidUserDataException("Missing \"to\" setting for license name mapping");
143-
}
144-
if (props.size() > 2) {
145-
throw new InvalidUserDataException("Unknown properties for mapping on dependencyLicenses: " + props.keySet());
146-
}
147-
mappings.put(from, to);
148-
}
124+
149125

150126
@Inject
151127
public DependencyLicensesTask(ObjectFactory objects, ProjectLayout projectLayout) {
@@ -267,7 +243,7 @@ private void checkDependencies(Map<String, Boolean> licenses, Map<String, Boolea
267243
for (File dependency : dependencies) {
268244
String jarName = dependency.getName();
269245
String depName = regex.matcher(jarName).replaceFirst("");
270-
String dependencyName = getDependencyName(mappings, depName);
246+
String dependencyName = getDependencyName(getMappings().get(), depName);
271247
logger.info("mapped dependency name {} to {} for license/notice check", depName, dependencyName);
272248
checkFile(dependencyName, jarName, licenses, "LICENSE");
273249
checkFile(dependencyName, jarName, notices, "NOTICE");
@@ -321,11 +297,6 @@ public LinkedHashSet<String> getIgnoreFiles() {
321297
return new LinkedHashSet<>(ignoreFiles);
322298
}
323299

324-
@Input
325-
public LinkedHashMap<String, String> getMappings() {
326-
return new LinkedHashMap<>(mappings);
327-
}
328-
329300
/**
330301
* Convencience method for configuring dependencies to be checked and ignoring transitive dependencies for now.
331302
* */

client/sniffer/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* under the License.
1818
*/
1919
import org.elasticsearch.gradle.internal.conventions.precommit.LicenseHeadersTask
20+
import org.elasticsearch.gradle.internal.AbstractDependenciesTask
2021

2122
apply plugin: 'elasticsearch.build'
2223
apply plugin: 'elasticsearch.publish'
@@ -67,7 +68,7 @@ tasks.named('forbiddenApisTest').configure {
6768
replaceSignatureFiles 'jdk-signatures'
6869
}
6970

70-
tasks.named("dependencyLicenses").configure {
71+
tasks.withType(AbstractDependenciesTask).configureEach {
7172
mapping from: /http.*/, to: 'httpclient'
7273
mapping from: /commons-.*/, to: 'commons'
7374
}

distribution/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ tasks.register("generateDependenciesReport", ConcatFilesTask) {
8383
'https://oss-dependencies.elastic.co/red-hat-universal-base-image-minimal/9/ubi-minimal-9-source.tar.gz'
8484
]
8585
additionalLines << rhelUbiFields.join(',')
86+
doLast {
87+
if(target.text.readLines().size() < 100) {
88+
throw new GradleException("Suspiciously low number of dependencies. Double check.")
89+
}
90+
}
8691
}
8792

8893
/*****************************************************************************

modules/ingest-geoip/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import org.elasticsearch.gradle.OS
11+
import org.elasticsearch.gradle.internal.AbstractDependenciesTask
1112

1213
apply plugin: 'elasticsearch.internal-yaml-rest-test'
1314
apply plugin: 'elasticsearch.yaml-rest-compat-test'
@@ -81,10 +82,13 @@ tasks.named("forbiddenPatterns").configure {
8182
exclude '**/*.mmdb'
8283
}
8384

84-
tasks.named("dependencyLicenses").configure {
85+
tasks.withType(AbstractDependenciesTask).configureEach {
8586
mapping from: /geoip.*/, to: 'maxmind-geolite2-eula'
8687
mapping from: /maxmind-db.*/, to: 'maxmind-db-reader'
8788
mapping from: /jackson.*/, to: 'jackson'
89+
}
90+
91+
tasks.named("dependencyLicenses").configure {
8892
ignoreFile 'elastic-geoip-database-service-agreement-LICENSE.txt'
8993
}
9094

modules/repository-s3/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
import org.apache.tools.ant.filters.ReplaceTokens
1010
import org.elasticsearch.gradle.internal.test.InternalClusterTestPlugin
11+
import org.elasticsearch.gradle.internal.AbstractDependenciesTask
1112

1213
apply plugin: 'elasticsearch.internal-yaml-rest-test'
1314
apply plugin: 'elasticsearch.internal-cluster-test'
@@ -89,7 +90,7 @@ restResources {
8990
}
9091
}
9192

92-
tasks.named("dependencyLicenses").configure {
93+
tasks.withType(AbstractDependenciesTask).configureEach {
9394
mapping from: 'annotations', to: 'aws-sdk-2'
9495
mapping from: 'apache-client', to: 'aws-sdk-2'
9596
mapping from: 'arns', to: 'aws-sdk-2'

plugins/analysis-ukrainian/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* your election, the "Elastic License 2.0", the "GNU Affero General Public
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
9+
import org.elasticsearch.gradle.internal.AbstractDependenciesTask
10+
911
apply plugin: 'elasticsearch.internal-yaml-rest-test'
1012
apply plugin: 'elasticsearch.yaml-rest-compat-test'
1113

@@ -27,7 +29,7 @@ restResources {
2729
}
2830
}
2931

30-
tasks.named("dependencyLicenses").configure {
32+
tasks.withType(AbstractDependenciesTask).configureEach {
3133
mapping from: /lucene-.*/, to: 'lucene'
3234
mapping from: /morfologik-.*/, to: 'lucene'
3335
}

plugins/discovery-ec2/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* your election, the "Elastic License 2.0", the "GNU Affero General Public
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
9+
import org.elasticsearch.gradle.internal.AbstractDependenciesTask
10+
911
apply plugin: 'elasticsearch.internal-cluster-test'
1012
apply plugin: 'elasticsearch.internal-java-rest-test'
1113
apply plugin: 'elasticsearch.internal-cluster-test'
@@ -16,7 +18,6 @@ esplugin {
1618
}
1719

1820
dependencies {
19-
2021
implementation "software.amazon.awssdk:annotations:${versions.awsv2sdk}"
2122
implementation "software.amazon.awssdk:apache-client:${versions.awsv2sdk}"
2223
implementation "software.amazon.awssdk:auth:${versions.awsv2sdk}"
@@ -67,7 +68,7 @@ dependencies {
6768
internalClusterTestImplementation project(':test:fixtures:ec2-imds-fixture')
6869
}
6970

70-
tasks.named("dependencyLicenses").configure {
71+
tasks.withType(AbstractDependenciesTask).configureEach {
7172
mapping from: 'annotations', to: 'aws-sdk-2'
7273
mapping from: 'apache-client', to: 'aws-sdk-2'
7374
mapping from: 'auth', to: 'aws-sdk-2'

x-pack/libs/es-opensaml-security-api/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* 2.0.
66
*/
77

8+
import org.elasticsearch.gradle.internal.AbstractDependenciesTask
9+
810
apply plugin: 'elasticsearch.build'
911
apply plugin: 'elasticsearch.publish'
1012
apply plugin: 'com.gradleup.shadow'
@@ -20,7 +22,7 @@ dependencies {
2022
}
2123
}
2224

23-
tasks.named("dependencyLicenses").configure {
25+
tasks.withType(AbstractDependenciesTask).configureEach {
2426
mapping from: /opensaml-.*/, to: 'shibboleth'
2527
}
2628

0 commit comments

Comments
 (0)