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   * @version $Id: UnpackMojo.java 1807877 2017-09-09 10:35:59Z khmarbaise $
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       * The artifact to unpack from command line. A string of the form
76       * <code>groupId:artifactId:version[:packaging[:classifier]]</code>. Use {@link #artifactItems} within the POM
77       * configuration.
78       */
79      @SuppressWarnings( "unused" ) // marker-field, setArtifact(String) does the magic
80      @Parameter( property = "artifact" )
81      private String artifact;
82  
83      /**
84       * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
85       * unpackArtifact.
86       *
87       * @throws MojoExecutionException with a message if an error occurs.
88       * @see ArtifactItem
89       * @see #getArtifactItems
90       * @see #unpackArtifact(ArtifactItem)
91       */
92      @Override
93      protected void doExecute()
94          throws MojoExecutionException, MojoFailureException
95      {
96          if ( isSkip() )
97          {
98              return;
99          }
100 
101         verifyRequirements();
102 
103         List<ArtifactItem> processedItems = getProcessedArtifactItems( false );
104         for ( ArtifactItem artifactItem : processedItems )
105         {
106             if ( artifactItem.isNeedsProcessing() )
107             {
108                 unpackArtifact( artifactItem );
109             }
110             else
111             {
112                 this.getLog().info( artifactItem.getArtifact().getFile().getName() + " already unpacked." );
113             }
114         }
115     }
116 
117     /**
118      * This method gets the Artifact object and calls DependencyUtil.unpackFile.
119      *
120      * @param artifactItem containing the information about the Artifact to unpack.
121      * @throws MojoExecutionException with a message if an error occurs.
122      * @see #getArtifact
123      */
124     private void unpackArtifact( ArtifactItem artifactItem )
125         throws MojoExecutionException
126     {
127         MarkerHandler handler = new UnpackFileMarkerHandler( artifactItem, this.markersDirectory );
128 
129         unpack( artifactItem.getArtifact(), artifactItem.getType(), artifactItem.getOutputDirectory(),
130                 artifactItem.getIncludes(), artifactItem.getExcludes(), artifactItem.getEncoding() );
131         handler.setMarker();
132     }
133 
134     @Override
135     ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
136     {
137         MarkerHandler handler = new UnpackFileMarkerHandler( item, this.markersDirectory );
138 
139         return new MarkerFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
140                                      handler );
141     }
142 
143     protected List<ArtifactItem> getProcessedArtifactItems( boolean removeVersion )
144         throws MojoExecutionException
145     {
146         List<ArtifactItem> items =
147             super.getProcessedArtifactItems( new ProcessArtifactItemsRequest( removeVersion, false, false, false ) );
148         for ( ArtifactItem artifactItem : items )
149         {
150             if ( StringUtils.isEmpty( artifactItem.getIncludes() ) )
151             {
152                 artifactItem.setIncludes( getIncludes() );
153             }
154             if ( StringUtils.isEmpty( artifactItem.getExcludes() ) )
155             {
156                 artifactItem.setExcludes( getExcludes() );
157             }
158         }
159         return items;
160     }
161 
162     /**
163      * @return Returns the markersDirectory.
164      */
165     public File getMarkersDirectory()
166     {
167         return this.markersDirectory;
168     }
169 
170     /**
171      * @param theMarkersDirectory The markersDirectory to set.
172      */
173     public void setMarkersDirectory( File theMarkersDirectory )
174     {
175         this.markersDirectory = theMarkersDirectory;
176     }
177 
178     /**
179      * @return Returns a comma separated list of excluded items
180      */
181     public String getExcludes()
182     {
183         return this.excludes;
184     }
185 
186     /**
187      * @param excludes A comma separated list of items to exclude i.e. **\/*.xml, **\/*.properties
188      */
189     public void setExcludes( String excludes )
190     {
191         this.excludes = excludes;
192     }
193 
194     /**
195      * @return Returns a comma separated list of included items
196      */
197     public String getIncludes()
198     {
199         return this.includes;
200     }
201 
202     /**
203      * @param includes A comma separated list of items to include i.e. **\/*.xml, **\/*.properties
204      */
205     public void setIncludes( String includes )
206     {
207         this.includes = includes;
208     }
209 }