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.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import java.util.List;
27  import java.util.Properties;
28  
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.versioning.ComparableVersion;
31  import org.apache.maven.doxia.tools.SiteTool;
32  import org.apache.maven.plugin.AbstractMojo;
33  import org.apache.maven.plugins.annotations.Component;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.project.MavenProject;
36  
37  import org.codehaus.plexus.i18n.I18N;
38  import org.codehaus.plexus.util.IOUtil;
39  import org.codehaus.plexus.util.ReaderFactory;
40  
41  /**
42   * Base class for site mojos.
43   *
44   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
45   */
46  public abstract class AbstractSiteMojo
47      extends AbstractMojo
48  {
49      /**
50       * A comma separated list of locales supported by Maven. The first valid token will be the default Locale
51       * for this instance of the Java Virtual Machine.
52       *
53       * @since 2.3
54       */
55      @Parameter( property = "locales" )
56      protected String locales;
57  
58      /**
59       * SiteTool.
60       */
61      @Component
62      protected SiteTool siteTool;
63  
64      /**
65       * Internationalization.
66       */
67      @Component
68      protected I18N i18n;
69  
70      /**
71       * Directory containing the site.xml file and the source for apt, fml and xdoc docs.
72       *
73       * @since 2.3
74       */
75      @Parameter( defaultValue = "${basedir}/src/site" )
76      protected File siteDirectory;
77  
78      /**
79       * The maven project.
80       */
81      @Parameter( defaultValue = "${project}", readonly = true )
82      protected MavenProject project;
83  
84      /**
85       * The local repository.
86       */
87      @Parameter( defaultValue = "${localRepository}", readonly = true )
88      protected ArtifactRepository localRepository;
89  
90      /**
91       * The reactor projects.
92       */
93      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
94      protected List<MavenProject> reactorProjects;
95  
96      /**
97       * Specifies the input encoding.
98       *
99       * @since 2.3
100      */
101     @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
102     private String inputEncoding;
103 
104     /**
105      * Specifies the output encoding.
106      *
107      * @since 2.3
108      */
109     @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
110     private String outputEncoding;
111 
112     /**
113      * Gets the input files encoding.
114      *
115      * @return The input files encoding, never <code>null</code>.
116      */
117     protected String getInputEncoding()
118     {
119         return ( inputEncoding == null ) ? ReaderFactory.ISO_8859_1 : inputEncoding;
120     }
121 
122     /**
123      * Gets the effective reporting output files encoding.
124      *
125      * @return The effective reporting output file encoding, never <code>null</code>.
126      */
127     protected String getOutputEncoding()
128     {
129         return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
130     }
131 
132     /**
133      * Check the current Maven version to see if it's Maven 3.0 or newer.
134      */
135     protected static boolean isMaven3OrMore()
136     {
137         return new ComparableVersion( getMavenVersion() ).compareTo( new ComparableVersion( "3.0" ) ) >= 0;
138     }
139 
140     protected static String getMavenVersion()
141     {
142         // This relies on the fact that MavenProject is the in core classloader
143         // and that the core classloader is for the maven-core artifact
144         // and that should have a pom.properties file
145         // if this ever changes, we will have to revisit this code.
146         final Properties properties = new Properties();
147         final InputStream in =
148             MavenProject.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" );
149         try
150         {
151             properties.load( in );
152         }
153         catch ( IOException ioe )
154         {
155             return "";
156         }
157         finally
158         {
159             IOUtil.close( in );
160         }
161 
162         return properties.getProperty( "version" ).trim();
163     }
164 }