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.plugins.site.descriptor;
20  
21  import java.io.File;
22  import java.util.List;
23  import java.util.Locale;
24  
25  import org.apache.maven.doxia.site.SiteModel;
26  import org.apache.maven.doxia.site.inheritance.SiteModelInheritanceAssembler;
27  import org.apache.maven.doxia.tools.SiteTool;
28  import org.apache.maven.doxia.tools.SiteToolException;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugins.annotations.Component;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.plugins.site.AbstractSiteMojo;
33  import org.apache.maven.project.MavenProject;
34  import org.eclipse.aether.RepositorySystemSession;
35  import org.eclipse.aether.repository.RemoteRepository;
36  
37  /**
38   * Abstract class to compute effective site model for site descriptors.
39   *
40   * @since 3.5
41   */
42  public abstract class AbstractSiteDescriptorMojo extends AbstractSiteMojo {
43      /**
44       * The component for assembling site model inheritance.
45       */
46      @Component
47      private SiteModelInheritanceAssembler assembler;
48  
49      /**
50       * The reactor projects.
51       */
52      @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
53      protected List<MavenProject> reactorProjects;
54  
55      @Parameter(defaultValue = "${repositorySystemSession}", required = true, readonly = true)
56      protected RepositorySystemSession repoSession;
57  
58      /**
59       * Remote project repositories used for the project.
60       *
61       * todo this is used for site descriptor resolution - it should relate to the actual project but for some reason
62       *       they are not always filled in
63       */
64      @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true)
65      protected List<RemoteRepository> remoteProjectRepositories;
66  
67      /**
68       * Directory containing the <code>site.xml</code> file and the source for hand written docs (one directory
69       * per Doxia-source-supported markup types):
70       * see <a href="/doxia/references/index.html">Doxia Markup Languages References</a>).
71       *
72       * @since 2.3
73       */
74      @Parameter(defaultValue = "${basedir}/src/site")
75      protected File siteDirectory;
76  
77      /**
78       * Make links in the site descriptor relative to the project URL.
79       * By default, any absolute links that appear in the site descriptor,
80       * e.g. banner hrefs, breadcrumbs, menu links, etc., will be made relative to project.url.
81       * <p/>
82       * Links will not be changed if this is set to false, or if the project has no URL defined.
83       *
84       * @since 2.3
85       */
86      @Parameter(property = "relativizeSiteLinks", defaultValue = "true")
87      private boolean relativizeSiteLinks;
88  
89      protected SiteModel prepareSiteModel(Locale locale) throws MojoExecutionException {
90          SiteModel siteModel;
91          try {
92              siteModel = siteTool.getSiteModel(
93                      siteDirectory, locale, project, reactorProjects, repoSession, remoteProjectRepositories);
94          } catch (SiteToolException e) {
95              throw new MojoExecutionException("Failed to obtain site model", e);
96          }
97  
98          if (relativizeSiteLinks) {
99              final String url = project.getUrl();
100 
101             if (url == null) {
102                 getLog().warn("No project URL defined - site links will not be relativized!");
103             } else {
104                 // MSITE-658
105                 final String localeUrl = !locale.equals(SiteTool.DEFAULT_LOCALE) ? append(url, locale.toString()) : url;
106 
107                 getLog().info("Relativizing site links with respect to localized project URL: " + localeUrl);
108                 assembler.resolvePaths(siteModel, localeUrl);
109             }
110         }
111         return siteModel;
112     }
113 
114     private String append(String url, String path) {
115         return url.endsWith("/") ? (url + path) : (url + '/' + path);
116     }
117 }