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