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