|
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,97 @@ 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 | + activate(runtimeClasspath); |
| 237 | + activate(compileClasspath); |
| 238 | + activate(annotationProcessor); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Activate the plugin's functionality for a single resolvable Configuration. |
| 243 | + * This is useful to use the plugins for scopes that are not tied to a Source Set, |
| 244 | + * for which the plugin does not activate automatically. |
| 245 | + * |
| 246 | + * @param resolvable a resolvable Configuration (e.g. configurations["customClasspath"]) |
| 247 | + */ |
| 248 | + public void activate(Configuration resolvable) { |
| 249 | + resolvable.getAttributes().attribute(JAVA_MODULE_ATTRIBUTE, true); |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Variant of {@link #activate(SourceSet)} and {@link #activate(Configuration)} that accepts either a |
| 254 | + * Provider of {@link SourceSet} or a Provider of {@link Configuration}. This is a convenience to use |
| 255 | + * notations like 'activate(sourceSets.main)' in Kotlin DSL. |
| 256 | + * |
| 257 | + * @param sourceSetOrResolvable the Source Set or Configuration to activate |
| 258 | + */ |
| 259 | + public void activate(NamedDomainObjectProvider<?> sourceSetOrResolvable) { |
| 260 | + Object realized = sourceSetOrResolvable.get(); |
| 261 | + if (realized instanceof SourceSet) { |
| 262 | + activate((SourceSet) realized); |
| 263 | + } else if (realized instanceof Configuration) { |
| 264 | + activate((Configuration) realized); |
| 265 | + } else { |
| 266 | + throw new RuntimeException("Not SourceSet or Configuration: " + realized); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Deactivate the plugin's functionality for dependencies of all scopes of the given source set |
| 272 | + * (runtimeClasspath, compileClasspath, annotationProcessor). |
| 273 | + * |
| 274 | + * @param sourceSet the Source Set to deactivate (e.g. sourceSets.test) |
| 275 | + */ |
| 276 | + public void deactivate(SourceSet sourceSet) { |
| 277 | + Configuration runtimeClasspath = getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName()); |
| 278 | + Configuration compileClasspath = getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()); |
| 279 | + Configuration annotationProcessor = getConfigurations().getByName(sourceSet.getAnnotationProcessorConfigurationName()); |
| 280 | + |
| 281 | + deactivate(runtimeClasspath); |
| 282 | + deactivate(compileClasspath); |
| 283 | + deactivate(annotationProcessor); |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * Deactivate the plugin's functionality for a single resolvable Configuration. |
| 288 | + * This is useful if selected scopes do not use the Module Path and therefore |
| 289 | + * module information is not required. |
| 290 | + * |
| 291 | + * @param resolvable a resolvable Configuration (e.g. configurations.annotationProcessor) |
| 292 | + */ |
| 293 | + public void deactivate(Configuration resolvable) { |
| 294 | + resolvable.getAttributes().attribute(JAVA_MODULE_ATTRIBUTE, false); |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * Variant of {@link #deactivate(SourceSet)} and {@link #deactivate(Configuration)} that accepts either a |
| 299 | + * Provider of {@link SourceSet} or a Provider of {@link Configuration}. This is a convenience to use |
| 300 | + * notations like 'deactivate(sourceSets.test)' in Kotlin DSL. |
| 301 | + * |
| 302 | + * @param sourceSetOrResolvable the Source Set or Configuration to activate |
| 303 | + */ |
| 304 | + public void deactivate(NamedDomainObjectProvider<?> sourceSetOrResolvable) { |
| 305 | + Object realized = sourceSetOrResolvable.get(); |
| 306 | + if (realized instanceof SourceSet) { |
| 307 | + deactivate((SourceSet) realized); |
| 308 | + } else if (realized instanceof Configuration) { |
| 309 | + deactivate((Configuration) realized); |
| 310 | + } else { |
| 311 | + throw new RuntimeException("Not SourceSet or Configuration: " + realized); |
| 312 | + } |
| 313 | + } |
212 | 314 | } |
0 commit comments