11package io .spring .gradle .javadoc ;
22
3- import org .gradle .api .Action ;
43import org .gradle .api .Plugin ;
54import org .gradle .api .Project ;
65import org .gradle .api .artifacts .Configuration ;
76import org .gradle .api .artifacts .ConfigurationContainer ;
8- import org .gradle .api .artifacts .ConfigurationPublications ;
97import org .gradle .api .artifacts .Dependency ;
108import 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 ;
179import org .gradle .api .plugins .JavaPlugin ;
18- import org .gradle .api .plugins .JavaPluginConvention ;
19- import org .gradle .api .tasks .SourceSet ;
2010import org .gradle .api .tasks .javadoc .Javadoc ;
2111
22- import java .io .File ;
23- import java .util .function .Consumer ;
24-
2512/**
13+ * Plugin used to generate aggregated Javadoc.
14+ *
2615 * @author Rob Winch
2716 */
2817public class AggregateJavadocPlugin implements Plugin <Project > {
18+
2919 public static final String AGGREGATE_JAVADOC_TASK_NAME = "aggregateJavadoc" ;
3020
3121 public static final String AGGREGATE_JAVADOC_CLASSPATH_CONFIGURATION_NAME = "aggregateJavadocClasspath" ;
@@ -35,85 +25,56 @@ public void apply(Project project) {
3525 project .getPlugins ().apply (JavaPlugin .class );
3626 Configuration aggregatedConfiguration = aggregatedConfiguration (project );
3727 Configuration sourcesPath = sourcesPath (project , aggregatedConfiguration );
38- aggregatedJavadoc (project ,sourcesPath , aggregatedConfiguration );
28+ aggregatedJavadoc (project , sourcesPath , aggregatedConfiguration );
3929 }
4030
4131 private Configuration aggregatedConfiguration (Project project ) {
4232 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 );
4534 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 );
6436 return aggregatedConfiguration ;
6537 }
6638
6739 private Configuration sourcesPath (Project project , Configuration aggregatedConfiguration ) {
6840 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 ));
10447 });
10548 }
10649
10750 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 );
11756 });
11857 }
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+
11980}
0 commit comments