View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.jxr;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.model.Plugin;
26  import org.apache.maven.model.PluginExecution;
27  import org.apache.maven.model.ReportPlugin;
28  import org.apache.maven.model.Site;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.wagon.repository.Repository;
31  import org.codehaus.plexus.util.StringUtils;
32  import org.codehaus.plexus.util.xml.Xpp3Dom;
33  
34  /**
35   * Utility class for the jxr report.
36   *
37   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
38   */
39  public class JxrReportUtil {
40  
41      private static final String MAVEN_JAVADOC_PLUGIN_GROUP_ID = "org.apache.maven.plugins";
42  
43      private static final String MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID = "maven-javadoc-plugin";
44  
45      /**
46       * Determine if javadoc is aggregated in this project, paying attention to both TODO: take cognizance of javadoc
47       * versus test-javadoc the old parameter and the new mojo.
48       *
49       * @param project the Maven project
50       * @return if javadoc is aggregated, false otherwise
51       */
52      protected static boolean isJavadocAggregated(MavenProject project) {
53          // first check conf for obsolete aggregate param.
54          boolean javadocAggregate =
55                  Boolean.parseBoolean(JxrReportUtil.getMavenJavadocPluginBasicOption(project, "aggregate", "false"));
56  
57          if (javadocAggregate) {
58              return true;
59          }
60          for (Object pluginObject : getMavenJavadocPlugins(project)) {
61              if (pluginObject instanceof Plugin) {
62                  Plugin plugin = (Plugin) pluginObject;
63                  List<PluginExecution> executions = plugin.getExecutions();
64                  for (PluginExecution pe : executions) {
65                      List<String> goals = pe.getGoals();
66                      for (String goal : goals) {
67                          if ("aggregate".equals(goal)) {
68                              return true;
69                          }
70                      }
71                  }
72              }
73          }
74          return false;
75      }
76  
77      /**
78       * Returns the {@code optionName} value defined in a project for the "maven-javadoc-plugin" plugin.
79       *
80       * @param project not null
81       * @param optionName the option name wanted
82       * @param defaultValue a default value
83       * @return the value for the option name or the default value. Could be null if not found.
84       */
85      protected static String getMavenJavadocPluginBasicOption(
86              MavenProject project, String optionName, String defaultValue) {
87          List<Object> plugins = new ArrayList<>();
88          plugins.addAll(project.getModel().getReporting().getPlugins());
89          plugins.addAll(project.getModel().getBuild().getPlugins());
90  
91          String pluginArtifactId = MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID;
92          for (Object next : plugins) {
93              Xpp3Dom pluginConf = null;
94  
95              if (next instanceof Plugin) {
96                  Plugin plugin = (Plugin) next;
97  
98                  // using out-of-box Maven plugins
99                  if (!isReportPluginMavenJavadoc(pluginArtifactId, plugin)) {
100                     continue;
101                 }
102 
103                 pluginConf = (Xpp3Dom) plugin.getConfiguration();
104             }
105 
106             if (next instanceof ReportPlugin) {
107                 ReportPlugin reportPlugin = (ReportPlugin) next;
108 
109                 // using out-of-box Maven plugins
110                 if (!isReportPluginJavaDocPlugin(pluginArtifactId, reportPlugin)) {
111                     continue;
112                 }
113 
114                 pluginConf = (Xpp3Dom) reportPlugin.getConfiguration();
115             }
116 
117             if (pluginConf == null) {
118                 continue;
119             }
120 
121             String attribute = pluginConf.getAttribute(optionName);
122             if (attribute != null && !attribute.isEmpty()) {
123                 return attribute;
124             }
125         }
126 
127         return defaultValue;
128     }
129 
130     /**
131      * Returns the plugin references for the javadoc plugin in a project.
132      *
133      * @param project Maven project
134      * @return list of Javadoc plugins
135      */
136     protected static List<?> getMavenJavadocPlugins(MavenProject project) {
137         List<Object> plugins = new ArrayList<>();
138         plugins.addAll(project.getModel().getReporting().getPlugins());
139         plugins.addAll(project.getModel().getBuild().getPlugins());
140 
141         List<Object> result = new ArrayList<>();
142 
143         String pluginArtifactId = MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID;
144         for (Object next : plugins) {
145             if (next instanceof Plugin) {
146                 Plugin plugin = (Plugin) next;
147 
148                 // using out-of-box Maven plugins
149                 if (!isReportPluginMavenJavadoc(pluginArtifactId, plugin)) {
150                     continue;
151                 }
152 
153                 result.add(plugin);
154             }
155 
156             if (next instanceof ReportPlugin) {
157                 ReportPlugin reportPlugin = (ReportPlugin) next;
158 
159                 // using out-of-box Maven plugins
160                 if (!isReportPluginJavaDocPlugin(pluginArtifactId, reportPlugin)) {
161                     continue;
162                 }
163                 result.add(reportPlugin);
164             }
165         }
166         return result;
167     }
168 
169     private static boolean isReportPluginMavenJavadoc(String pluginArtifactId, Plugin plugin) {
170         return (plugin.getGroupId().equals(MAVEN_JAVADOC_PLUGIN_GROUP_ID))
171                 && (plugin.getArtifactId().equals(pluginArtifactId));
172     }
173 
174     private static boolean isReportPluginJavaDocPlugin(String pluginArtifactId, ReportPlugin reportPlugin) {
175         return (reportPlugin.getGroupId().equals(MAVEN_JAVADOC_PLUGIN_GROUP_ID))
176                 && (reportPlugin.getArtifactId().equals(pluginArtifactId));
177     }
178 
179     /**
180      * Generates the site structure using the project hierarchy (project and its modules) or using the
181      * distributionManagement elements from the pom.xml.
182      *
183      * @param project the Maven project
184      * @param ignoreMissingSiteUrl whether missing site url in distribution management of the POM should be ignored
185      * @return the structure relative path
186      * @throws IOException if site url is missing
187      */
188     protected static String getStructure(MavenProject project, boolean ignoreMissingSiteUrl) throws IOException {
189         // @todo come from site plugin!
190         // @see o.a.m.p.site.SiteStageMojo#getStructure(MavenProject project, boolean ignoreMissingSiteUrl )
191         if (project.getDistributionManagement() == null) {
192             String hierarchy = project.getName();
193 
194             MavenProject parent = project.getParent();
195             while (parent != null) {
196                 hierarchy = parent.getName() + '/' + hierarchy;
197                 parent = parent.getParent();
198             }
199 
200             return hierarchy;
201         }
202 
203         Site site = project.getDistributionManagement().getSite();
204         if (site == null) {
205             if (!ignoreMissingSiteUrl) {
206                 throw new IOException("Missing site information in the distribution management "
207                         + "element in the project: '" + project.getName() + "'.");
208             }
209 
210             return null;
211         }
212 
213         if (StringUtils.isEmpty(site.getUrl())) {
214             if (!ignoreMissingSiteUrl) {
215                 throw new IOException("The URL in the site is missing in the project descriptor.");
216             }
217 
218             return null;
219         }
220 
221         Repository repository = new Repository(site.getId(), site.getUrl());
222         if (StringUtils.isEmpty(repository.getBasedir())) {
223             return repository.getHost();
224         }
225 
226         if (repository.getBasedir().startsWith("/")) {
227             return repository.getHost() + repository.getBasedir();
228         }
229 
230         return repository.getHost() + '/' + repository.getBasedir();
231     }
232 }