View Javadoc
1   package org.apache.maven.plugins.site;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.repository.ArtifactRepository;
23  import org.apache.maven.artifact.versioning.ComparableVersion;
24  import org.apache.maven.doxia.tools.SiteTool;
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugins.annotations.Component;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.i18n.I18N;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.util.List;
34  import java.util.Locale;
35  import java.util.Properties;
36  
37  /**
38   * Base class for site mojos.
39   *
40   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
41   */
42  public abstract class AbstractSiteMojo
43      extends AbstractMojo
44  {
45      /**
46       * A comma separated list of locales to render. The first valid token will be the default Locale
47       * for this site.
48       *
49       * @since 2.3
50       */
51      @Parameter( property = "locales", defaultValue = "en" )
52      private String locales;
53  
54      /**
55       * Set this to 'true' to skip site generation and staging.
56       *
57       * @since 3.0
58       */
59      @Parameter( property = "maven.site.skip", defaultValue = "false" )
60      protected boolean skip;
61  
62      /**
63       * SiteTool.
64       */
65      @Component
66      protected SiteTool siteTool;
67  
68      /**
69       * Internationalization.
70       */
71      @Component
72      protected I18N i18n;
73  
74      /**
75       * The Maven project.
76       */
77      @Parameter( defaultValue = "${project}", readonly = true )
78      protected MavenProject project;
79  
80      /**
81       * The local repository.
82       */
83      @Parameter( defaultValue = "${localRepository}", readonly = true )
84      protected ArtifactRepository localRepository;
85  
86      /**
87       * The reactor projects.
88       */
89      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
90      protected List<MavenProject> reactorProjects;
91  
92      /**
93       * Check the current Maven version to see if it's Maven 3.0 or newer.
94       */
95      protected static boolean isMaven3OrMore()
96      {
97          return new ComparableVersion( getMavenVersion() ).compareTo( new ComparableVersion( "3.0" ) ) >= 0;
98      }
99  
100     protected static String getMavenVersion()
101     {
102         // This relies on the fact that MavenProject is the in core classloader
103         // and that the core classloader is for the maven-core artifact
104         // and that should have a pom.properties file
105         // if this ever changes, we will have to revisit this code.
106         final Properties properties = new Properties();
107         final String corePomProperties = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
108 
109         try ( InputStream in = MavenProject.class.getClassLoader().getResourceAsStream( corePomProperties ) )
110         {
111             properties.load( in );
112         }
113         catch ( IOException ioe )
114         {
115             return "";
116         }
117 
118         return properties.getProperty( "version" ).trim();
119     }
120 
121     protected List<Locale> getLocales()
122     {
123         return siteTool.getSiteLocales( locales );
124     }
125 }