1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36
37
38 public abstract class AbstractLogMojo
39 extends AbstractMojo
40 {
41
42
43
44
45
46
47
48 private File basedir;
49
50
51
52
53
54
55 private File logFile;
56
57
58
59
60 private String encoding = "UTF-8";
61
62
63
64
65
66
67 private File getLogFile()
68 {
69
70
71
72 return logFile.isAbsolute() ? logFile : new File( basedir, logFile.getPath() );
73 }
74
75
76
77
78
79
80
81
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
112 }
113 }
114 }
115 catch ( IOException e )
116 {
117 throw new MojoExecutionException( "Failed to update log file " + logFile, e );
118 }
119 }
120
121
122
123
124
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
135
136 file.getParentFile().mkdirs();
137 OutputStream out = new FileOutputStream( file );
138 try
139 {
140 out.close();
141 }
142 catch ( IOException e )
143 {
144
145 }
146 }
147 catch ( IOException e )
148 {
149 throw new MojoExecutionException( "Failed to reset log file " + logFile, e );
150 }
151 }
152
153 }