View Javadoc
1   package org.apache.maven.plugin.coreit;
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.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.reporting.MavenReport;
26  import org.apache.maven.reporting.MavenReportException;
27  
28  import org.codehaus.doxia.sink.Sink;
29  
30  import java.io.File;
31  import java.io.FileOutputStream;
32  import java.io.IOException;
33  import java.io.OutputStream;
34  import java.util.Locale;
35  import java.util.Properties;
36  
37  /**
38   * Creates a properties file in the site output directory.
39   *
40   * @goal info
41   *
42   * @author Benjamin Bentmann
43   *
44   */
45  public class InfoReport
46      extends AbstractMojo
47      implements MavenReport
48  {
49  
50      /**
51       * The base directory of the current Maven project.
52       *
53       * @parameter default-value="${basedir}"
54       * @required
55       * @readonly
56       */
57      private File basedir;
58  
59      /**
60       * The path to the properties file, relative to the output directory of the site. The keys
61       * <code>locale.language</code>, <code>locale.country</code> and <code>locale.variant</code> indicate the report's
62       * locale.
63       *
64       * @parameter default-value="info.properties"
65       */
66      private String infoFile = "info.properties";
67  
68      /**
69       * The path to the output directory of the site.
70       *
71       * @parameter default-value="${project.reporting.outputDirectory}"
72       */
73      private File outputDirectory;
74  
75      /**
76       * The locale for the report.
77       */
78      private Locale locale;
79  
80      /**
81       * Runs this mojo.
82       *
83       * @throws MojoExecutionException If the output file could not be created.
84       * @throws MojoFailureException If the output file has not been set.
85       */
86      public void execute()
87          throws MojoExecutionException, MojoFailureException
88      {
89          getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + infoFile );
90  
91          if ( infoFile == null || infoFile.length() <= 0 )
92          {
93              throw new MojoFailureException( "Path name for output file has not been specified" );
94          }
95  
96          File outputFile = new File( outputDirectory, infoFile );
97          if ( !outputFile.isAbsolute() )
98          {
99              outputFile = new File( new File( basedir, outputDirectory.getPath() ), infoFile ).getAbsoluteFile();
100         }
101 
102         Properties props = new Properties();
103         props.setProperty( "site.output.directory", outputDirectory.getPath() );
104         if ( locale != null )
105         {
106             props.setProperty( "locale.language", locale.getLanguage() );
107             props.setProperty( "locale.country", locale.getCountry() );
108             props.setProperty( "locale.variant", locale.getVariant() );
109         }
110 
111         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + outputFile );
112 
113         OutputStream out = null;
114         try
115         {
116             outputFile.getParentFile().mkdirs();
117             out = new FileOutputStream( outputFile );
118             props.store( out, "MAVEN-CORE-IT-LOG" );
119         }
120         catch ( IOException e )
121         {
122             throw new MojoExecutionException( "Output file could not be created: " + outputFile, e );
123         }
124         finally
125         {
126             if ( out != null )
127             {
128                 try
129                 {
130                     out.close();
131                 }
132                 catch ( IOException e )
133                 {
134                     // just ignore
135                 }
136             }
137         }
138 
139         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + outputFile );
140     }
141 
142     /**
143      * Runs this report.
144      *
145      * @throws MavenReportException If the report could not be created.
146      */
147     public void generate( Sink sink, Locale locale )
148         throws MavenReportException
149     {
150         this.locale = locale;
151         try
152         {
153             execute();
154         }
155         catch ( Exception e )
156         {
157             throw new MavenReportException( "Report could not be created", e );
158         }
159     }
160 
161     public String getOutputName()
162     {
163         return "info";
164     }
165 
166     public String getCategoryName()
167     {
168         return "Project Reports";
169     }
170 
171     public String getName( Locale locale )
172     {
173         return "name";
174     }
175 
176     public String getDescription( Locale locale )
177     {
178         return "description";
179     }
180 
181     public void setReportOutputDirectory( File outputDirectory )
182     {
183         this.outputDirectory = outputDirectory;
184     }
185 
186     public File getReportOutputDirectory()
187     {
188         return outputDirectory;
189     }
190 
191     public boolean isExternalReport()
192     {
193         return true;
194     }
195 
196     public boolean canGenerateReport()
197     {
198         return true;
199     }
200 
201 }