JavadocApiPlugin.groovy 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2004-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package io.spring.gradle.convention;
  17. import java.io.File;
  18. import java.util.Collections;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import java.util.regex.Pattern;
  22. import org.gradle.api.Action;
  23. import org.gradle.api.JavaVersion
  24. import org.gradle.api.Plugin;
  25. import org.gradle.api.Project;
  26. import org.gradle.api.plugins.JavaPluginConvention;
  27. import org.gradle.api.tasks.SourceSet;
  28. import org.gradle.api.tasks.javadoc.Javadoc;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. /**
  32. * @author Rob Winch
  33. */
  34. public class JavadocApiPlugin implements Plugin<Project> {
  35. Logger logger = LoggerFactory.getLogger(getClass());
  36. Set<Pattern> excludes = Collections.singleton(Pattern.compile("test"));
  37. @Override
  38. public void apply(Project project) {
  39. logger.info("Applied");
  40. Project rootProject = project.getRootProject();
  41. //Task docs = project.getTasks().findByPath("docs") ?: project.getTasks().create("docs");
  42. Javadoc api = project.getTasks().create("api", Javadoc);
  43. api.setGroup("Documentation");
  44. api.setDescription("Generates aggregated Javadoc API documentation.");
  45. api.doLast {
  46. if (JavaVersion.current().isJava11Compatible()) {
  47. project.copy({ copy -> copy
  48. .from(api.destinationDir)
  49. .into(api.destinationDir)
  50. .include("element-list")
  51. .rename("element-list", "package-list")
  52. });
  53. }
  54. }
  55. Set<Project> subprojects = rootProject.getSubprojects();
  56. for (Project subproject : subprojects) {
  57. addProject(api, subproject);
  58. }
  59. if (subprojects.isEmpty()) {
  60. addProject(api, project);
  61. }
  62. api.setMaxMemory("1024m");
  63. api.setDestinationDir(new File(project.getBuildDir(), "api"));
  64. project.getPluginManager().apply("io.spring.convention.javadoc-options");
  65. }
  66. public void setExcludes(String... excludes) {
  67. if(excludes == null) {
  68. this.excludes = Collections.emptySet();
  69. }
  70. this.excludes = new HashSet<Pattern>(excludes.length);
  71. for(String exclude : excludes) {
  72. this.excludes.add(Pattern.compile(exclude));
  73. }
  74. }
  75. private void addProject(final Javadoc api, final Project project) {
  76. for(Pattern exclude : excludes) {
  77. if(exclude.matcher(project.getName()).matches()) {
  78. logger.info("Skipping {} because it is excluded by {}", project, exclude);
  79. return;
  80. }
  81. }
  82. logger.info("Try add sources for {}", project);
  83. project.getPlugins().withType(SpringModulePlugin.class).all(new Action<SpringModulePlugin>() {
  84. @Override
  85. public void execute(SpringModulePlugin plugin) {
  86. logger.info("Added sources for {}", project);
  87. JavaPluginConvention java = project.getConvention().getPlugin(JavaPluginConvention.class);
  88. SourceSet mainSourceSet = java.getSourceSets().getByName("main");
  89. api.setSource(api.getSource().plus(mainSourceSet.getAllJava()));
  90. project.getTasks().withType(Javadoc.class).all(new Action<Javadoc>() {
  91. @Override
  92. public void execute(Javadoc projectJavadoc) {
  93. api.setClasspath(api.getClasspath().plus(projectJavadoc.getClasspath()));
  94. }
  95. });
  96. }
  97. });
  98. }
  99. }