View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 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 4613 2012-09-22 10:07:08Z 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 4613 2012-09-22 10:07:08Z schulte $
50   */
51  public abstract class AbstractAttachMojo extends AbstractMojo
52  {
53  
54      /** Constant for the name of the tool backing the mojo. */
55      private static final String TOOLNAME = "MavenProjectHelper";
56  
57      /** Prefix prepended to log messages. */
58      private static final String LOG_PREFIX = "[JOMC] ";
59  
60      /**
61       * The Maven project of the instance.
62       *
63       * @parameter expression="${project}"
64       * @required
65       * @readonly
66       */
67      private MavenProject mavenProject;
68  
69      /**
70       * The Maven ProjectHelper of the instance.
71       *
72       * @component
73       * @required
74       * @readonly
75       */
76      private MavenProjectHelper mavenProjectHelper;
77  
78      /**
79       * The Maven session of the instance.
80       *
81       * @parameter expression="${session}"
82       * @required
83       * @readonly
84       * @since 1.1
85       */
86      private MavenSession mavenSession;
87  
88      /**
89       * Directory holding the session related files of the project.
90       *
91       * @parameter default-value="${project.build.directory}/jomc-sessions" expression="${jomc.sessionDirectory}"
92       * @since 1.1
93       */
94      private String sessionDirectory;
95  
96      /**
97       * Controls verbosity of the plugin.
98       *
99       * @parameter expression="${jomc.verbose}" default-value="false"
100      * @since 1.1
101      */
102     private boolean verbose;
103 
104     /** Creates a new {@code AbstractAttachMojo} instance. */
105     public AbstractAttachMojo()
106     {
107         super();
108     }
109 
110     /**
111      * Gets the Maven project of the instance.
112      *
113      * @return The Maven project of the instance.
114      *
115      * @throws MojoExecutionException if getting the Maven project of the instance fails.
116      *
117      * @since 1.1
118      */
119     protected MavenProject getMavenProject() throws MojoExecutionException
120     {
121         return this.mavenProject;
122     }
123 
124     /**
125      * Gets the Maven session of the instance.
126      *
127      * @return The Maven session of the instance.
128      *
129      * @throws MojoExecutionException if getting the Maven session of the instance fails.
130      *
131      * @since 1.1
132      */
133     protected MavenSession getMavenSession() throws MojoExecutionException
134     {
135         return this.mavenSession;
136     }
137 
138     /**
139      * Gets the Maven project helper of the instance.
140      *
141      * @return The Maven project helper of the instance.
142      *
143      * @throws MojoExecutionException if getting the Maven project helper of the instance fails.
144      *
145      * @since 1.1
146      */
147     protected MavenProjectHelper getMavenProjectHelper() throws MojoExecutionException
148     {
149         return this.mavenProjectHelper;
150     }
151 
152     /**
153      * Gets the directory holding the session related files of the project.
154      *
155      * @return The directory holding the session related files of the project.
156      *
157      * @throws MojoExecutionException if getting the directory fails.
158      *
159      * @since 1.1
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      * Gets a flag indicating verbose output is enabled.
175      *
176      * @return {@code true}, if verbose output is enabled; {@code false}, if information messages are suppressed.
177      *
178      * @throws MojoExecutionException if getting the flag fails.
179      *
180      * @since 1.1
181      */
182     protected final boolean isVerbose() throws MojoExecutionException
183     {
184         return this.verbose;
185     }
186 
187     /**
188      * Sets the flag indicating verbose output is enabled.
189      *
190      * @param value {@code true}, to enable verbose output; {@code false}, to suppress information messages.
191      *
192      * @throws MojoExecutionException if setting the flag fails.
193      *
194      * @since 1.1
195      */
196     protected final void setVerbose( final boolean value ) throws MojoExecutionException
197     {
198         this.verbose = value;
199     }
200 
201     /**
202      * Gets the file of the artifact to attach.
203      *
204      * @return The file of the artifact to attach.
205      */
206     protected abstract File getArtifactFile();
207 
208     /**
209      * Gets the classifier of the artifact to attach.
210      *
211      * @return The classifier of the artifact to attach.
212      */
213     protected abstract String getArtifactClassifier();
214 
215     /**
216      * Gets the type of the artifact to attach.
217      *
218      * @return The type of the artifact to attach.
219      */
220     protected abstract String getArtifactType();
221 
222     /**
223      * Gets the execution strategy of the instance.
224      *
225      * @return The execution strategy of the instance.
226      *
227      * @since 1.1
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 }