View Javadoc

1   package org.apache.maven.plugin.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.plugin.dependency.utils.filters.ArtifactItemFilter;
25  import org.apache.maven.plugin.dependency.utils.filters.MarkerFileFilter;
26  import org.apache.maven.plugin.dependency.utils.markers.MarkerHandler;
27  import org.apache.maven.plugin.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 1400739 2012-10-21 23:05:22Z hboutemy $
41   * @since 1.0
42   */
43  @Mojo( name = "unpack", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true )
44  public final 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. **\/*.xml,**\/*.properties
56       * NOTE: Excludes patterns override the includes. (component code = return isIncluded( name ) AND !isExcluded( name
57       * );)
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       * NOTE: Excludes patterns override the includes. (component code = return isIncluded( name ) AND !isExcluded( name
67       * );)
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 commandLine.
76       * Use {@link #artifactItems} within the pom-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      public void execute()
92          throws MojoExecutionException, MojoFailureException
93      {
94          if ( isSkip() )
95          {
96              return;
97          }
98          
99          verifyRequirements();
100 
101         List<ArtifactItem> processedItems = getProcessedArtifactItems( false );
102         for ( ArtifactItem artifactItem : processedItems )
103         {
104             if ( artifactItem.isNeedsProcessing() )
105             {
106                 unpackArtifact( artifactItem );
107             }
108             else
109             {
110                 this.getLog().info( artifactItem.getArtifact().getFile().getName() + " already unpacked." );
111             }
112         }
113     }
114 
115     /**
116      * This method gets the Artifact object and calls DependencyUtil.unpackFile.
117      *
118      * @param artifactItem containing the information about the Artifact to unpack.
119      * @throws MojoExecutionException with a message if an error occurs.
120      * @see #getArtifact
121      * @see DependencyUtil#unpackFile(Artifact, File, File, ArchiverManager, Log)
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.getOutputDirectory(), artifactItem.getIncludes(),
129                 artifactItem.getExcludes() );
130         handler.setMarker();
131     }
132 
133     ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
134     {
135         MarkerHandler handler = new UnpackFileMarkerHandler( item, this.markersDirectory );
136 
137         return new MarkerFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
138                                      handler );
139     }
140 
141     protected List<ArtifactItem> getProcessedArtifactItems( boolean removeVersion )
142         throws MojoExecutionException
143     {
144         List<ArtifactItem> items = super.getProcessedArtifactItems( removeVersion );
145         for ( ArtifactItem artifactItem : items )
146         {
147             if ( StringUtils.isEmpty( artifactItem.getIncludes() ) )
148             {
149                 artifactItem.setIncludes( getIncludes() );
150             }
151             if ( StringUtils.isEmpty( artifactItem.getExcludes() ) )
152             {
153                 artifactItem.setExcludes( getExcludes() );
154             }
155         }
156         return items;
157     }
158 
159     /**
160      * @return Returns the markersDirectory.
161      */
162     public File getMarkersDirectory()
163     {
164         return this.markersDirectory;
165     }
166 
167     /**
168      * @param theMarkersDirectory The markersDirectory to set.
169      */
170     public void setMarkersDirectory( File theMarkersDirectory )
171     {
172         this.markersDirectory = theMarkersDirectory;
173     }
174 
175     /**
176      * @return Returns a comma separated list of excluded items
177      */
178     public String getExcludes()
179     {
180         return this.excludes;
181     }
182 
183     /**
184      * @param excludes A comma separated list of items to exclude i.e. **\/*.xml, **\/*.properties
185      */
186     public void setExcludes( String excludes )
187     {
188         this.excludes = excludes;
189     }
190 
191     /**
192      * @return Returns a comma separated list of included items
193      */
194     public String getIncludes()
195     {
196         return this.includes;
197     }
198 
199     /**
200      * @param includes A comma separated list of items to include i.e. **\/*.xml, **\/*.properties
201      */
202     public void setIncludes( String includes )
203     {
204         this.includes = includes;
205     }
206 }