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 java.io.IOException;
23  import java.io.InputStream;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Properties;
27  
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.versioning.ComparableVersion;
30  import org.apache.maven.doxia.tools.SiteTool;
31  import org.apache.maven.plugin.AbstractMojo;
32  import org.apache.maven.plugins.annotations.Component;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.project.MavenProject;
35  import org.codehaus.plexus.i18n.I18N;
36  import org.codehaus.plexus.util.IOUtil;
37  
38  /**
39   * Base class for site mojos.
40   *
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   */
43  public abstract class AbstractSiteMojo
44      extends AbstractMojo
45  {
46      /**
47       * A comma separated list of locales to render. The first valid token will be the default Locale
48       * for this site.
49       *
50       * @since 2.3
51       */
52      @Parameter( property = "locales", defaultValue = "en" )
53      private String locales;
54  
55      /**
56       * Set this to 'true' to skip site generation and staging.
57       *
58       * @since 3.0
59       */
60      @Parameter( property = "maven.site.skip", defaultValue = "false" )
61      protected boolean skip;
62  
63      /**
64       * SiteTool.
65       */
66      @Component
67      protected SiteTool siteTool;
68  
69      /**
70       * Internationalization.
71       */
72      @Component
73      protected I18N i18n;
74  
75      /**
76       * The Maven project.
77       */
78      @Parameter( defaultValue = "${project}", readonly = true )
79      protected MavenProject project;
80  
81      /**
82       * The local repository.
83       */
84      @Parameter( defaultValue = "${localRepository}", readonly = true )
85      protected ArtifactRepository localRepository;
86  
87      /**
88       * The reactor projects.
89       */
90      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
91      protected List<MavenProject> reactorProjects;
92  
93      /**
94       * Check the current Maven version to see if it's Maven 3.0 or newer.
95       */
96      protected static boolean isMaven3OrMore()
97      {
98          return new ComparableVersion( getMavenVersion() ).compareTo( new ComparableVersion( "3.0" ) ) >= 0;
99      }
100 
101     protected static String getMavenVersion()
102     {
103         // This relies on the fact that MavenProject is the in core classloader
104         // and that the core classloader is for the maven-core artifact
105         // and that should have a pom.properties file
106         // if this ever changes, we will have to revisit this code.
107         final Properties properties = new Properties();
108         final String corePomProperties = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
109         final InputStream in = MavenProject.class.getClassLoader().getResourceAsStream( corePomProperties );
110        try
111         {
112             properties.load( in );
113         }
114         catch ( IOException ioe )
115         {
116             return "";
117         }
118         finally
119         {
120             IOUtil.close( in );
121         }
122 
123         return properties.getProperty( "version" ).trim();
124     }
125 
126     protected List<Locale> getLocales()
127     {
128         return siteTool.getSiteLocales( locales );
129     }
130 }