|
8 | 8 | import javax.inject.Inject;
|
9 | 9 |
|
10 | 10 | import org.gradle.api.Plugin;
|
11 |
| -import org.gradle.api.Project; |
| 11 | +import org.gradle.api.Project; |
12 | 12 | import org.gradle.api.artifacts.Configuration;
|
13 | 13 | import org.gradle.api.artifacts.ConfigurationContainer;
|
14 | 14 | import org.gradle.api.component.AdhocComponentWithVariants;
|
15 | 15 | import org.gradle.api.component.SoftwareComponentFactory;
|
16 | 16 | import org.gradle.api.internal.artifacts.ArtifactAttributes;
|
17 | 17 | import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact;
|
| 18 | +import org.gradle.api.internal.file.FileResolver; |
| 19 | +import org.gradle.api.internal.project.ProjectInternal; |
| 20 | +import org.gradle.api.internal.tasks.TaskDependencyFactory; |
18 | 21 | import org.gradle.api.provider.Provider;
|
19 | 22 |
|
| 23 | +import dev.galasa.gradle.common.GradleCompatibilityService; |
| 24 | +import dev.galasa.gradle.common.ICompatibilityService; |
| 25 | + |
20 | 26 | /**
|
21 | 27 | * Generate an OBR
|
22 | 28 | */
|
23 | 29 | public class ObrPlugin implements Plugin<Project> {
|
24 | 30 |
|
25 | 31 | private final SoftwareComponentFactory softwareComponentFactory;
|
| 32 | + private final TaskDependencyFactory taskDependencyFactory; |
| 33 | + private final ICompatibilityService compatibilityService = new GradleCompatibilityService(); |
26 | 34 |
|
27 | 35 | @Inject
|
28 |
| - public ObrPlugin(SoftwareComponentFactory softwareComponentFactory) { |
| 36 | + public ObrPlugin(SoftwareComponentFactory softwareComponentFactory, TaskDependencyFactory taskDependencyFactory) { |
29 | 37 | this.softwareComponentFactory = softwareComponentFactory;
|
| 38 | + this.taskDependencyFactory = taskDependencyFactory; |
30 | 39 | }
|
31 | 40 |
|
32 | 41 | public void apply(Project project) {
|
@@ -58,11 +67,24 @@ private void createObrBuildTask(Project project) {
|
58 | 67 | Provider<ObrBuildTask> provider = project.getTasks().register("genobr", ObrBuildTask.class, obrTask -> {
|
59 | 68 | obrTask.apply();
|
60 | 69 | });
|
61 |
| - |
| 70 | + |
62 | 71 | // Create the Publish Artifact that the task will be creating and add it the
|
63 | 72 | // configuration outbound list
|
64 |
| - LazyPublishArtifact artifact = new LazyPublishArtifact(provider); |
65 |
| - project.getConfigurations().getByName("galasagenobr").getOutgoing().artifact(artifact); |
| 73 | + try { |
| 74 | + LazyPublishArtifact artifact; |
| 75 | + if (compatibilityService.isCurrentVersionLaterThanGradle8()) { |
| 76 | + // Create the artifact using the Gradle 8.x constructor |
| 77 | + artifact = LazyPublishArtifact.class |
| 78 | + .getConstructor(Provider.class, FileResolver.class, TaskDependencyFactory.class) |
| 79 | + .newInstance(provider, ((ProjectInternal) project).getFileResolver(), taskDependencyFactory); |
| 80 | + } else { |
| 81 | + // Create the artifact using the Gradle 6.x/7.x constructor |
| 82 | + artifact = LazyPublishArtifact.class.getConstructor(Provider.class).newInstance(provider); |
| 83 | + } |
| 84 | + project.getConfigurations().getByName("galasagenobr").getOutgoing().artifact(artifact); |
| 85 | + } catch (ReflectiveOperationException err) { |
| 86 | + throw new IllegalArgumentException("Incompatible LazyPublishArtifact constructor for Gradle version " + project.getGradle().getGradleVersion()); |
| 87 | + } |
66 | 88 | }
|
67 | 89 |
|
68 | 90 | private void createSoftwareComponents(Project project) {
|
|
0 commit comments