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.FileInputStream;
35  import java.io.FileOutputStream;
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.io.OutputStream;
39  import org.apache.maven.artifact.ArtifactUtils;
40  import org.apache.maven.execution.MavenSession;
41  import org.apache.maven.plugin.AbstractMojo;
42  import org.apache.maven.plugin.MojoExecutionException;
43  import org.apache.maven.plugin.MojoFailureException;
44  import org.apache.maven.plugin.descriptor.MojoDescriptor;
45  import org.apache.maven.plugins.annotations.Component;
46  import org.apache.maven.plugins.annotations.Parameter;
47  import org.apache.maven.project.MavenProject;
48  import org.apache.maven.project.MavenProjectHelper;
49  
50  
51  
52  
53  
54  
55  
56  public abstract class AbstractAttachMojo extends AbstractMojo
57  {
58  
59      
60  
61  
62      private static final String TOOLNAME = "MavenProjectHelper";
63  
64      
65  
66  
67      private static final String LOG_PREFIX = "[JOMC] ";
68  
69      
70  
71  
72      @Parameter( name = "mavenProject",
73                  defaultValue = "${project}",
74                  readonly = true,
75                  required = true )
76      private MavenProject mavenProject;
77  
78      
79  
80  
81      @Component
82      private MavenProjectHelper mavenProjectHelper;
83  
84      
85  
86  
87  
88  
89      @Parameter( name = "mavenSession",
90                  defaultValue = "${session}",
91                  readonly = true,
92                  required = true )
93      private MavenSession mavenSession;
94  
95      
96  
97  
98  
99  
100     @Parameter( name = "sessionDirectory",
101                 property = "jomc.sessionDirectory",
102                 defaultValue = "${project.build.directory}/jomc-sessions" )
103     private String sessionDirectory;
104 
105     
106 
107 
108 
109 
110     @Parameter( name = "verbose",
111                 property = "jomc.verbose",
112                 defaultValue = "false" )
113     private boolean verbose;
114 
115     
116 
117 
118     public AbstractAttachMojo()
119     {
120         super();
121     }
122 
123     
124 
125 
126 
127 
128 
129 
130 
131 
132     protected MavenProject getMavenProject() throws MojoExecutionException
133     {
134         return this.mavenProject;
135     }
136 
137     
138 
139 
140 
141 
142 
143 
144 
145 
146     protected MavenSession getMavenSession() throws MojoExecutionException
147     {
148         return this.mavenSession;
149     }
150 
151     
152 
153 
154 
155 
156 
157 
158 
159 
160     protected MavenProjectHelper getMavenProjectHelper() throws MojoExecutionException
161     {
162         return this.mavenProjectHelper;
163     }
164 
165     
166 
167 
168 
169 
170 
171 
172 
173 
174     protected File getSessionDirectory() throws MojoExecutionException
175     {
176         File directory = new File( this.sessionDirectory );
177 
178         if ( !directory.isAbsolute() )
179         {
180             directory = new File( this.getMavenProject().getBasedir(), this.sessionDirectory );
181         }
182 
183         return directory;
184     }
185 
186     
187 
188 
189 
190 
191 
192 
193 
194 
195     protected final boolean isVerbose() throws MojoExecutionException
196     {
197         return this.verbose;
198     }
199 
200     
201 
202 
203 
204 
205 
206 
207 
208 
209     protected final void setVerbose( final boolean value ) throws MojoExecutionException
210     {
211         this.verbose = value;
212     }
213 
214     
215 
216 
217 
218 
219     protected abstract File getArtifactFile();
220 
221     
222 
223 
224 
225 
226     protected abstract String getArtifactClassifier();
227 
228     
229 
230 
231 
232 
233     protected abstract String getArtifactType();
234 
235     
236 
237 
238 
239 
240 
241 
242     protected abstract String getExecutionStrategy();
243 
244     public final void execute() throws MojoExecutionException, MojoFailureException
245     {
246         final File attachment =
247             new File( this.getSessionDirectory(),
248                       ArtifactUtils.versionlessKey( this.getMavenProject().getArtifact() ).hashCode()
249                           + "-" + this.getArtifactClassifier()
250                           + "-" + this.getMavenSession().getStartTime().getTime()
251                           + "." + this.getArtifactType() );
252 
253         try
254         {
255             if ( this.isVerbose() && this.getLog().isInfoEnabled() )
256             {
257                 this.getLog().info( LOG_PREFIX + Messages.getMessage( "separator" ) );
258                 this.getLog().info( LOG_PREFIX + Messages.getMessage( "title" ) );
259             }
260 
261             if ( MojoDescriptor.MULTI_PASS_EXEC_STRATEGY.equals( this.getExecutionStrategy() )
262                      || !attachment.exists() )
263             {
264                 if ( this.isVerbose() && this.getLog().isInfoEnabled() )
265                 {
266                     this.getLog().info( LOG_PREFIX + Messages.getMessage( "separator" ) );
267                     this.getLog().info( LOG_PREFIX + Messages.getMessage(
268                         "processingProject", TOOLNAME, this.getMavenProject().getName() == null
269                                                            ? this.getMavenProject().getArtifactId()
270                                                            : this.getMavenProject().getName() ) );
271 
272                 }
273 
274                 if ( this.getArtifactFile().isFile() )
275                 {
276                     if ( attachment.exists() && !attachment.delete() )
277                     {
278                         this.getLog().warn( LOG_PREFIX + Messages.getMessage(
279                             "failedDeletingFile", attachment.getAbsolutePath() ) );
280 
281                     }
282                     if ( !attachment.getParentFile().exists() && !attachment.getParentFile().mkdirs() )
283                     {
284                         throw new MojoExecutionException( Messages.getMessage(
285                             "failedCreatingDirectory", attachment.getParentFile().getAbsolutePath() ) );
286 
287                     }
288 
289                     this.copyFile( this.getArtifactFile(), attachment );
290                     this.getMavenProjectHelper().attachArtifact( this.getMavenProject(), this.getArtifactType(),
291                                                                  this.getArtifactClassifier(), attachment );
292 
293                     if ( this.isVerbose() && this.getLog().isInfoEnabled() )
294                     {
295                         this.getLog().info( LOG_PREFIX + Messages.getMessage(
296                             "creatingAttachment", this.getArtifactFile().getAbsolutePath(),
297                             this.getArtifactClassifier(), this.getArtifactType() ) );
298 
299                         this.getLog().info( LOG_PREFIX + Messages.getMessage( "toolSuccess", TOOLNAME ) );
300                     }
301                 }
302                 else if ( this.getLog().isWarnEnabled() )
303                 {
304                     this.getLog().warn( LOG_PREFIX + Messages.getMessage(
305                         "artifactFileNotFound", this.getArtifactFile().getAbsolutePath() ) );
306 
307                 }
308             }
309             else if ( this.isVerbose() && this.getLog().isInfoEnabled() )
310             {
311                 this.getLog().info( LOG_PREFIX + Messages.getMessage( "executionSuppressed",
312                                                                       this.getExecutionStrategy() ) );
313 
314             }
315         }
316         catch ( final IOException e )
317         {
318             final String message = Messages.getMessage( e );
319             throw new MojoExecutionException( Messages.getMessage(
320                 "failedCopying", this.getArtifactFile().getAbsolutePath(), attachment.getAbsolutePath(),
321                 message != null ? message : "" ), e );
322 
323         }
324         finally
325         {
326             if ( this.isVerbose() && this.getLog().isInfoEnabled() )
327             {
328                 this.getLog().info( LOG_PREFIX + Messages.getMessage( "separator" ) );
329             }
330         }
331     }
332 
333     
334 
335 
336 
337 
338 
339 
340 
341 
342 
343     protected final void copyFile( final File source, final File target ) throws IOException
344     {
345         InputStream in = null;
346         OutputStream out = null;
347         try
348         {
349             if ( !source.equals( target ) )
350             {
351                 in = new FileInputStream( source );
352                 out = new FileOutputStream( target );
353 
354                 final byte[] buffer = new byte[ 65536 ];
355 
356                 for ( int read = in.read( buffer );
357                       read >= 0;
358                       out.write( buffer, 0, read ), read = in.read( buffer ) );
359 
360                 out.close();
361                 out = null;
362 
363                 in.close();
364                 in = null;
365             }
366         }
367         finally
368         {
369             try
370             {
371                 if ( out != null )
372                 {
373                     out.close();
374                 }
375             }
376             catch ( final IOException e )
377             {
378                 this.getLog().warn( e );
379             }
380             finally
381             {
382                 try
383                 {
384                     if ( in != null )
385                     {
386                         in.close();
387                     }
388                 }
389                 catch ( final IOException e )
390                 {
391                     this.getLog().warn( e );
392                 }
393             }
394         }
395     }
396 
397 }