From b9129e633e57fa18b41251b3103705354c6f4e5c Mon Sep 17 00:00:00 2001 From: Eamonn Mansour <47121388+eamansour@users.noreply.github.com> Date: Tue, 30 Jul 2024 12:08:50 +0100 Subject: [PATCH] Add support for Gradle 8 Signed-off-by: Eamonn Mansour <47121388+eamansour@users.noreply.github.com> --- build.gradle | 2 +- dev.galasa.gradle.impl/build.gradle | 8 ++--- .../common/GradleCompatibilityService.java | 17 ++++++++++ .../gradle/common/ICompatibilityService.java | 10 ++++++ .../gradle/hashes/GitHashBuildTask.java | 17 +++++----- .../dev/galasa/gradle/obr/ObrBuildTask.java | 7 ++-- .../java/dev/galasa/gradle/obr/ObrPlugin.java | 32 ++++++++++++++++--- .../testcatalog/TestCatalogBuildTask.java | 5 ++- .../gradle/testcatalog/TestCatalogPlugin.java | 28 ++++++++++++++-- .../gradle/tests/TestCatalogBuildTask.java | 7 ++-- dev.galasa.plugin.common.impl/build.gradle | 2 +- dev.galasa.plugin.common.test/build.gradle | 2 +- dev.galasa.plugin.common/build.gradle | 8 ++--- 13 files changed, 110 insertions(+), 35 deletions(-) create mode 100644 dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/common/GradleCompatibilityService.java create mode 100644 dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/common/ICompatibilityService.java diff --git a/build.gradle b/build.gradle index 7f53f75..2264761 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ group = "dev.galasa" // Note: The following line is changed by the set-version.sh script. // It is also read by other build scrips as required. -version = "0.33.0" +version = "0.36.0" signing { def signingKeyId = findProperty("signingKeyId") diff --git a/dev.galasa.gradle.impl/build.gradle b/dev.galasa.gradle.impl/build.gradle index 406caa7..4b5209e 100644 --- a/dev.galasa.gradle.impl/build.gradle +++ b/dev.galasa.gradle.impl/build.gradle @@ -5,7 +5,7 @@ plugins { } group = "dev.galasa" -version = "0.33.0" +version = "0.36.0" repositories { mavenLocal() @@ -33,9 +33,9 @@ tasks.withType(Javadoc) { } task gitHash(dependsOn : 'classes') { - def dir = file("${buildDir}/githash") - def meta = file("${buildDir}/githash/META-INF") - def result = file("${buildDir}/githash/META-INF/git.hash") + def dir = layout.buildDirectory.dir("githash").get().asFile + def meta = layout.buildDirectory.dir("githash/META-INF").get().asFile + def result = layout.buildDirectory.file("githash/META-INF/git.hash").get().asFile outputs.dir meta tasks.jar.dependsOn('gitHash') diff --git a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/common/GradleCompatibilityService.java b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/common/GradleCompatibilityService.java new file mode 100644 index 0000000..70755bd --- /dev/null +++ b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/common/GradleCompatibilityService.java @@ -0,0 +1,17 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.gradle.common; + +import org.gradle.util.GradleVersion; + +public class GradleCompatibilityService implements ICompatibilityService { + + public static final GradleVersion CURRENT_GRADLE_VERSION = GradleVersion.current(); + + public boolean isCurrentVersionLaterThanGradle8() { + return CURRENT_GRADLE_VERSION.compareTo(GradleVersion.version("8.0")) >= 0; + } +} diff --git a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/common/ICompatibilityService.java b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/common/ICompatibilityService.java new file mode 100644 index 0000000..b8bdc04 --- /dev/null +++ b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/common/ICompatibilityService.java @@ -0,0 +1,10 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.gradle.common; + +public interface ICompatibilityService { + boolean isCurrentVersionLaterThanGradle8(); +} diff --git a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/hashes/GitHashBuildTask.java b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/hashes/GitHashBuildTask.java index 462e754..f84e0a4 100644 --- a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/hashes/GitHashBuildTask.java +++ b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/hashes/GitHashBuildTask.java @@ -13,6 +13,7 @@ import org.gradle.api.DefaultTask; import org.gradle.api.Task; +import org.gradle.api.file.Directory; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskAction; @@ -57,17 +58,15 @@ public void apply() { Jar jarTask = (Jar) getProject().getTasks().getByName("jar"); jarTask.getDependsOn().add(this); - //*** Create the directories so Gradle does not moan - File dirGenTestCatalog = new File(getProject().getBuildDir(),"hashes"); - File dirGenTestCatalogMeta = new File(dirGenTestCatalog,"META-INF"); - //*** dont need to create the file here, just indicate where it will be on the outputs - this.gitHash = new File(dirGenTestCatalogMeta,"git.hash"); + //*** Create the directories so Gradle does not moan) + Directory dirHashes = getProject().getLayout().getBuildDirectory().dir("hashes").get(); + Directory dirHashesMeta = dirHashes.dir("META-INF"); - //*** Tell the JAR task we want to it to include the meta-inf and json file - jarTask.from(dirGenTestCatalog); + this.gitHash = dirHashesMeta.file("git.hash").getAsFile(); + //*** Tell the JAR task we want to it to include the meta-inf and json file + jarTask.from(dirHashes); //*** Mark the meta-inf file as output for caching purposes - getOutputs().dir(dirGenTestCatalog); + getOutputs().dir(dirHashes); } - } diff --git a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/obr/ObrBuildTask.java b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/obr/ObrBuildTask.java index 36a38a3..00eea42 100644 --- a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/obr/ObrBuildTask.java +++ b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/obr/ObrBuildTask.java @@ -12,7 +12,6 @@ import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; @@ -31,7 +30,9 @@ import org.gradle.api.artifacts.ConfigurationContainer; import org.gradle.api.artifacts.ResolvedArtifact; import org.gradle.api.artifacts.ResolvedDependency; +import org.gradle.api.file.RegularFile; import org.gradle.api.logging.Logger; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.TaskAction; public class ObrBuildTask extends DefaultTask { @@ -200,8 +201,8 @@ private void processObr(DataModelHelper obrDataModelHelper, RepositoryImpl newRe public void apply() { // Run during apply phase, need to decide on the output file name - Path buildDirectory = Paths.get(project.getBuildDir().toURI()); - this.pathObr = buildDirectory.resolve("galasa.obr"); + Provider obrFile = project.getLayout().getBuildDirectory().file("galasa.obr"); + this.pathObr = obrFile.get().getAsFile().toPath(); getOutputs().file(pathObr); ConfigurationContainer configurations = project.getConfigurations(); diff --git a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/obr/ObrPlugin.java b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/obr/ObrPlugin.java index 44558dd..96d62a0 100644 --- a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/obr/ObrPlugin.java +++ b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/obr/ObrPlugin.java @@ -8,25 +8,34 @@ import javax.inject.Inject; import org.gradle.api.Plugin; -import org.gradle.api.Project; +import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.ConfigurationContainer; import org.gradle.api.component.AdhocComponentWithVariants; import org.gradle.api.component.SoftwareComponentFactory; import org.gradle.api.internal.artifacts.ArtifactAttributes; import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact; +import org.gradle.api.internal.file.FileResolver; +import org.gradle.api.internal.project.ProjectInternal; +import org.gradle.api.internal.tasks.TaskDependencyFactory; import org.gradle.api.provider.Provider; +import dev.galasa.gradle.common.GradleCompatibilityService; +import dev.galasa.gradle.common.ICompatibilityService; + /** * Generate an OBR */ public class ObrPlugin implements Plugin { private final SoftwareComponentFactory softwareComponentFactory; + private final TaskDependencyFactory taskDependencyFactory; + private final ICompatibilityService compatibilityService = new GradleCompatibilityService(); @Inject - public ObrPlugin(SoftwareComponentFactory softwareComponentFactory) { + public ObrPlugin(SoftwareComponentFactory softwareComponentFactory, TaskDependencyFactory taskDependencyFactory) { this.softwareComponentFactory = softwareComponentFactory; + this.taskDependencyFactory = taskDependencyFactory; } public void apply(Project project) { @@ -58,11 +67,24 @@ private void createObrBuildTask(Project project) { Provider provider = project.getTasks().register("genobr", ObrBuildTask.class, obrTask -> { obrTask.apply(); }); - + // Create the Publish Artifact that the task will be creating and add it the // configuration outbound list - LazyPublishArtifact artifact = new LazyPublishArtifact(provider); - project.getConfigurations().getByName("galasagenobr").getOutgoing().artifact(artifact); + try { + LazyPublishArtifact artifact; + if (compatibilityService.isCurrentVersionLaterThanGradle8()) { + // Create the artifact using the Gradle 8.x constructor + artifact = LazyPublishArtifact.class + .getConstructor(Provider.class, FileResolver.class, TaskDependencyFactory.class) + .newInstance(provider, ((ProjectInternal) project).getFileResolver(), taskDependencyFactory); + } else { + // Create the artifact using the Gradle 6.x/7.x constructor + artifact = LazyPublishArtifact.class.getConstructor(Provider.class).newInstance(provider); + } + project.getConfigurations().getByName("galasagenobr").getOutgoing().artifact(artifact); + } catch (ReflectiveOperationException err) { + throw new IllegalArgumentException("Incompatible LazyPublishArtifact constructor for Gradle version " + project.getGradle().getGradleVersion()); + } } private void createSoftwareComponents(Project project) { diff --git a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/testcatalog/TestCatalogBuildTask.java b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/testcatalog/TestCatalogBuildTask.java index 490d5d0..caea8f4 100644 --- a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/testcatalog/TestCatalogBuildTask.java +++ b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/testcatalog/TestCatalogBuildTask.java @@ -22,6 +22,8 @@ import org.gradle.api.artifacts.ConfigurationContainer; import org.gradle.api.artifacts.ResolvedArtifact; import org.gradle.api.artifacts.ResolvedDependency; +import org.gradle.api.file.RegularFile; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.TaskAction; import com.google.gson.Gson; @@ -201,7 +203,8 @@ private void processBundle(ResolvedDependency dependency) throws TestCatalogExce public void apply() { // Run during apply phase, need to decide on the output file name - this.testCatalog = new File(getProject().getBuildDir(),"testcatalog.json"); + Provider testCatalogFile = getProject().getLayout().getBuildDirectory().file("testcatalog.json"); + this.testCatalog = testCatalogFile.get().getAsFile(); getOutputs().file(testCatalog); ConfigurationContainer configurations = getProject().getConfigurations(); diff --git a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/testcatalog/TestCatalogPlugin.java b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/testcatalog/TestCatalogPlugin.java index 7dd8398..914f4c2 100644 --- a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/testcatalog/TestCatalogPlugin.java +++ b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/testcatalog/TestCatalogPlugin.java @@ -15,19 +15,28 @@ import org.gradle.api.component.SoftwareComponentFactory; import org.gradle.api.internal.artifacts.ArtifactAttributes; import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact; +import org.gradle.api.internal.file.FileResolver; +import org.gradle.api.internal.project.ProjectInternal; +import org.gradle.api.internal.tasks.TaskDependencyFactory; import org.gradle.api.provider.Provider; import org.gradle.api.publish.plugins.PublishingPlugin; +import dev.galasa.gradle.common.GradleCompatibilityService; +import dev.galasa.gradle.common.ICompatibilityService; + /** * Build testcatalog artifact */ public class TestCatalogPlugin implements Plugin { private final SoftwareComponentFactory softwareComponentFactory; + private final TaskDependencyFactory taskDependencyFactory; + private final ICompatibilityService compatibilityService = new GradleCompatibilityService(); @Inject - public TestCatalogPlugin(SoftwareComponentFactory softwareComponentFactory) { + public TestCatalogPlugin(SoftwareComponentFactory softwareComponentFactory, TaskDependencyFactory taskDependencyFactory) { this.softwareComponentFactory = softwareComponentFactory; + this.taskDependencyFactory = taskDependencyFactory; } public void apply(Project project) { @@ -65,8 +74,21 @@ private void createTestcatBuildTask(Project project) { // Create the Publish Artifact that the task will be creating and add it the // configuration outbound list - LazyPublishArtifact artifact = new LazyPublishArtifact(provider); - project.getConfigurations().getByName("galasamergetestcat").getOutgoing().artifact(artifact); + try { + LazyPublishArtifact artifact; + if (compatibilityService.isCurrentVersionLaterThanGradle8()) { + // Create the artifact using the Gradle 8.x constructor + artifact = LazyPublishArtifact.class + .getConstructor(Provider.class, FileResolver.class, TaskDependencyFactory.class) + .newInstance(provider, ((ProjectInternal) project).getFileResolver(), taskDependencyFactory); + } else { + // Create the artifact using the Gradle 6.x/7.x constructor + artifact = LazyPublishArtifact.class.getConstructor(Provider.class).newInstance(provider); + } + project.getConfigurations().getByName("galasamergetestcat").getOutgoing().artifact(artifact); + } catch (ReflectiveOperationException err) { + throw new IllegalArgumentException("Incompatible LazyPublishArtifact constructor for Gradle version " + project.getGradle().getGradleVersion()); + } } private void createTestcatDeployTask(Project project) { diff --git a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/tests/TestCatalogBuildTask.java b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/tests/TestCatalogBuildTask.java index 31f7f05..d1208f6 100644 --- a/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/tests/TestCatalogBuildTask.java +++ b/dev.galasa.gradle.impl/src/main/java/dev/galasa/gradle/tests/TestCatalogBuildTask.java @@ -20,6 +20,7 @@ import org.gradle.api.DefaultTask; import org.gradle.api.Task; +import org.gradle.api.file.Directory; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskAction; @@ -252,10 +253,10 @@ public void apply() { jarTask.getDependsOn().add(this); //*** Create the directories so Gradle does not moan - File dirGenTestCatalog = new File(getProject().getBuildDir(),"gentestcatalog"); - File dirGenTestCatalogMeta = new File(dirGenTestCatalog,"META-INF"); + Directory dirGenTestCatalog = getProject().getLayout().getBuildDirectory().dir("gentestcatalog").get(); + Directory dirGenTestCatalogMeta = dirGenTestCatalog.dir("META-INF"); //*** dont need to create the file here, just indicate where it will be on the outputs - this.testCatalog = new File(dirGenTestCatalogMeta,"testcatalog.json"); + this.testCatalog = dirGenTestCatalogMeta.file("testcatalog.json").getAsFile(); //*** Tell the JAR task we want to it to include the meta-inf and json file jarTask.from(dirGenTestCatalog); diff --git a/dev.galasa.plugin.common.impl/build.gradle b/dev.galasa.plugin.common.impl/build.gradle index c9a7e0d..a6f9d14 100644 --- a/dev.galasa.plugin.common.impl/build.gradle +++ b/dev.galasa.plugin.common.impl/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "dev.galasa" -version = "0.33.0" +version = "0.36.0" repositories { maven { diff --git a/dev.galasa.plugin.common.test/build.gradle b/dev.galasa.plugin.common.test/build.gradle index 7b31e5c..8d9ac61 100644 --- a/dev.galasa.plugin.common.test/build.gradle +++ b/dev.galasa.plugin.common.test/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "dev.galasa" -version = "0.33.0" +version = "0.36.0" repositories { maven { diff --git a/dev.galasa.plugin.common/build.gradle b/dev.galasa.plugin.common/build.gradle index d9a6c8f..47f7751 100644 --- a/dev.galasa.plugin.common/build.gradle +++ b/dev.galasa.plugin.common/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "dev.galasa" -version = "0.33.0" +version = "0.36.0" repositories { maven { @@ -31,9 +31,9 @@ tasks.withType(Javadoc) { } task gitHash(dependsOn : 'classes') { - def dir = file("${buildDir}/githash") - def meta = file("${buildDir}/githash/META-INF") - def result = file("${buildDir}/githash/META-INF/git.hash") + def dir = layout.buildDirectory.dir("githash").get().asFile + def meta = layout.buildDirectory.dir("githash/META-INF").get().asFile + def result = layout.buildDirectory.file("githash/META-INF/git.hash").get().asFile outputs.dir meta tasks.jar.dependsOn('gitHash')