View Javadoc
1   package org.codehaus.mojo.appassembler;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006-2012, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
33  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
34  import org.apache.maven.plugin.AbstractMojo;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.MojoFailureException;
37  import org.apache.maven.plugins.annotations.Component;
38  import org.apache.maven.plugins.annotations.Parameter;
39  import org.apache.maven.shared.mapping.MappingUtils;
40  import org.codehaus.plexus.PlexusConstants;
41  import org.codehaus.plexus.PlexusContainer;
42  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
43  import org.codehaus.plexus.context.Context;
44  import org.codehaus.plexus.context.ContextException;
45  import org.codehaus.plexus.interpolation.InterpolationException;
46  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
47  import org.codehaus.plexus.util.FileUtils;
48  
49  /**
50   * This is intended to summarize all generic parts of the Mojos into a single class. First step of refactoring code.
51   *
52   * @author <a href="mailto:codehaus@soebes.de">Karl Heinz Marbaise</a>
53   */
54  public abstract class AbstractAppAssemblerMojo
55      extends AbstractMojo
56      implements Contextualizable
57  {
58      // -----------------------------------------------------------------------
59      // Parameters
60      // -----------------------------------------------------------------------
61  
62      /**
63       * The file name mapping to use when copying libraries to the repository. If no file mapping is set (default) the
64       * files are copied with their standard names.
65       * <p>
66       * <b>Note: </b> if you use this parameter, then the <code>useTimestampInSnapshotFileName</code> parameter will be
67       * ignored.
68       * </p>
69       *
70       * @since 1.5
71       */
72      @Parameter
73      protected String outputFileNameMapping;
74  
75      /**
76       * The layout of the generated Maven repository. Supported types - "default" (Maven2) | "legacy" (Maven1) | "flat"
77       * (flat <code>lib/</code> style). The style "legacy" is only supported if you are running under Maven 2.2.1 and
78       * before.
79       */
80      @Parameter( defaultValue = "default" )
81      protected String repositoryLayout;
82  
83      /**
84       * For those snapshots downloaded from a remote repo, replace the timestamp part with "SNAPSHOT" instead.
85       *
86       * @since 1.2.3 (create-repository), 1.3 (assemble and generate-daemons)
87       */
88      @Parameter( defaultValue = "true" )
89      protected boolean useTimestampInSnapshotFileName;
90  
91      // -----------------------------------------------------------------------
92      // Read-only parameters
93      // -----------------------------------------------------------------------
94  
95      @Parameter( defaultValue = "${localRepository}", readonly = true )
96      protected ArtifactRepository localRepository;
97  
98      @Parameter( defaultValue = "${project.artifact}", readonly = true )
99      protected Artifact projectArtifact;
100 
101     // -----------------------------------------------------------------------
102     // Components
103     // -----------------------------------------------------------------------
104 
105     @Component
106     protected ArtifactRepositoryFactory artifactRepositoryFactory;
107 
108     // ----------------------------------------------------------------------
109     // Variables
110     // ----------------------------------------------------------------------
111 
112     /**
113      * A reference to the Plexus container so that we can do our own component lookups, which was required to solve
114      * MAPPASM-96.
115      */
116     protected PlexusContainer container;
117 
118     // -----------------------------------------------------------------------
119     // Plexus Implementation
120     // -----------------------------------------------------------------------
121 
122     /**
123      * {@inheritDoc}
124      */
125     public void contextualize( Context context )
126         throws ContextException
127     {
128         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
129     }
130 
131     // -----------------------------------------------------------------------
132     // Methods
133     // -----------------------------------------------------------------------
134 
135     protected ArtifactRepositoryLayout getArtifactRepositoryLayout()
136         throws MojoFailureException
137     {
138         try
139         {
140             ArtifactRepositoryLayout artifactRepositoryLayout = null;
141             artifactRepositoryLayout =
142                 (ArtifactRepositoryLayout) container.lookup( "org.apache.maven.artifact."
143                     + "repository.layout.ArtifactRepositoryLayout", repositoryLayout );
144             if ( artifactRepositoryLayout == null )
145             {
146                 throw new MojoFailureException( "Unknown repository layout '" + repositoryLayout + "'." );
147             }
148             return artifactRepositoryLayout;
149         }
150         catch ( ComponentLookupException e )
151         {
152             throw new MojoFailureException( "Unable to lookup the repository layout component '" + repositoryLayout
153                 + "': " + e.getMessage() );
154         }
155     }
156 
157     /**
158      * Copy artifact to another repository, with an option not to use timestamp in the snapshot filename.
159      *
160      * @param artifact The artifact to install.
161      * @param artifactRepository The repository where to install.
162      * @param useTimestampInSnapshotFileName Using timestamp for SNAPSHOT's.
163      * @throws MojoExecutionException
164      */
165     protected void installArtifact( Artifact artifact, ArtifactRepository artifactRepository,
166                                     boolean useTimestampInSnapshotFileName )
167         throws MojoExecutionException
168     {
169         if ( artifact != null && artifact.getFile() != null )
170         {
171             try
172             {
173                 // Necessary for the artifact's baseVersion to be set correctly
174                 // See: http://mail-archives.apache.org/mod_mbox/maven-dev/200511.mbox/%3c437288F4.4080003@apache.org%3e
175                 artifact.isSnapshot();
176 
177                 File source = artifact.getFile();
178 
179                 String localPath = artifactRepository.pathOf( artifact );
180 
181                 File destination = new File( artifactRepository.getBasedir(), localPath );
182                 if ( !destination.getParentFile().exists() )
183                 {
184                     destination.getParentFile().mkdirs();
185                 }
186 
187                 if ( artifact.isSnapshot() )
188                 {
189                     if ( !useTimestampInSnapshotFileName )
190                     {
191                         // Don't want timestamp in the snapshot file during copy
192                         destination = new File( destination.getParentFile(), source.getName() );
193                     }
194                 }
195 
196                 if ( !source.isDirectory() )
197                 {
198                     if ( outputFileNameMapping != null )
199                     {
200                         String fileName = MappingUtils.evaluateFileNameMapping( outputFileNameMapping, artifact );
201                         destination = new File( destination.getParent(), fileName );
202                     }
203                     // Sometimes target/classes is in the artifact list and copyFile() would fail.
204                     // Need to ignore this condition
205                     FileUtils.copyFile( source, destination );
206                 }
207 
208                 getLog().info( "Installing artifact " + source.getPath() + " to " + destination );
209 
210             }
211             catch ( IOException e )
212             {
213                 throw new MojoExecutionException( "Failed to copy artifact.", e );
214             }
215             catch ( InterpolationException e )
216             {
217                 throw new MojoExecutionException( "Failed to map file name.", e );
218             }
219         }
220     }
221 
222     /**
223      * Copy artifact to another repository.
224      *
225      * @param artifact
226      * @param artifactRepository
227      * @throws MojoExecutionException
228      */
229     protected void installArtifact( Artifact artifact, ArtifactRepository artifactRepository )
230         throws MojoExecutionException
231     {
232         installArtifact( artifact, artifactRepository, true );
233     }
234 
235 }