View Javadoc
1   /*
2    *   Copyright (C) Christian Schulte <cs@schulte.it>, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: AbstractAttachMojo.java 5043 2015-05-27 07:03:39Z schulte $
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   * Base class for attaching artifacts to a project.
47   *
48   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
49   * @version $JOMC: AbstractAttachMojo.java 5043 2015-05-27 07:03:39Z schulte $
50   */
51  public abstract class AbstractAttachMojo extends AbstractMojo
52  {
53  
54      /**
55       * Constant for the name of the tool backing the mojo.
56       */
57      private static final String TOOLNAME = "MavenProjectHelper";
58  
59      /**
60       * Prefix prepended to log messages.
61       */
62      private static final String LOG_PREFIX = "[JOMC] ";
63  
64      /**
65       * The Maven project of the instance.
66       *
67       * @parameter expression="${project}"
68       * @required
69       * @readonly
70       */
71      private MavenProject mavenProject;
72  
73      /**
74       * The Maven ProjectHelper of the instance.
75       *
76       * @component
77       * @required
78       * @readonly
79       */
80      private MavenProjectHelper mavenProjectHelper;
81  
82      /**
83       * The Maven session of the instance.
84       *
85       * @parameter expression="${session}"
86       * @required
87       * @readonly
88       * @since 1.1
89       */
90      private MavenSession mavenSession;
91  
92      /**
93       * Directory holding the session related files of the project.
94       *
95       * @parameter default-value="${project.build.directory}/jomc-sessions" expression="${jomc.sessionDirectory}"
96       * @since 1.1
97       */
98      private String sessionDirectory;
99  
100     /**
101      * Controls verbosity of the plugin.
102      *
103      * @parameter expression="${jomc.verbose}" default-value="false"
104      * @since 1.1
105      */
106     private boolean verbose;
107 
108     /**
109      * Creates a new {@code AbstractAttachMojo} instance.
110      */
111     public AbstractAttachMojo()
112     {
113         super();
114     }
115 
116     /**
117      * Gets the Maven project of the instance.
118      *
119      * @return The Maven project of the instance.
120      *
121      * @throws MojoExecutionException if getting the Maven project of the instance fails.
122      *
123      * @since 1.1
124      */
125     protected MavenProject getMavenProject() throws MojoExecutionException
126     {
127         return this.mavenProject;
128     }
129 
130     /**
131      * Gets the Maven session of the instance.
132      *
133      * @return The Maven session of the instance.
134      *
135      * @throws MojoExecutionException if getting the Maven session of the instance fails.
136      *
137      * @since 1.1
138      */
139     protected MavenSession getMavenSession() throws MojoExecutionException
140     {
141         return this.mavenSession;
142     }
143 
144     /**
145      * Gets the Maven project helper of the instance.
146      *
147      * @return The Maven project helper of the instance.
148      *
149      * @throws MojoExecutionException if getting the Maven project helper of the instance fails.
150      *
151      * @since 1.1
152      */
153     protected MavenProjectHelper getMavenProjectHelper() throws MojoExecutionException
154     {
155         return this.mavenProjectHelper;
156     }
157 
158     /**
159      * Gets the directory holding the session related files of the project.
160      *
161      * @return The directory holding the session related files of the project.
162      *
163      * @throws MojoExecutionException if getting the directory fails.
164      *
165      * @since 1.1
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      * Gets a flag indicating verbose output is enabled.
181      *
182      * @return {@code true}, if verbose output is enabled; {@code false}, if information messages are suppressed.
183      *
184      * @throws MojoExecutionException if getting the flag fails.
185      *
186      * @since 1.1
187      */
188     protected final boolean isVerbose() throws MojoExecutionException
189     {
190         return this.verbose;
191     }
192 
193     /**
194      * Sets the flag indicating verbose output is enabled.
195      *
196      * @param value {@code true}, to enable verbose output; {@code false}, to suppress information messages.
197      *
198      * @throws MojoExecutionException if setting the flag fails.
199      *
200      * @since 1.1
201      */
202     protected final void setVerbose( final boolean value ) throws MojoExecutionException
203     {
204         this.verbose = value;
205     }
206 
207     /**
208      * Gets the file of the artifact to attach.
209      *
210      * @return The file of the artifact to attach.
211      */
212     protected abstract File getArtifactFile();
213 
214     /**
215      * Gets the classifier of the artifact to attach.
216      *
217      * @return The classifier of the artifact to attach.
218      */
219     protected abstract String getArtifactClassifier();
220 
221     /**
222      * Gets the type of the artifact to attach.
223      *
224      * @return The type of the artifact to attach.
225      */
226     protected abstract String getArtifactType();
227 
228     /**
229      * Gets the execution strategy of the instance.
230      *
231      * @return The execution strategy of the instance.
232      *
233      * @since 1.1
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 }