View Javadoc

1   package org.alfresco.maven.plugin.amp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.alfresco.plexus.archiver.AmpArchiver;
23  import org.apache.maven.archiver.MavenArchiver;
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.DependencyResolutionRequiredException;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.project.MavenProjectHelper;
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.archiver.jar.ManifestException;
31  import org.codehaus.plexus.archiver.zip.ZipArchiver;
32  
33  import java.io.File;
34  import java.io.IOException;
35  
36  /***
37   * Build a amp/webapp.
38   * <p>
39   * Note this is a modification of Emmanuel Venisse's (evenisse@apache.org) WAR
40   * Mojo and has been adapted to build an Alfresco AMP.
41   * @version $Id:$
42   * @goal amp
43   * @phase package
44   * @requiresDependencyResolution runtime
45   */
46  public class AmpMojo extends AbstractAmpMojo
47  {
48      /* ====================================================================== */
49      // constructors
50      /* ====================================================================== */
51  
52      /***
53       * default constructor
54       */
55      public AmpMojo()
56      {
57          this.setAmpArchiver(null);
58          this.setAmpName(null);
59          this.setOutputDirectory(null);
60      }
61      
62      /* ====================================================================== */
63      // properties
64      /* ====================================================================== */
65  
66      /***
67       * Whether this is the main artifact being built. Set to <code>false</code> if you don't want to install or
68       * deploy it to the local repository instead of the default one in an execution.
69       *
70       * @parameter expression="${primaryArtifact}" default-value="true"
71       */
72      private boolean mPrimaryArtifact;
73  
74      /***
75       * get the the internal value for the <code>ampArchiver</code> property.
76       * <p>
77       * The <code>ampArchiver</code> property 
78       * @return Returns the internal value for the ampArchiver property.
79       */
80      protected AmpArchiver getAmpArchiver()
81      {
82          return this.mAmpArchiver;
83      }
84  
85      /***
86       * set the internal value for the <code>ampArchiver</code> property
87       * @param pAmpArchiver The <code>ampArchiver</code> to set.
88       */
89      protected void setAmpArchiver(AmpArchiver pAmpArchiver)
90      {
91          this.mAmpArchiver = pAmpArchiver;
92      }
93  
94      /***
95       * get the the internal value for the <code>ampName</code> property.
96       * <p>
97       * The <code>ampName</code> property 
98       * @return Returns the internal value for the ampName property.
99       */
100     protected String getAmpName()
101     {
102         return this.mAmpName;
103     }
104 
105     /***
106      * set the internal value for the <code>ampName</code> property
107      * @param pAmpName The <code>ampName</code> to set.
108      */
109     protected void setAmpName(String pAmpName)
110     {
111         this.mAmpName = pAmpName;
112     }
113 
114     /***
115      * get the the internal value for the <code>outputDirectory</code> property.
116      * <p>
117      * The <code>outputDirectory</code> property 
118      * @return Returns the internal value for the outputDirectory property.
119      */
120     protected String getOutputDirectory()
121     {
122         return this.mOutputDirectory;
123     }
124 
125     /***
126      * set the internal value for the <code>outputDirectory</code> property
127      * @param pOutputDirectory The <code>outputDirectory</code> to set.
128      */
129     protected void setOutputDirectory(String pOutputDirectory)
130     {
131         this.mOutputDirectory = pOutputDirectory;
132     }
133 
134     /***
135      * get the the internal value for the <code>primaryArtifact</code> property.
136      * <p>
137      * The <code>primaryArtifact</code> property 
138      * @return Returns the internal value for the primaryArtifact property.
139      */
140     protected boolean isPrimaryArtifact()
141     {
142         return this.mPrimaryArtifact;
143     }
144 
145     /***
146      * set the internal value for the <code>primaryArtifact</code> property
147      * @param pPrimaryArtifact The <code>primaryArtifact</code> to set.
148      */
149     protected void setPrimaryArtifact(boolean pPrimaryArtifact)
150     {
151         this.mPrimaryArtifact = pPrimaryArtifact;
152     }
153 
154     /***
155      * get the the internal value for the <code>projectHelper</code> property.
156      * <p>
157      * The <code>projectHelper</code> property 
158      * @return Returns the internal value for the projectHelper property.
159      */
160     protected MavenProjectHelper getProjectHelper()
161     {
162         return this.mProjectHelper;
163     }
164 
165     /***
166      * set the internal value for the <code>projectHelper</code> property
167      * @param pProjectHelper The <code>projectHelper</code> to set.
168      */
169     protected void setProjectHelper(MavenProjectHelper pProjectHelper)
170     {
171         this.mProjectHelper = pProjectHelper;
172     }
173 
174     /***
175      * Overload this to produce a test-war, for example.
176      */
177     protected String getClassifier()
178     {
179         return mClassifier;
180     }
181     
182     /***
183      * set the internal value for the <code>classifier</code> property
184      * @param pClassifier The <code>classifier</code> to set.
185      */
186     protected void setClassifier(String pClassifier)
187     {
188         this.mClassifier = pClassifier;
189     }
190 
191     /* ====================================================================== */
192     // public methods
193     /* ====================================================================== */
194 
195     /***
196      * Executes the WarMojo on the current project.
197      *
198      * @throws MojoExecutionException if an error occured while building the webapp
199      */
200     public void execute()
201     throws MojoExecutionException, 
202            MojoFailureException
203     {
204 
205         File vAmpFile = AmpMojo.getAmpFile(new File( getOutputDirectory() ), getAmpName(), getClassifier());
206 
207         try
208         {
209             this.performPackaging(vAmpFile);
210         }
211         catch (Exception eAssemblyFailure)
212         {
213             /* behavior is the same for the following exceptions:
214              *    DependencyResolutionRequiredException
215              *    ManifestException
216              *    IOException
217              *    ArchiverException
218              */
219             throw new MojoExecutionException( "Error assembling AMP: " + eAssemblyFailure.getMessage(), eAssemblyFailure );
220         }
221     }
222 
223     /* ====================================================================== */
224     // protected methods
225     /* ====================================================================== */
226 
227 
228     /***
229      * composes the full file name for the AMP and gets a file handle for that file
230      * TODO: what happens when nulls are passed in
231      * TODO: what does a null response mean?
232      * @param pBaseDir  Base directory for AMP
233      * @param pFileName Final Name of AMP   
234      * @param pClassifier TODO: fill this in
235      */
236     protected static File getAmpFile( File pBasedir, String pFinalName, String pClassifier )
237     {
238         String vClassifier = pClassifier;
239 
240         if (vClassifier == null)
241         {
242             vClassifier = "";
243         }
244         else if (vClassifier.trim().length() > 0 && !vClassifier.startsWith( "-" ) )
245         {
246             vClassifier = "-" + vClassifier;
247         }
248 
249         return new File(pBasedir, pFinalName + vClassifier + ".amp" );
250     }
251 
252 
253     /***
254      * Generates the webapp according to the <tt>mode</tt> attribute.
255      *
256      * @param pAmpFile the target AMP file
257      * @throws IOException
258      * @throws ArchiverException
259      * @throws ManifestException
260      * @throws DependencyResolutionRequiredException
261      *
262      */
263     protected void performPackaging(File pAmpFile)
264     throws IOException, 
265            ArchiverException, 
266            ManifestException, 
267            DependencyResolutionRequiredException,
268            MojoExecutionException, MojoFailureException
269     {
270         getLog().info( "Packaging Alfresco AMP (" + this.getAmpName() + ")" );
271 
272         
273         this.buildExplodedAmp(this.getAmpDirectory());
274 
275         /* create and setup an archiver */
276         MavenArchiver vArchiver = new MavenArchiver();
277         vArchiver.setArchiver(this.getAmpArchiver());
278         vArchiver.setOutputFile(pAmpFile);
279 
280         /* setup amp Archiver */
281         this.getAmpArchiver().addDirectory(this.getAmpDirectory(), this.getIncludes(), this.getExcludes());
282 
283         // create archive
284         vArchiver.createArchive(this.getProject(), archive );
285 
286         String vClassifier = this.getClassifier();
287         
288         if ( vClassifier != null )
289         {
290             this.getProjectHelper().attachArtifact( this.getProject(), "amp", vClassifier, pAmpFile );
291         }
292         else
293         {
294             Artifact vArtifact = this.getProject().getArtifact();
295             
296             if ( this.isPrimaryArtifact() )
297             {
298                 vArtifact.setFile(pAmpFile);
299             }
300             else if(vArtifact.getFile() == null || vArtifact.getFile().isDirectory() )
301             {
302                 vArtifact.setFile(pAmpFile);
303             }
304         }
305     }
306 
307 
308     /* ====================================================================== */
309     // member fields
310     /* ====================================================================== */
311     
312     /***
313      * The directory for the generated AMP.
314      *
315      * @parameter expression="${project.build.directory}"
316      * @required
317      */
318     private String mOutputDirectory;
319 
320     /***
321      * The name of the generated AMP.
322      *
323      * @parameter expression="${project.build.finalName}"
324      * @required
325      */
326     private String mAmpName;
327 
328     /***
329      * Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.
330      *
331      * @parameter
332      */
333     private String mClassifier;
334 
335     /***
336      * The AMP archiver.
337      * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#amp}"
338      * @required
339      */
340     private AmpArchiver mAmpArchiver;
341 
342 
343     /***
344      * @component
345      */
346     private MavenProjectHelper mProjectHelper;
347 }
348