View Javadoc
1   package org.apache.maven.plugins.dependency.fromConfiguration;
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.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
25  import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
26  import org.apache.maven.plugins.dependency.utils.markers.MarkerHandler;
27  import org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.codehaus.plexus.components.io.filemappers.FileMapper;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  import java.io.File;
35  import java.util.List;
36  
37  /**
38   * Goal that retrieves a list of artifacts from the repository and unpacks them in a defined location.
39   *
40   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
41   * @since 1.0
42   */
43  @Mojo( name = "unpack", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true )
44  public class UnpackMojo
45      extends AbstractFromConfigurationMojo
46  {
47  
48      /**
49       * Directory to store flag files after unpack
50       */
51      @Parameter( defaultValue = "${project.build.directory}/dependency-maven-plugin-markers" )
52      private File markersDirectory;
53  
54      /**
55       * A comma separated list of file patterns to include when unpacking the artifact. i.e.
56       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
57       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
58       *
59       * @since 2.0-alpha-5
60       */
61      @Parameter( property = "mdep.unpack.includes" )
62      private String includes;
63  
64      /**
65       * A comma separated list of file patterns to exclude when unpacking the artifact. i.e. **\/*.xml,**\/*.properties
66       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
67       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
68       *
69       * @since 2.0-alpha-5
70       */
71      @Parameter( property = "mdep.unpack.excludes" )
72      private String excludes;
73  
74      /**
75       * {@link FileMapper} to be used for rewriting each target path, or {@code null} if no rewriting shall happen.
76       *
77       * @since 3.1.2
78       */
79      @Parameter( property = "mdep.unpack.filemappers" )
80      private FileMapper[] fileMappers;
81  
82      /**
83       * The artifact to unpack from command line. A string of the form
84       * <code>groupId:artifactId:version[:packaging[:classifier]]</code>. Use {@link #artifactItems} within the POM
85       * configuration.
86       */
87      @SuppressWarnings( "unused" ) // marker-field, setArtifact(String) does the magic
88      @Parameter( property = "artifact" )
89      private String artifact;
90  
91      /**
92       * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
93       * unpackArtifact.
94       *
95       * @throws MojoExecutionException with a message if an error occurs.
96       * @see ArtifactItem
97       * @see #getArtifactItems
98       * @see #unpackArtifact(ArtifactItem)
99       */
100     @Override
101     protected void doExecute()
102         throws MojoExecutionException, MojoFailureException
103     {
104         if ( isSkip() )
105         {
106             return;
107         }
108 
109         verifyRequirements();
110 
111         List<ArtifactItem> processedItems = getProcessedArtifactItems( false );
112         for ( ArtifactItem artifactItem : processedItems )
113         {
114             if ( artifactItem.isNeedsProcessing() )
115             {
116                 unpackArtifact( artifactItem );
117             }
118             else
119             {
120                 this.getLog().info( artifactItem.getArtifact().getFile().getName() + " already unpacked." );
121             }
122         }
123     }
124 
125     /**
126      * This method gets the Artifact object and calls DependencyUtil.unpackFile.
127      *
128      * @param artifactItem containing the information about the Artifact to unpack.
129      * @throws MojoExecutionException with a message if an error occurs.
130      * @see #getArtifact
131      */
132     private void unpackArtifact( ArtifactItem artifactItem )
133         throws MojoExecutionException
134     {
135         MarkerHandler handler = new UnpackFileMarkerHandler( artifactItem, this.markersDirectory );
136 
137         unpack( artifactItem.getArtifact(), artifactItem.getType(), artifactItem.getOutputDirectory(),
138                 artifactItem.getIncludes(), artifactItem.getExcludes(), artifactItem.getEncoding(),
139                 artifactItem.getFileMappers() );
140         handler.setMarker();
141     }
142 
143     @Override
144     ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
145     {
146         MarkerHandler handler = new UnpackFileMarkerHandler( item, this.markersDirectory );
147 
148         return new MarkerFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
149                                      handler );
150     }
151 
152     /**
153      * @param removeVersion removeVersion.
154      * @return list of {@link ArtifactItem}
155      * @throws MojoExecutionException in case of an error.
156      */
157     protected List<ArtifactItem> getProcessedArtifactItems( boolean removeVersion )
158         throws MojoExecutionException
159     {
160         List<ArtifactItem> items =
161             super.getProcessedArtifactItems( new ProcessArtifactItemsRequest( removeVersion, false, false, false ) );
162         for ( ArtifactItem artifactItem : items )
163         {
164             if ( StringUtils.isEmpty( artifactItem.getIncludes() ) )
165             {
166                 artifactItem.setIncludes( getIncludes() );
167             }
168             if ( StringUtils.isEmpty( artifactItem.getExcludes() ) )
169             {
170                 artifactItem.setExcludes( getExcludes() );
171             }
172         }
173         return items;
174     }
175 
176     /**
177      * @return Returns the markersDirectory.
178      */
179     public File getMarkersDirectory()
180     {
181         return this.markersDirectory;
182     }
183 
184     /**
185      * @param theMarkersDirectory The markersDirectory to set.
186      */
187     public void setMarkersDirectory( File theMarkersDirectory )
188     {
189         this.markersDirectory = theMarkersDirectory;
190     }
191 
192     /**
193      * @return Returns a comma separated list of excluded items
194      */
195     public String getExcludes()
196     {
197         return this.excludes;
198     }
199 
200     /**
201      * @param excludes A comma separated list of items to exclude i.e. **\/*.xml, **\/*.properties
202      */
203     public void setExcludes( String excludes )
204     {
205         this.excludes = excludes;
206     }
207 
208     /**
209      * @return Returns a comma separated list of included items
210      */
211     public String getIncludes()
212     {
213         return this.includes;
214     }
215 
216     /**
217      * @param includes A comma separated list of items to include i.e. **\/*.xml, **\/*.properties
218      */
219     public void setIncludes( String includes )
220     {
221         this.includes = includes;
222     }
223 
224     /**
225      * @return {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall
226      *         happen.
227      *
228      * @since 3.1.2
229      */
230     public FileMapper[] getFileMappers()
231     {
232         return this.fileMappers;
233     }
234 
235     /**
236      * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no
237      * rewriting shall happen.
238      *
239      * @since 3.1.2
240      */
241     public void setFileMappers( FileMapper[] fileMappers )
242     {
243         this.fileMappers = fileMappers;
244     }
245 }