1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import org .gradle .api .Plugin ;
20
20
import org .gradle .api .Project ;
21
21
import org .gradle .api .artifacts .Configuration ;
22
- import org .gradle .api .attributes .Usage ;
23
22
import org .gradle .api .plugins .JavaPlugin ;
24
23
import org .gradle .api .plugins .JavaPluginConvention ;
25
24
import org .gradle .api .tasks .SourceSetContainer ;
26
- import org .gradle .api .tasks .javadoc .Javadoc ;
27
- import org .gradle .plugins .ide .eclipse .EclipsePlugin ;
28
- import org .gradle .plugins .ide .eclipse .model .EclipseModel ;
29
25
30
26
/**
31
27
* A {@code Plugin} that adds support for Maven-style optional dependencies. Creates a new
32
28
* {@code optional} configuration. The {@code optional} configuration is part of the
33
- * project's compile and runtime classpath's but does not affect the classpath of
34
- * dependent projects.
29
+ * project's compile and runtime classpaths but does not affect the classpath of dependent
30
+ * projects.
35
31
*
36
32
* @author Andy Wilkinson
37
33
*/
@@ -44,22 +40,19 @@ public class OptionalDependenciesPlugin implements Plugin<Project> {
44
40
45
41
@ Override
46
42
public void apply (Project project ) {
47
- Configuration optional = project .getConfigurations ().create (OPTIONAL_CONFIGURATION_NAME );
48
- optional .attributes (( attributes ) -> attributes . attribute ( Usage . USAGE_ATTRIBUTE ,
49
- project . getObjects (). named ( Usage . class , Usage . JAVA_RUNTIME )) );
43
+ Configuration optional = project .getConfigurations ().create ("optional" );
44
+ optional .setCanBeConsumed ( false );
45
+ optional . setCanBeResolved ( false );
50
46
project .getPlugins ().withType (JavaPlugin .class , (javaPlugin ) -> {
51
47
SourceSetContainer sourceSets = project .getConvention ().getPlugin (JavaPluginConvention .class )
52
48
.getSourceSets ();
53
49
sourceSets .all ((sourceSet ) -> {
54
- sourceSet .setCompileClasspath (sourceSet .getCompileClasspath ().plus (optional ));
55
- sourceSet .setRuntimeClasspath (sourceSet .getRuntimeClasspath ().plus (optional ));
50
+ project .getConfigurations ().getByName (sourceSet .getCompileClasspathConfigurationName ())
51
+ .extendsFrom (optional );
52
+ project .getConfigurations ().getByName (sourceSet .getRuntimeClasspathConfigurationName ())
53
+ .extendsFrom (optional );
56
54
});
57
- project .getTasks ().withType (Javadoc .class )
58
- .all ((javadoc ) -> javadoc .setClasspath (javadoc .getClasspath ().plus (optional )));
59
55
});
60
- project .getPlugins ().withType (EclipsePlugin .class ,
61
- (eclipsePlugin ) -> project .getExtensions ().getByType (EclipseModel .class )
62
- .classpath ((classpath ) -> classpath .getPlusConfigurations ().add (optional )));
63
56
}
64
57
65
58
}
0 commit comments