1
1
package io .spring .gradle .javadoc ;
2
2
3
- import org .gradle .api .Action ;
4
3
import org .gradle .api .Plugin ;
5
4
import org .gradle .api .Project ;
6
5
import org .gradle .api .artifacts .Configuration ;
7
6
import org .gradle .api .artifacts .ConfigurationContainer ;
8
- import org .gradle .api .artifacts .ConfigurationPublications ;
9
7
import org .gradle .api .artifacts .Dependency ;
10
8
import org .gradle .api .artifacts .DependencySet ;
11
- import org .gradle .api .attributes .Attribute ;
12
- import org .gradle .api .attributes .AttributeContainer ;
13
- import org .gradle .api .attributes .Category ;
14
- import org .gradle .api .attributes .DocsType ;
15
- import org .gradle .api .attributes .Usage ;
16
- import org .gradle .api .model .ObjectFactory ;
17
9
import org .gradle .api .plugins .JavaPlugin ;
18
- import org .gradle .api .plugins .JavaPluginConvention ;
19
- import org .gradle .api .tasks .SourceSet ;
20
10
import org .gradle .api .tasks .javadoc .Javadoc ;
21
11
22
- import java .io .File ;
23
- import java .util .function .Consumer ;
24
-
25
12
/**
13
+ * Plugin used to generate aggregated Javadoc.
14
+ *
26
15
* @author Rob Winch
27
16
*/
28
17
public class AggregateJavadocPlugin implements Plugin <Project > {
18
+
29
19
public static final String AGGREGATE_JAVADOC_TASK_NAME = "aggregateJavadoc" ;
30
20
31
21
public static final String AGGREGATE_JAVADOC_CLASSPATH_CONFIGURATION_NAME = "aggregateJavadocClasspath" ;
@@ -35,85 +25,56 @@ public void apply(Project project) {
35
25
project .getPlugins ().apply (JavaPlugin .class );
36
26
Configuration aggregatedConfiguration = aggregatedConfiguration (project );
37
27
Configuration sourcesPath = sourcesPath (project , aggregatedConfiguration );
38
- aggregatedJavadoc (project ,sourcesPath , aggregatedConfiguration );
28
+ aggregatedJavadoc (project , sourcesPath , aggregatedConfiguration );
39
29
}
40
30
41
31
private Configuration aggregatedConfiguration (Project project ) {
42
32
ConfigurationContainer configurations = project .getConfigurations ();
43
- Configuration aggregatedConfiguration = configurations
44
- .maybeCreate (AGGREGATE_JAVADOC_CLASSPATH_CONFIGURATION_NAME );
33
+ Configuration aggregatedConfiguration = configurations .maybeCreate (AGGREGATE_JAVADOC_CLASSPATH_CONFIGURATION_NAME );
45
34
configurations .getByName (JavaPlugin .IMPLEMENTATION_CONFIGURATION_NAME ).extendsFrom (aggregatedConfiguration );
46
- aggregatedConfiguration .defaultDependencies (new Action <DependencySet >() {
47
- @ Override
48
- public void execute (DependencySet defaultDependencies ) {
49
- project .getGradle ().getRootProject ().subprojects (new Action <Project >() {
50
- @ Override
51
- public void execute (Project subproject ) {
52
- subproject .getPlugins ().withType (JavadocPlugin .class , new Action <JavadocPlugin >() {
53
- @ Override
54
- public void execute (JavadocPlugin javadoc ) {
55
- Dependency dependency = project .getDependencies ()
56
- .create (subproject );
57
- defaultDependencies .add (dependency );
58
- }
59
- });
60
- }
61
- });
62
- }
63
- });
35
+ aggregatedConfiguration .defaultDependencies (new AggregatedDependencies (project )::apply );
64
36
return aggregatedConfiguration ;
65
37
}
66
38
67
39
private Configuration sourcesPath (Project project , Configuration aggregatedConfiguration ) {
68
40
ConfigurationContainer configurations = project .getConfigurations ();
69
- return configurations .create ("sourcesPath" , new Action <Configuration >() {
70
- @ Override
71
- public void execute (Configuration sourcesPath ) {
72
- sourcesPath .setCanBeResolved (true );
73
- sourcesPath .setCanBeConsumed (false );
74
- sourcesPath .extendsFrom (aggregatedConfiguration );
75
- sourcesPath .attributes (new Action <AttributeContainer >() {
76
- @ Override
77
- public void execute (AttributeContainer attributes ) {
78
- ObjectFactory objects = project .getObjects ();
79
- attributes .attribute (
80
- Usage .USAGE_ATTRIBUTE , objects .named (Usage .class , Usage .JAVA_RUNTIME ));
81
- attributes .attribute (Category .CATEGORY_ATTRIBUTE , objects .named (Category .class , Category .DOCUMENTATION ));
82
- attributes .attribute (DocsType .DOCS_TYPE_ATTRIBUTE , objects .named (DocsType .class , DocsType .SOURCES ));
83
- attributes .attribute (
84
- Attribute .of ("org.gradle.docselements" , String .class ), "sources" );
85
- }
86
- });
87
- sourcesPath .outgoing (new Action <ConfigurationPublications >() {
88
- @ Override
89
- public void execute (
90
- ConfigurationPublications publications ) {
91
- JavaPluginConvention javaPlugin = project .getConvention ()
92
- .getPlugin (JavaPluginConvention .class );
93
- SourceSet mainSrc = javaPlugin .getSourceSets ()
94
- .getByName (SourceSet .MAIN_SOURCE_SET_NAME );
95
- mainSrc .getAllJava ().getSrcDirs ().forEach (new Consumer <File >() {
96
- @ Override
97
- public void accept (File file ) {
98
- publications .artifact (file );
99
- }
100
- });
101
- }
102
- });
103
- }
41
+ return configurations .create ("sourcesPath" , (sourcesPath ) -> {
42
+ sourcesPath .setCanBeResolved (true );
43
+ sourcesPath .setCanBeConsumed (false );
44
+ sourcesPath .extendsFrom (aggregatedConfiguration );
45
+ sourcesPath .attributes (attributes -> JavadocPlugin .addAttributes (project , attributes ));
46
+ sourcesPath .outgoing (publications -> JavadocPlugin .addOutgoing (project , publications ));
104
47
});
105
48
}
106
49
107
50
private void aggregatedJavadoc (Project project , Configuration sourcesPath , Configuration aggregatedConfiguration ) {
108
- project .getTasks ().create (AGGREGATE_JAVADOC_TASK_NAME , Javadoc .class , new Action <Javadoc >() {
109
- @ Override
110
- public void execute (Javadoc javadoc ) {
111
- javadoc .setGroup ("Documentation" );
112
- javadoc .setDescription ("Generates the aggregate Javadoc" );
113
- ConfigurationContainer configurations = project .getConfigurations ();
114
- javadoc .setSource (sourcesPath );
115
- javadoc .setClasspath (aggregatedConfiguration );
116
- }
51
+ project .getTasks ().create (AGGREGATE_JAVADOC_TASK_NAME , Javadoc .class , (javadoc ) -> {
52
+ javadoc .setGroup ("Documentation" );
53
+ javadoc .setDescription ("Generates the aggregate Javadoc" );
54
+ javadoc .setSource (sourcesPath );
55
+ javadoc .setClasspath (aggregatedConfiguration );
117
56
});
118
57
}
58
+
59
+ private static class AggregatedDependencies {
60
+
61
+ private final Project project ;
62
+
63
+ public AggregatedDependencies (Project project ) {
64
+ this .project = project ;
65
+ }
66
+
67
+ public void apply (DependencySet defaultDependencies ) {
68
+ project .getGradle ().getRootProject ().subprojects (subproject -> apply (defaultDependencies , subproject ));
69
+ }
70
+
71
+ private void apply (DependencySet defaultDependencies , Project subproject ) {
72
+ subproject .getPlugins ().withType (JavadocPlugin .class , (javadocPlugin ) -> {
73
+ Dependency subprojectDependency = this .project .getDependencies ().create (subproject );
74
+ defaultDependencies .add (subprojectDependency );
75
+ });
76
+ }
77
+
78
+ }
79
+
119
80
}
0 commit comments