|
17 | 17 | package org.gradlex.javamodule.moduleinfo;
|
18 | 18 |
|
19 | 19 | import org.gradle.api.Action;
|
| 20 | +import org.gradle.api.NamedDomainObjectProvider; |
| 21 | +import org.gradle.api.artifacts.Configuration; |
| 22 | +import org.gradle.api.artifacts.ConfigurationContainer; |
20 | 23 | import org.gradle.api.artifacts.MinimalExternalModuleDependency;
|
| 24 | +import org.gradle.api.attributes.Attribute; |
21 | 25 | import org.gradle.api.model.ObjectFactory;
|
22 | 26 | import org.gradle.api.provider.MapProperty;
|
23 | 27 | import org.gradle.api.provider.Property;
|
24 | 28 | import org.gradle.api.provider.Provider;
|
| 29 | +import org.gradle.api.tasks.SourceSet; |
25 | 30 |
|
26 | 31 | import javax.annotation.Nullable;
|
27 | 32 | import javax.inject.Inject;
|
|
33 | 38 | */
|
34 | 39 | @SuppressWarnings("unused")
|
35 | 40 | public abstract class ExtraJavaModuleInfoPluginExtension {
|
| 41 | + static Attribute<Boolean> JAVA_MODULE_ATTRIBUTE = Attribute.of("javaModule", Boolean.class); |
36 | 42 |
|
37 | 43 | @Inject
|
38 | 44 | protected abstract ObjectFactory getObjects();
|
39 | 45 |
|
| 46 | + @Inject |
| 47 | + protected abstract ConfigurationContainer getConfigurations(); |
| 48 | + |
40 | 49 | public abstract MapProperty<String, ModuleSpec> getModuleSpecs();
|
41 | 50 | public abstract Property<Boolean> getFailOnMissingModuleInfo();
|
42 | 51 | public abstract Property<Boolean> getFailOnAutomaticModules();
|
@@ -209,4 +218,99 @@ public void knownModule(String coordinates, String moduleName) {
|
209 | 218 | public void knownModule(Provider<MinimalExternalModuleDependency> alias, String moduleName) {
|
210 | 219 | knownModule(alias.get().getModule().toString(), moduleName);
|
211 | 220 | }
|
| 221 | + |
| 222 | + /** |
| 223 | + * Activate the plugin's functionality for dependencies of all scopes of the given source set |
| 224 | + * (runtimeClasspath, compileClasspath, annotationProcessor). |
| 225 | + * Note that the plugin activates the functionality for all source sets by default. |
| 226 | + * Therefore, this method only has an effect for source sets for which a {@link #deactivate(SourceSet)} |
| 227 | + * has been performed. |
| 228 | + * |
| 229 | + * @param sourceSet the Source Set to activate (e.g. sourceSets.test) |
| 230 | + */ |
| 231 | + public void activate(SourceSet sourceSet) { |
| 232 | + Configuration runtimeClasspath = getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName()); |
| 233 | + Configuration compileClasspath = getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()); |
| 234 | + Configuration annotationProcessor = getConfigurations().getByName(sourceSet.getAnnotationProcessorConfigurationName()); |
| 235 | + |
| 236 | + // compile, runtime and annotation processor classpaths express that they only accept modules by requesting the javaModule=true attribute |
| 237 | + activate(runtimeClasspath); |
| 238 | + activate(compileClasspath); |
| 239 | + activate(annotationProcessor); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Activate the plugin's functionality for a single resolvable Configuration. |
| 244 | + * This is useful to use the plugins for scopes that are not tied to a Source Set, |
| 245 | + * for which the plugin does not activate automatically. |
| 246 | + * |
| 247 | + * @param resolvable a resolvable Configuration (e.g. configurations["customClasspath"]) |
| 248 | + */ |
| 249 | + public void activate(Configuration resolvable) { |
| 250 | + resolvable.getAttributes().attribute(JAVA_MODULE_ATTRIBUTE, true); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Variant of {@link #activate(SourceSet)} and {@link #activate(Configuration)} that accepts either a |
| 255 | + * Provider of {@link SourceSet} or a Provider of {@link Configuration}. This is a convenience to use |
| 256 | + * notations like 'activate(sourceSets.main)' in Kotlin DSL. |
| 257 | + * |
| 258 | + * @param resolvableOrSourceSet the Source Set or Configuration to activate |
| 259 | + */ |
| 260 | + public void activate(NamedDomainObjectProvider<?> resolvableOrSourceSet) { |
| 261 | + Object realized = resolvableOrSourceSet.get(); |
| 262 | + if (realized instanceof SourceSet) { |
| 263 | + activate((SourceSet) realized); |
| 264 | + } else if (realized instanceof Configuration) { |
| 265 | + activate((Configuration) realized); |
| 266 | + } else { |
| 267 | + throw new RuntimeException("Not SourceSet or Configuration: " + realized); |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Deactivate the plugin's functionality for dependencies of all scopes of the given source set |
| 273 | + * (runtimeClasspath, compileClasspath, annotationProcessor). |
| 274 | + * |
| 275 | + * @param sourceSet the Source Set to deactivate (e.g. sourceSets.test) |
| 276 | + */ |
| 277 | + public void deactivate(SourceSet sourceSet) { |
| 278 | + Configuration runtimeClasspath = getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName()); |
| 279 | + Configuration compileClasspath = getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()); |
| 280 | + Configuration annotationProcessor = getConfigurations().getByName(sourceSet.getAnnotationProcessorConfigurationName()); |
| 281 | + |
| 282 | + // compile, runtime and annotation processor classpaths express that they only accept modules by requesting the javaModule=true attribute |
| 283 | + deactivate(runtimeClasspath); |
| 284 | + deactivate(compileClasspath); |
| 285 | + deactivate(annotationProcessor); |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * Deactivate the plugin's functionality for a single resolvable Configuration. |
| 290 | + * This is useful if selected scopes do not use the Module Path and therefore |
| 291 | + * module information is not required. |
| 292 | + * |
| 293 | + * @param resolvable a resolvable Configuration (e.g. configurations.annotationProcessor) |
| 294 | + */ |
| 295 | + public void deactivate(Configuration resolvable) { |
| 296 | + resolvable.getAttributes().attribute(JAVA_MODULE_ATTRIBUTE, false); |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * Variant of {@link #deactivate(SourceSet)} and {@link #deactivate(Configuration)} that accepts either a |
| 301 | + * Provider of {@link SourceSet} or a Provider of {@link Configuration}. This is a convenience to use |
| 302 | + * notations like 'deactivate(sourceSets.test)' in Kotlin DSL. |
| 303 | + * |
| 304 | + * @param resolvableOrSourceSet the Source Set or Configuration to activate |
| 305 | + */ |
| 306 | + public void deactivate(NamedDomainObjectProvider<?> resolvableOrSourceSet) { |
| 307 | + Object realized = resolvableOrSourceSet.get(); |
| 308 | + if (realized instanceof SourceSet) { |
| 309 | + deactivate((SourceSet) realized); |
| 310 | + } else if (realized instanceof Configuration) { |
| 311 | + deactivate((Configuration) realized); |
| 312 | + } else { |
| 313 | + throw new RuntimeException("Not SourceSet or Configuration: " + realized); |
| 314 | + } |
| 315 | + } |
212 | 316 | }
|
0 commit comments