Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractLogMojo |
|
| 3.6666666666666665;3,667 |
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 | * @version $Id: AbstractLogMojo.java 723979 2008-12-06 13:25:46Z bentmann $ | |
37 | */ | |
38 | 0 | 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 expression="${log.logFile}" | |
54 | */ | |
55 | private File logFile; | |
56 | ||
57 | /** | |
58 | * The character encoding of the log file. | |
59 | */ | |
60 | 0 | 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 | 0 | 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 | 0 | File file = getLogFile(); |
87 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Updating log file: " + file ); |
88 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] " + value ); |
89 | try | |
90 | { | |
91 | 0 | file.getParentFile().mkdirs(); |
92 | 0 | OutputStream out = new FileOutputStream( file, true ); |
93 | try | |
94 | { | |
95 | 0 | BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out, encoding ) ); |
96 | 0 | if ( value != null ) |
97 | { | |
98 | 0 | writer.write( value.toString() ); |
99 | 0 | writer.newLine(); |
100 | 0 | writer.flush(); |
101 | } | |
102 | } | |
103 | finally | |
104 | { | |
105 | 0 | try |
106 | { | |
107 | 0 | out.close(); |
108 | } | |
109 | 0 | catch ( IOException e ) |
110 | { | |
111 | // just ignore, we tried our best to clean up | |
112 | 0 | } |
113 | 0 | } |
114 | } | |
115 | 0 | catch ( IOException e ) |
116 | { | |
117 | 0 | throw new MojoExecutionException( "Failed to update log file " + logFile, e ); |
118 | 0 | } |
119 | 0 | } |
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 | 0 | File file = getLogFile(); |
130 | 0 | 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 | 0 | file.getParentFile().mkdirs(); |
137 | 0 | OutputStream out = new FileOutputStream( file ); |
138 | try | |
139 | { | |
140 | 0 | out.close(); |
141 | } | |
142 | 0 | catch ( IOException e ) |
143 | { | |
144 | // just ignore, we tried our best to clean up | |
145 | 0 | } |
146 | } | |
147 | 0 | catch ( IOException e ) |
148 | { | |
149 | 0 | throw new MojoExecutionException( "Failed to reset log file " + logFile, e ); |
150 | 0 | } |
151 | 0 | } |
152 | ||
153 | } |