|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.build.multirelease; |
| 18 | + |
| 19 | +import javax.inject.Inject; |
| 20 | + |
| 21 | +import org.gradle.api.artifacts.Configuration; |
| 22 | +import org.gradle.api.artifacts.ConfigurationContainer; |
| 23 | +import org.gradle.api.artifacts.dsl.DependencyHandler; |
| 24 | +import org.gradle.api.attributes.LibraryElements; |
| 25 | +import org.gradle.api.file.ConfigurableFileCollection; |
| 26 | +import org.gradle.api.file.FileCollection; |
| 27 | +import org.gradle.api.java.archives.Attributes; |
| 28 | +import org.gradle.api.model.ObjectFactory; |
| 29 | +import org.gradle.api.tasks.SourceSet; |
| 30 | +import org.gradle.api.tasks.SourceSetContainer; |
| 31 | +import org.gradle.api.tasks.TaskContainer; |
| 32 | +import org.gradle.api.tasks.TaskProvider; |
| 33 | +import org.gradle.api.tasks.bundling.Jar; |
| 34 | +import org.gradle.api.tasks.compile.JavaCompile; |
| 35 | +import org.gradle.api.tasks.testing.Test; |
| 36 | +import org.gradle.language.base.plugins.LifecycleBasePlugin; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Cedric Champeau |
| 40 | + * @author Brian Clozel |
| 41 | + */ |
| 42 | +public abstract class MultiReleaseExtension { |
| 43 | + private final TaskContainer tasks; |
| 44 | + private final SourceSetContainer sourceSets; |
| 45 | + private final DependencyHandler dependencies; |
| 46 | + private final ObjectFactory objects; |
| 47 | + private final ConfigurationContainer configurations; |
| 48 | + |
| 49 | + @Inject |
| 50 | + public MultiReleaseExtension(SourceSetContainer sourceSets, |
| 51 | + ConfigurationContainer configurations, |
| 52 | + TaskContainer tasks, |
| 53 | + DependencyHandler dependencies, |
| 54 | + ObjectFactory objectFactory) { |
| 55 | + this.sourceSets = sourceSets; |
| 56 | + this.configurations = configurations; |
| 57 | + this.tasks = tasks; |
| 58 | + this.dependencies = dependencies; |
| 59 | + this.objects = objectFactory; |
| 60 | + } |
| 61 | + |
| 62 | + public void releaseVersions(int... javaVersions) { |
| 63 | + releaseVersions("src/main/", "src/test/", javaVersions); |
| 64 | + } |
| 65 | + |
| 66 | + private void releaseVersions(String mainSourceDirectory, String testSourceDirectory, int... javaVersions) { |
| 67 | + for (int javaVersion : javaVersions) { |
| 68 | + addLanguageVersion(javaVersion, mainSourceDirectory, testSourceDirectory); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void addLanguageVersion(int javaVersion, String mainSourceDirectory, String testSourceDirectory) { |
| 73 | + String javaN = "java" + javaVersion; |
| 74 | + |
| 75 | + SourceSet langSourceSet = sourceSets.create(javaN, srcSet -> srcSet.getJava().srcDir(mainSourceDirectory + javaN)); |
| 76 | + SourceSet testSourceSet = sourceSets.create(javaN + "Test", srcSet -> srcSet.getJava().srcDir(testSourceDirectory + javaN)); |
| 77 | + SourceSet sharedSourceSet = sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME); |
| 78 | + SourceSet sharedTestSourceSet = sourceSets.findByName(SourceSet.TEST_SOURCE_SET_NAME); |
| 79 | + |
| 80 | + FileCollection mainClasses = objects.fileCollection().from(sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput().getClassesDirs()); |
| 81 | + dependencies.add(javaN + "Implementation", mainClasses); |
| 82 | + |
| 83 | + tasks.named(langSourceSet.getCompileJavaTaskName(), JavaCompile.class, task -> |
| 84 | + task.getOptions().getRelease().set(javaVersion) |
| 85 | + ); |
| 86 | + tasks.named(testSourceSet.getCompileJavaTaskName(), JavaCompile.class, task -> |
| 87 | + task.getOptions().getRelease().set(javaVersion) |
| 88 | + ); |
| 89 | + |
| 90 | + TaskProvider<Test> testTask = createTestTask(javaVersion, testSourceSet, sharedTestSourceSet, langSourceSet, sharedSourceSet); |
| 91 | + tasks.named("check", task -> task.dependsOn(testTask)); |
| 92 | + |
| 93 | + configureMultiReleaseJar(javaVersion, langSourceSet); |
| 94 | + } |
| 95 | + |
| 96 | + private TaskProvider<Test> createTestTask(int javaVersion, SourceSet testSourceSet, SourceSet sharedTestSourceSet, SourceSet langSourceSet, SourceSet sharedSourceSet) { |
| 97 | + Configuration testImplementation = configurations.getByName(testSourceSet.getImplementationConfigurationName()); |
| 98 | + testImplementation.extendsFrom(configurations.getByName(sharedTestSourceSet.getImplementationConfigurationName())); |
| 99 | + Configuration testCompileOnly = configurations.getByName(testSourceSet.getCompileOnlyConfigurationName()); |
| 100 | + testCompileOnly.extendsFrom(configurations.getByName(sharedTestSourceSet.getCompileOnlyConfigurationName())); |
| 101 | + testCompileOnly.getDependencies().add(dependencies.create(langSourceSet.getOutput().getClassesDirs())); |
| 102 | + testCompileOnly.getDependencies().add(dependencies.create(sharedSourceSet.getOutput().getClassesDirs())); |
| 103 | + |
| 104 | + Configuration testRuntimeClasspath = configurations.getByName(testSourceSet.getRuntimeClasspathConfigurationName()); |
| 105 | + // so here's the deal. MRjars are JARs! Which means that to execute tests, we need |
| 106 | + // the JAR on classpath, not just classes + resources as Gradle usually does |
| 107 | + testRuntimeClasspath.getAttributes() |
| 108 | + .attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.class, LibraryElements.JAR)); |
| 109 | + |
| 110 | + TaskProvider<Test> testTask = tasks.register("java" + javaVersion + "Test", Test.class, test -> { |
| 111 | + test.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP); |
| 112 | + |
| 113 | + ConfigurableFileCollection testClassesDirs = objects.fileCollection(); |
| 114 | + testClassesDirs.from(testSourceSet.getOutput()); |
| 115 | + testClassesDirs.from(sharedTestSourceSet.getOutput()); |
| 116 | + test.setTestClassesDirs(testClassesDirs); |
| 117 | + ConfigurableFileCollection classpath = objects.fileCollection(); |
| 118 | + // must put the MRJar first on classpath |
| 119 | + classpath.from(tasks.named("jar")); |
| 120 | + // then we put the specific test sourceset tests, so that we can override |
| 121 | + // the shared versions |
| 122 | + classpath.from(testSourceSet.getOutput()); |
| 123 | + |
| 124 | + // then we add the shared tests |
| 125 | + classpath.from(sharedTestSourceSet.getRuntimeClasspath()); |
| 126 | + test.setClasspath(classpath); |
| 127 | + }); |
| 128 | + return testTask; |
| 129 | + } |
| 130 | + |
| 131 | + private void configureMultiReleaseJar(int version, SourceSet languageSourceSet) { |
| 132 | + tasks.named("jar", Jar.class, jar -> { |
| 133 | + jar.into("META-INF/versions/" + version, s -> s.from(languageSourceSet.getOutput())); |
| 134 | + Attributes attributes = jar.getManifest().getAttributes(); |
| 135 | + attributes.put("Multi-Release", "true"); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments