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  
25  import java.io.BufferedWriter;
26  import java.io.File;
27  import java.io.FileOutputStream;
28  import java.io.IOException;
29  import java.io.OutputStream;
30  import java.io.OutputStreamWriter;
31  
32  /**
33   * Provides common services for the mojos of this plugin.
34   *
35   * @author Benjamin Bentmann
36   *
37   */
38  public abstract class AbstractLogMojo
39      extends AbstractMojo
40  {
41  
42      /**
43       * The project's base directory, used for manual path translation.
44       *
45       * @parameter default-value="${basedir}"
46       * @readonly
47       */
48      private File basedir;
49  
50      /**
51       * The path to the output file, relative to the project's base directory.
52       *
53       * @parameter property="log.logFile"
54       */
55      private File logFile;
56  
57      /**
58       * The character encoding of the log file.
59       */
60      private String encoding = "UTF-8";
61  
62      /**
63       * Gets the absolute path to the log file.
64       *
65       * @return The absolute path to the log file, never <code>null</code>.
66       */
67      private File getLogFile()
68      {
69          /*
70           * NOTE: We don't want to test path translation here.
71           */
72          return logFile.isAbsolute() ? logFile : new File( basedir, logFile.getPath() );
73      }
74  
75      /**
76       * Appends the string representation of the specified object to the log file. Logging <code>null</code> has no other
77       * effect than touching the file. For each value different from <code>null</code>, a line terminator will be
78       * appended to the value's string representation.
79       *
80       * @param value The object to log, may be <code>null</code>.
81       * @throws MojoExecutionException If the log file could not be updated.
82       */
83      protected void append( Object value )
84          throws MojoExecutionException
85      {
86          File file = getLogFile();
87          getLog().info( "[MAVEN-CORE-IT-LOG] Updating log file: " + file );
88          getLog().info( "[MAVEN-CORE-IT-LOG]   " + value );
89          try
90          {
91              file.getParentFile().mkdirs();
92              OutputStream out = new FileOutputStream( file, true );
93              try
94              {
95                  BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out, encoding ) );
96                  if ( value != null )
97                  {
98                      writer.write( value.toString() );
99                      writer.newLine();
100                     writer.flush();
101                 }
102             }
103             finally
104             {
105                 try
106                 {
107                     out.close();
108                 }
109                 catch ( IOException e )
110                 {
111                     // just ignore, we tried our best to clean up
112                 }
113             }
114         }
115         catch ( IOException e )
116         {
117             throw new MojoExecutionException( "Failed to update log file " + logFile, e );
118         }
119     }
120 
121     /**
122      * Clears the contents of the log file by creating a new empty log file.
123      *
124      * @throws MojoExecutionException If the log file could not be reset.
125      */
126     protected void reset()
127         throws MojoExecutionException
128     {
129         File file = getLogFile();
130         getLog().info( "[MAVEN-CORE-IT-LOG] Resetting log file: " + file );
131         try
132         {
133             /*
134              * NOTE: Intentionally don't delete the file but create a new empty one to check the plugin was executed.
135              */
136             file.getParentFile().mkdirs();
137             OutputStream out = new FileOutputStream( file );
138             try
139             {
140                 out.close();
141             }
142             catch ( IOException e )
143             {
144                 // just ignore, we tried our best to clean up
145             }
146         }
147         catch ( IOException e )
148         {
149             throw new MojoExecutionException( "Failed to reset log file " + logFile, e );
150         }
151     }
152 
153 }