Skip to content

Commit 8af4647

Browse files
authored
Signed-off-by: Andriy Redko <[email protected]>
1 parent 18c5bb6 commit 8af4647

32 files changed

+467
-77
lines changed

buildSrc/src/main/java/org/opensearch/gradle/info/GlobalBuildInfoPlugin.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,28 @@ private JavaVersion determineJavaVersion(String description, File javaHome, Java
199199
}
200200

201201
private JvmInstallationMetadata getJavaInstallation(File javaHome) {
202-
final InstallationLocation location = new InstallationLocation(javaHome, "Java home");
202+
InstallationLocation location = null;
203+
204+
try {
205+
try {
206+
// The InstallationLocation(File, String) is used by Gradle pre-8.8
207+
location = (InstallationLocation) MethodHandles.publicLookup()
208+
.findConstructor(InstallationLocation.class, MethodType.methodType(void.class, File.class, String.class))
209+
.invokeExact(javaHome, "Java home");
210+
} catch (Throwable ex) {
211+
// The InstallationLocation::userDefined is used by Gradle post-8.7
212+
location = (InstallationLocation) MethodHandles.publicLookup()
213+
.findStatic(
214+
InstallationLocation.class,
215+
"userDefined",
216+
MethodType.methodType(InstallationLocation.class, File.class, String.class)
217+
)
218+
.invokeExact(javaHome, "Java home");
219+
220+
}
221+
} catch (Throwable ex) {
222+
throw new IllegalStateException("Unable to find suitable InstallationLocation constructor / factory method", ex);
223+
}
203224

204225
try {
205226
try {

buildSrc/src/main/java/org/opensearch/gradle/internal/InternalDistributionArchiveSetupPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ private void configureGeneralTaskDefaults(Project project) {
148148
project.getTasks().withType(AbstractCopyTask.class).configureEach(t -> {
149149
t.dependsOn(project.getTasks().withType(EmptyDirTask.class));
150150
t.setIncludeEmptyDirs(true);
151-
t.setDirMode(0755);
152-
t.setFileMode(0644);
151+
t.dirPermissions(perms -> perms.unix(0755));
152+
t.filePermissions(perms -> perms.unix(0644));
153153
});
154154

155155
// common config across all archives

buildSrc/src/main/java/org/opensearch/gradle/precommit/DependencyLicensesPrecommitPlugin.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
package org.opensearch.gradle.precommit;
3434

3535
import org.opensearch.gradle.dependencies.CompileOnlyResolvePlugin;
36+
import org.opensearch.gradle.util.GradleUtils;
3637
import org.gradle.api.Project;
3738
import org.gradle.api.Task;
3839
import org.gradle.api.artifacts.Configuration;
3940
import org.gradle.api.artifacts.ProjectDependency;
41+
import org.gradle.api.file.FileCollection;
4042
import org.gradle.api.plugins.JavaPlugin;
43+
import org.gradle.api.provider.Provider;
4144
import org.gradle.api.tasks.TaskProvider;
4245

4346
public class DependencyLicensesPrecommitPlugin extends PrecommitPlugin {
@@ -48,15 +51,16 @@ public TaskProvider<? extends Task> createTask(Project project) {
4851
TaskProvider<DependencyLicensesTask> dependencyLicenses = project.getTasks()
4952
.register("dependencyLicenses", DependencyLicensesTask.class);
5053

54+
final Configuration runtimeClasspath = project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
55+
final Configuration compileOnly = project.getConfigurations()
56+
.getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME);
57+
final Provider<FileCollection> provider = project.provider(
58+
() -> GradleUtils.getFiles(project, runtimeClasspath, dependency -> dependency instanceof ProjectDependency == false)
59+
.minus(compileOnly)
60+
);
61+
5162
// only require dependency licenses for non-opensearch deps
52-
dependencyLicenses.configure(t -> {
53-
Configuration runtimeClasspath = project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
54-
Configuration compileOnly = project.getConfigurations()
55-
.getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME);
56-
t.setDependencies(
57-
runtimeClasspath.fileCollection(dependency -> dependency instanceof ProjectDependency == false).minus(compileOnly)
58-
);
59-
});
63+
dependencyLicenses.configure(t -> t.getDependencies().set(provider));
6064

6165
// we also create the updateShas helper task that is associated with dependencyLicenses
6266
project.getTasks().register("updateShas", UpdateShasTask.class, t -> t.setParentTask(dependencyLicenses));

buildSrc/src/main/java/org/opensearch/gradle/precommit/DependencyLicensesTask.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.gradle.api.file.FileCollection;
4040
import org.gradle.api.logging.Logger;
4141
import org.gradle.api.logging.Logging;
42+
import org.gradle.api.provider.Property;
4243
import org.gradle.api.tasks.Input;
4344
import org.gradle.api.tasks.InputDirectory;
4445
import org.gradle.api.tasks.InputFiles;
@@ -121,7 +122,7 @@ public class DependencyLicensesTask extends DefaultTask {
121122
/**
122123
* A collection of jar files that should be checked.
123124
*/
124-
private FileCollection dependencies;
125+
private Property<FileCollection> dependenciesProvider;
125126

126127
/**
127128
* The directory to find the license and sha files in.
@@ -158,12 +159,11 @@ public void mapping(Map<String, String> props) {
158159
}
159160

160161
@InputFiles
161-
public FileCollection getDependencies() {
162-
return dependencies;
163-
}
164-
165-
public void setDependencies(FileCollection dependencies) {
166-
this.dependencies = dependencies;
162+
public Property<FileCollection> getDependencies() {
163+
if (dependenciesProvider == null) {
164+
dependenciesProvider = getProject().getObjects().property(FileCollection.class);
165+
}
166+
return dependenciesProvider;
167167
}
168168

169169
@Optional
@@ -190,6 +190,11 @@ public void ignoreSha(String dep) {
190190

191191
@TaskAction
192192
public void checkDependencies() throws IOException, NoSuchAlgorithmException {
193+
if (dependenciesProvider == null) {
194+
throw new GradleException("No dependencies variable defined.");
195+
}
196+
197+
final FileCollection dependencies = dependenciesProvider.get();
193198
if (dependencies == null) {
194199
throw new GradleException("No dependencies variable defined.");
195200
}
@@ -226,7 +231,7 @@ public void checkDependencies() throws IOException, NoSuchAlgorithmException {
226231
}
227232
}
228233

229-
checkDependencies(licenses, notices, sources, shaFiles);
234+
checkDependencies(dependencies, licenses, notices, sources, shaFiles);
230235

231236
licenses.forEach((item, exists) -> failIfAnyMissing(item, exists, "license"));
232237

@@ -255,6 +260,7 @@ private void failIfAnyMissing(String item, Boolean exists, String type) {
255260
}
256261

257262
private void checkDependencies(
263+
FileCollection dependencies,
258264
Map<String, Boolean> licenses,
259265
Map<String, Boolean> notices,
260266
Map<String, Boolean> sources,

buildSrc/src/main/java/org/opensearch/gradle/precommit/ThirdPartyAuditTask.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.opensearch.gradle.LoggedExec;
3838
import org.opensearch.gradle.OS;
3939
import org.opensearch.gradle.dependencies.CompileOnlyResolvePlugin;
40+
import org.opensearch.gradle.util.GradleUtils;
4041
import org.gradle.api.DefaultTask;
4142
import org.gradle.api.JavaVersion;
4243
import org.gradle.api.artifacts.Configuration;
@@ -203,11 +204,13 @@ public Set<File> getJarsToScan() {
203204
// or dependencies added as `files(...)`, we can't be sure if those are third party or not.
204205
// err on the side of scanning these to make sure we don't miss anything
205206
Spec<Dependency> reallyThirdParty = dep -> dep.getGroup() != null && dep.getGroup().startsWith("org.opensearch") == false;
206-
Set<File> jars = getRuntimeConfiguration().getResolvedConfiguration().getFiles(reallyThirdParty);
207-
Set<File> compileOnlyConfiguration = getProject().getConfigurations()
208-
.getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME)
209-
.getResolvedConfiguration()
210-
.getFiles(reallyThirdParty);
207+
208+
Set<File> jars = GradleUtils.getFiles(getProject(), getRuntimeConfiguration(), reallyThirdParty).getFiles();
209+
Set<File> compileOnlyConfiguration = GradleUtils.getFiles(
210+
getProject(),
211+
getProject().getConfigurations().getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME),
212+
reallyThirdParty
213+
).getFiles();
211214
// don't scan provided dependencies that we already scanned, e.x. don't scan cores dependencies for every plugin
212215
if (compileOnlyConfiguration != null) {
213216
jars.removeAll(compileOnlyConfiguration);

buildSrc/src/main/java/org/opensearch/gradle/precommit/UpdateShasTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public UpdateShasTask() {
6666
public void updateShas() throws NoSuchAlgorithmException, IOException {
6767
Set<File> shaFiles = parentTask.get().getShaFiles();
6868

69-
for (File dependency : parentTask.get().getDependencies()) {
69+
for (File dependency : parentTask.get().getDependencies().get()) {
7070
String jarName = dependency.getName();
7171
File shaFile = parentTask.get().getShaFile(jarName);
7272

buildSrc/src/main/java/org/opensearch/gradle/tar/SymbolicLinkPreservingTar.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private void visitSymbolicLink(final FileCopyDetailsInternal details) {
184184
visitedSymbolicLinks.add(details.getFile());
185185
final TarArchiveEntry entry = new TarArchiveEntry(details.getRelativePath().getPathString(), TarConstants.LF_SYMLINK);
186186
entry.setModTime(getModTime(details));
187-
entry.setMode(UnixStat.LINK_FLAG | details.getMode());
187+
entry.setMode(UnixStat.LINK_FLAG | details.getPermissions().toUnixNumeric());
188188
try {
189189
entry.setLinkName(Files.readSymbolicLink(details.getFile().toPath()).toString());
190190
tar.putArchiveEntry(entry);
@@ -197,7 +197,7 @@ private void visitSymbolicLink(final FileCopyDetailsInternal details) {
197197
private void visitDirectory(final FileCopyDetailsInternal details) {
198198
final TarArchiveEntry entry = new TarArchiveEntry(details.getRelativePath().getPathString() + "/");
199199
entry.setModTime(getModTime(details));
200-
entry.setMode(UnixStat.DIR_FLAG | details.getMode());
200+
entry.setMode(UnixStat.DIR_FLAG | details.getPermissions().toUnixNumeric());
201201
try {
202202
tar.putArchiveEntry(entry);
203203
tar.closeArchiveEntry();
@@ -209,7 +209,7 @@ private void visitDirectory(final FileCopyDetailsInternal details) {
209209
private void visitFile(final FileCopyDetailsInternal details) {
210210
final TarArchiveEntry entry = new TarArchiveEntry(details.getRelativePath().getPathString());
211211
entry.setModTime(getModTime(details));
212-
entry.setMode(UnixStat.FILE_FLAG | details.getMode());
212+
entry.setMode(UnixStat.FILE_FLAG | details.getPermissions().toUnixNumeric());
213213
entry.setSize(details.getSize());
214214
try {
215215
tar.putArchiveEntry(entry);

buildSrc/src/main/java/org/opensearch/gradle/util/GradleUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@
3939
import org.gradle.api.UnknownTaskException;
4040
import org.gradle.api.artifacts.Configuration;
4141
import org.gradle.api.artifacts.Dependency;
42+
import org.gradle.api.artifacts.LenientConfiguration;
43+
import org.gradle.api.file.FileCollection;
44+
import org.gradle.api.internal.artifacts.ivyservice.ResolvedFilesCollectingVisitor;
45+
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.SelectedArtifactSet;
4246
import org.gradle.api.plugins.JavaBasePlugin;
4347
import org.gradle.api.plugins.JavaPluginExtension;
4448
import org.gradle.api.provider.Provider;
4549
import org.gradle.api.services.BuildService;
4650
import org.gradle.api.services.BuildServiceRegistration;
4751
import org.gradle.api.services.BuildServiceRegistry;
52+
import org.gradle.api.specs.Spec;
4853
import org.gradle.api.tasks.SourceSet;
4954
import org.gradle.api.tasks.SourceSetContainer;
5055
import org.gradle.api.tasks.TaskContainer;
@@ -53,6 +58,9 @@
5358
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
5459
import org.gradle.plugins.ide.idea.model.IdeaModel;
5560

61+
import java.lang.invoke.MethodHandle;
62+
import java.lang.invoke.MethodHandles;
63+
import java.lang.invoke.MethodType;
5664
import java.util.ArrayList;
5765
import java.util.Arrays;
5866
import java.util.Collection;
@@ -245,4 +253,22 @@ public static String getProjectPathFromTask(String taskPath) {
245253
int lastDelimiterIndex = taskPath.lastIndexOf(":");
246254
return lastDelimiterIndex == 0 ? ":" : taskPath.substring(0, lastDelimiterIndex);
247255
}
256+
257+
public static FileCollection getFiles(Project project, Configuration cfg, Spec<Dependency> spec) {
258+
final LenientConfiguration configuration = cfg.getResolvedConfiguration().getLenientConfiguration();
259+
try {
260+
// Using reflection here to cover the pre 8.7 releases (since those have no such APIs), the
261+
// ResolverResults.LegacyResolverResults.LegacyVisitedArtifactSet::select(...) is not available
262+
// on older versions.
263+
final MethodHandle mh = MethodHandles.lookup()
264+
.findVirtual(configuration.getClass(), "select", MethodType.methodType(SelectedArtifactSet.class, Spec.class))
265+
.bindTo(configuration);
266+
267+
final ResolvedFilesCollectingVisitor visitor = new ResolvedFilesCollectingVisitor();
268+
((SelectedArtifactSet) mh.invoke(spec)).visitArtifacts(visitor, false);
269+
return project.files(visitor.getFiles());
270+
} catch (Throwable ex) {
271+
return project.files(configuration.getFiles(spec));
272+
}
273+
}
248274
}

buildSrc/src/test/java/org/opensearch/gradle/precommit/DependencyLicensesTaskTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private TaskProvider<DependencyLicensesTask> createDependencyLicensesTask(Projec
344344
.register("dependencyLicenses", DependencyLicensesTask.class, new Action<DependencyLicensesTask>() {
345345
@Override
346346
public void execute(DependencyLicensesTask dependencyLicensesTask) {
347-
dependencyLicensesTask.setDependencies(getDependencies(project));
347+
dependencyLicensesTask.getDependencies().set(getDependencies(project));
348348

349349
final Map<String, String> mappings = new HashMap<>();
350350
mappings.put("from", "groovy-.*");

buildSrc/src/test/java/org/opensearch/gradle/precommit/UpdateShasTaskTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void whenDependencyExistsButShaNotThenShouldCreateNewShaFile() throws IOE
102102
public void whenDependencyAndWrongShaExistsThenShouldNotOverwriteShaFile() throws IOException, NoSuchAlgorithmException {
103103
project.getDependencies().add("someCompileConfiguration", dependency);
104104

105-
File groovyJar = task.getParentTask().getDependencies().getFiles().iterator().next();
105+
File groovyJar = task.getParentTask().getDependencies().get().getFiles().iterator().next();
106106
String groovyShaName = groovyJar.getName() + ".sha1";
107107

108108
File groovySha = createFileIn(getLicensesDir(project), groovyShaName, "content");
@@ -162,7 +162,7 @@ private TaskProvider<DependencyLicensesTask> createDependencyLicensesTask(Projec
162162
.register("dependencyLicenses", DependencyLicensesTask.class, new Action<DependencyLicensesTask>() {
163163
@Override
164164
public void execute(DependencyLicensesTask dependencyLicensesTask) {
165-
dependencyLicensesTask.setDependencies(getDependencies(project));
165+
dependencyLicensesTask.getDependencies().set(getDependencies(project));
166166
}
167167
});
168168

distribution/archives/build.gradle

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ CopySpec archiveFiles(CopySpec modulesFiles, String distributionType, String pla
3939
with libFiles()
4040
}
4141
into('config') {
42-
dirMode 0750
43-
fileMode 0660
42+
dirPermissions {
43+
unix 0750
44+
}
45+
filePermissions {
46+
unix 0660
47+
}
4448
with configFiles(distributionType, java)
4549
from {
46-
dirMode 0750
50+
dirPermissions {
51+
unix 0750
52+
}
4753
jvmOptionsDir.getParent()
4854
}
4955
}
@@ -61,13 +67,17 @@ CopySpec archiveFiles(CopySpec modulesFiles, String distributionType, String pla
6167
}
6268
into('') {
6369
from {
64-
dirMode 0755
70+
dirPermissions {
71+
unix 0755
72+
}
6573
logsDir.getParent()
6674
}
6775
}
6876
into('') {
6977
from {
70-
dirMode 0755
78+
dirPermissions {
79+
unix 0755
80+
}
7181
pluginsDir.getParent()
7282
}
7383
}

distribution/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,9 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
363363
if (it.relativePath.segments[-2] == 'bin' || ((platform == 'darwin-x64' || platform == 'darwin-arm64') && it.relativePath.segments[-2] == 'MacOS')) {
364364
// bin files, wherever they are within modules (eg platform specific) should be executable
365365
// and MacOS is an alternative to bin on macOS
366-
it.mode = 0755
366+
it.permissions(perm -> perm.unix(0755))
367367
} else {
368-
it.mode = 0644
368+
it.permissions(perm -> perm.unix(0644))
369369
}
370370
}
371371
def buildModules = buildModulesTaskProvider
@@ -413,7 +413,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
413413
from '../src/bin'
414414
exclude '*.exe'
415415
exclude '*.bat'
416-
eachFile { it.setMode(0755) }
416+
eachFile { it.permissions(perm -> perm.unix(0755)) }
417417
MavenFilteringHack.filter(it, expansionsForDistribution(distributionType, java))
418418
}
419419
// windows files, only for zip
@@ -431,7 +431,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
431431
}
432432
// module provided bin files
433433
with copySpec {
434-
eachFile { it.setMode(0755) }
434+
eachFile { it.permissions(perm -> perm.unix(0755)) }
435435
from project(':distribution').buildBin
436436
if (distributionType != 'zip') {
437437
exclude '*.bat'
@@ -473,7 +473,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
473473
}
474474
eachFile { FileCopyDetails details ->
475475
if (details.relativePath.segments[-2] == 'bin' || details.relativePath.segments[-1] == 'jspawnhelper') {
476-
details.mode = 0755
476+
details.permissions(perm -> perm.unix(0755))
477477
}
478478
if (details.name == 'src.zip') {
479479
details.exclude()
@@ -501,7 +501,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
501501
}
502502
eachFile { FileCopyDetails details ->
503503
if (details.relativePath.segments[-2] == 'bin' || details.relativePath.segments[-1] == 'jspawnhelper') {
504-
details.mode = 0755
504+
details.permissions(perm -> perm.unix(0755))
505505
}
506506
}
507507
}

0 commit comments

Comments
 (0)