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.doxia.tools.SiteTool;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.project.MavenProject;
33
34 import org.codehaus.plexus.i18n.I18N;
35 import org.codehaus.plexus.util.IOUtil;
36 import org.codehaus.plexus.util.ReaderFactory;
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 supported by Maven. The first valid token will be the default Locale
48 * for this instance of the Java Virtual Machine.
49 *
50 * @parameter expression="${locales}"
51 */
52 protected String locales;
53
54 /**
55 * SiteTool.
56 *
57 * @component
58 */
59 protected SiteTool siteTool;
60
61 /**
62 * Internationalization.
63 *
64 * @component
65 */
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 default-value="${basedir}/src/site"
72 */
73 protected File siteDirectory;
74
75 /**
76 * The maven project.
77 *
78 * @parameter default-value="${project}"
79 * @required
80 * @readonly
81 */
82 protected MavenProject project;
83
84 /**
85 * The local repository.
86 *
87 * @parameter default-value="${localRepository}"
88 * @readonly
89 */
90 protected ArtifactRepository localRepository;
91
92 /**
93 * The reactor projects.
94 *
95 * @parameter default-value="${reactorProjects}"
96 * @required
97 * @readonly
98 */
99 protected List<MavenProject> reactorProjects;
100
101 /**
102 * Specifies the input encoding.
103 *
104 * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
105 */
106 private String inputEncoding;
107
108 /**
109 * Specifies the output encoding.
110 *
111 * @parameter expression="${outputEncoding}" default-value="${project.reporting.outputEncoding}"
112 */
113 private String outputEncoding;
114
115 /**
116 * Gets the input files encoding.
117 *
118 * @return The input files encoding, never <code>null</code>.
119 */
120 protected String getInputEncoding()
121 {
122 return ( inputEncoding == null ) ? ReaderFactory.ISO_8859_1 : inputEncoding;
123 }
124
125 /**
126 * Gets the effective reporting output files encoding.
127 *
128 * @return The effective reporting output file encoding, never <code>null</code>.
129 */
130 protected String getOutputEncoding()
131 {
132 return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
133 }
134
135 /**
136 * Check the current Maven version and emit a warning if it's Maven 3.
137 * This plugin does not work with Maven 3.x.
138 */
139 protected void checkMavenVersion()
140 {
141 try
142 {
143 final String version = getMavenVersion();
144
145 if ( version.startsWith( "3" ) )
146 {
147 getLog().warn( "Running 2.x site-plugin with Maven 3, use site-plugin-3.x instead!" );
148 }
149 }
150 catch ( IOException e )
151 {
152 getLog().debug( "Unable to determine Maven version", e );
153 }
154 }
155
156 protected String getMavenVersion()
157 throws IOException
158 {
159 // This relies on the fact that MavenProject is the in core classloader
160 // and that the core classloader is for the maven-core artifact
161 // and that should have a pom.properties file
162 // if this ever changes, we will have to revisit this code.
163 final Properties properties = new Properties();
164 final InputStream in =
165 MavenProject.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" );
166 try
167 {
168 properties.load( in );
169 }
170 finally
171 {
172 IOUtil.close( in );
173 }
174
175 return properties.getProperty( "version" ).trim();
176 }
177 }