1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.mojo;
32
33 import java.io.File;
34 import java.io.IOException;
35 import org.apache.commons.io.FileUtils;
36 import org.apache.maven.artifact.ArtifactUtils;
37 import org.apache.maven.execution.MavenSession;
38 import org.apache.maven.plugin.AbstractMojo;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.plugin.MojoFailureException;
41 import org.apache.maven.plugin.descriptor.MojoDescriptor;
42 import org.apache.maven.project.MavenProject;
43 import org.apache.maven.project.MavenProjectHelper;
44
45
46
47
48
49
50
51 public abstract class AbstractAttachMojo extends AbstractMojo
52 {
53
54
55 private static final String TOOLNAME = "MavenProjectHelper";
56
57
58 private static final String LOG_PREFIX = "[JOMC] ";
59
60
61
62
63
64
65
66
67 private MavenProject mavenProject;
68
69
70
71
72
73
74
75
76 private MavenProjectHelper mavenProjectHelper;
77
78
79
80
81
82
83
84
85
86 private MavenSession mavenSession;
87
88
89
90
91
92
93
94 private String sessionDirectory;
95
96
97
98
99
100
101
102 private boolean verbose;
103
104
105 public AbstractAttachMojo()
106 {
107 super();
108 }
109
110
111
112
113
114
115
116
117
118
119 protected MavenProject getMavenProject() throws MojoExecutionException
120 {
121 return this.mavenProject;
122 }
123
124
125
126
127
128
129
130
131
132
133 protected MavenSession getMavenSession() throws MojoExecutionException
134 {
135 return this.mavenSession;
136 }
137
138
139
140
141
142
143
144
145
146
147 protected MavenProjectHelper getMavenProjectHelper() throws MojoExecutionException
148 {
149 return this.mavenProjectHelper;
150 }
151
152
153
154
155
156
157
158
159
160
161 protected File getSessionDirectory() throws MojoExecutionException
162 {
163 File directory = new File( this.sessionDirectory );
164
165 if ( !directory.isAbsolute() )
166 {
167 directory = new File( this.getMavenProject().getBasedir(), this.sessionDirectory );
168 }
169
170 return directory;
171 }
172
173
174
175
176
177
178
179
180
181
182 protected final boolean isVerbose() throws MojoExecutionException
183 {
184 return this.verbose;
185 }
186
187
188
189
190
191
192
193
194
195
196 protected final void setVerbose( final boolean value ) throws MojoExecutionException
197 {
198 this.verbose = value;
199 }
200
201
202
203
204
205
206 protected abstract File getArtifactFile();
207
208
209
210
211
212
213 protected abstract String getArtifactClassifier();
214
215
216
217
218
219
220 protected abstract String getArtifactType();
221
222
223
224
225
226
227
228
229 protected abstract String getExecutionStrategy();
230
231 public final void execute() throws MojoExecutionException, MojoFailureException
232 {
233 final File attachment =
234 new File( this.getSessionDirectory(),
235 ArtifactUtils.versionlessKey( this.getMavenProject().getArtifact() ).hashCode()
236 + "-" + this.getArtifactClassifier()
237 + "-" + this.getMavenSession().getStartTime().getTime()
238 + "." + this.getArtifactType() );
239
240 try
241 {
242 if ( this.isVerbose() && this.getLog().isInfoEnabled() )
243 {
244 this.getLog().info( LOG_PREFIX + Messages.getMessage( "separator" ) );
245 this.getLog().info( LOG_PREFIX + Messages.getMessage( "title" ) );
246 }
247
248 if ( MojoDescriptor.MULTI_PASS_EXEC_STRATEGY.equals( this.getExecutionStrategy() )
249 || !attachment.exists() )
250 {
251 if ( this.isVerbose() && this.getLog().isInfoEnabled() )
252 {
253 this.getLog().info( LOG_PREFIX + Messages.getMessage( "separator" ) );
254 this.getLog().info( LOG_PREFIX + Messages.getMessage(
255 "processingProject", TOOLNAME, this.getMavenProject().getName() == null
256 ? this.getMavenProject().getArtifactId()
257 : this.getMavenProject().getName() ) );
258
259 }
260
261 if ( this.getArtifactFile().isFile() )
262 {
263 if ( attachment.exists() && !attachment.delete() )
264 {
265 this.getLog().warn( LOG_PREFIX + Messages.getMessage(
266 "failedDeletingFile", attachment.getAbsolutePath() ) );
267
268 }
269 if ( !attachment.getParentFile().exists() && !attachment.getParentFile().mkdirs() )
270 {
271 throw new MojoExecutionException( Messages.getMessage(
272 "failedCreatingDirectory", attachment.getParentFile().getAbsolutePath() ) );
273
274 }
275
276 FileUtils.copyFile( this.getArtifactFile(), attachment );
277 this.getMavenProjectHelper().attachArtifact( this.getMavenProject(), this.getArtifactType(),
278 this.getArtifactClassifier(), attachment );
279
280 if ( this.isVerbose() && this.getLog().isInfoEnabled() )
281 {
282 this.getLog().info( LOG_PREFIX + Messages.getMessage(
283 "creatingAttachment", this.getArtifactFile().getAbsolutePath(),
284 this.getArtifactClassifier(), this.getArtifactType() ) );
285
286 this.getLog().info( LOG_PREFIX + Messages.getMessage( "toolSuccess", TOOLNAME ) );
287 }
288 }
289 else if ( this.getLog().isWarnEnabled() )
290 {
291 this.getLog().warn( LOG_PREFIX + Messages.getMessage(
292 "artifactFileNotFound", this.getArtifactFile().getAbsolutePath() ) );
293
294 }
295 }
296 else if ( this.isVerbose() && this.getLog().isInfoEnabled() )
297 {
298 this.getLog().info( LOG_PREFIX + Messages.getMessage( "executionSuppressed",
299 this.getExecutionStrategy() ) );
300
301 }
302 }
303 catch ( final IOException e )
304 {
305 final String message = Messages.getMessage( e );
306 throw new MojoExecutionException( Messages.getMessage(
307 "failedCopying", this.getArtifactFile().getAbsolutePath(), attachment.getAbsolutePath(),
308 message != null ? message : "" ), e );
309
310 }
311 finally
312 {
313 if ( this.isVerbose() && this.getLog().isInfoEnabled() )
314 {
315 this.getLog().info( LOG_PREFIX + Messages.getMessage( "separator" ) );
316 }
317 }
318 }
319
320 }