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