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      @Parameter( property = "locales" )
54      protected String locales;
55  
56      /**
57       * SiteTool.
58       */
59      @Component
60      protected SiteTool siteTool;
61  
62      /**
63       * Internationalization.
64       */
65      @Component
66      protected I18N i18n;
67  
68      /**
69       * Directory containing the site.xml file and the source for apt, fml and xdoc docs.
70       */
71      @Parameter( defaultValue = "${basedir}/src/site" )
72      protected File siteDirectory;
73  
74      /**
75       * The maven project.
76       */
77      @Component
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       * Specifies the input encoding.
94       */
95      @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
96      private String inputEncoding;
97  
98      /**
99       * Specifies the output encoding.
100      */
101     @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
102     private String outputEncoding;
103 
104     /**
105      * Gets the input files encoding.
106      *
107      * @return The input files encoding, never <code>null</code>.
108      */
109     protected String getInputEncoding()
110     {
111         return ( inputEncoding == null ) ? ReaderFactory.ISO_8859_1 : inputEncoding;
112     }
113 
114     /**
115      * Gets the effective reporting output files encoding.
116      *
117      * @return The effective reporting output file encoding, never <code>null</code>.
118      */
119     protected String getOutputEncoding()
120     {
121         return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
122     }
123 
124     /**
125      * Check the current Maven version to see if it's Maven 3.0 or newer.
126      */
127     protected static boolean isMaven3OrMore()
128     {
129         return new ComparableVersion( getMavenVersion() ).compareTo( new ComparableVersion( "3.0" ) ) >= 0;
130     }
131 
132     protected static String getMavenVersion()
133     {
134         // This relies on the fact that MavenProject is the in core classloader
135         // and that the core classloader is for the maven-core artifact
136         // and that should have a pom.properties file
137         // if this ever changes, we will have to revisit this code.
138         final Properties properties = new Properties();
139         final InputStream in =
140             MavenProject.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" );
141         try
142         {
143             properties.load( in );
144         }
145         catch ( IOException ioe )
146         {
147             return "";
148         }
149         finally
150         {
151             IOUtil.close( in );
152         }
153 
154         return properties.getProperty( "version" ).trim();
155     }
156 }