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
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.util.List;
31 import java.util.Properties;
32
33 /**
34 * Lists the available/configured reports in a properties file.
35 *
36 * @goal list
37 * @phase initialize
38 * @requiresReports true
39 *
40 * @author Benjamin Bentmann
41 *
42 */
43 public class ListMojo
44 extends AbstractMojo
45 {
46
47 /**
48 * The path to the properties file used to list the available reports. The properties file will have a key named
49 * <code>reports</code> that gives the total count of reports. The keys <code>reports.0</code>,
50 * <code>reports.1</code> etc. will be used to denote the qualified class names of the reports.
51 *
52 * @parameter property="site.properties" default-value="target/reports.properties"
53 */
54 private File reportsFile;
55
56 /**
57 * The reports configured for the current build.
58 *
59 * @parameter default-value="${reports}"
60 * @required
61 * @readonly
62 */
63 private List reports;
64
65 /**
66 * Runs this mojo.
67 *
68 * @throws MojoExecutionException If the output file could not be created.
69 */
70 public void execute()
71 throws MojoExecutionException, MojoFailureException
72 {
73 Properties reportProperties = new Properties();
74
75 reportProperties.setProperty( "reports", "" + reports.size() );
76
77 for ( int i = 0; i < reports.size(); i++ )
78 {
79 Object report = reports.get( i );
80 getLog().info( "[MAVEN-CORE-IT-LOG] Listing report " + report );
81 reportProperties.setProperty( "reports." + i, report.getClass().getName() );
82 }
83
84 getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + reportsFile );
85
86 OutputStream out = null;
87 try
88 {
89 reportsFile.getParentFile().mkdirs();
90 out = new FileOutputStream( reportsFile );
91 reportProperties.store( out, "MAVEN-CORE-IT-LOG" );
92 }
93 catch ( IOException e )
94 {
95 throw new MojoExecutionException( "Output file could not be created: " + reportsFile, e );
96 }
97 finally
98 {
99 if ( out != null )
100 {
101 try
102 {
103 out.close();
104 }
105 catch ( IOException e )
106 {
107 // just ignore
108 }
109 }
110 }
111
112 getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + reportsFile );
113 }
114
115 }