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.dependency.utils.filters.ArtifactItemFilter;
24  import org.apache.maven.plugin.dependency.utils.filters.MarkerFileFilter;
25  import org.apache.maven.plugin.dependency.utils.markers.MarkerHandler;
26  import org.apache.maven.plugin.dependency.utils.markers.UnpackFileMarkerHandler;
27  import org.apache.maven.plugins.annotations.LifecyclePhase;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  import java.io.File;
33  import java.util.List;
34  
35  /**
36   * Goal that retrieves a list of artifacts from the repository and unpacks them in a defined location.
37   *
38   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39   * @version $Id: UnpackMojo.java 1357251 2012-07-04 13:28:33Z olamy $
40   * @since 1.0
41   */
42  @Mojo( name = "unpack", defaultPhase = LifecyclePhase.PROCESS_SOURCES )
43  public final 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. **\/*.xml,**\/*.properties
55       * NOTE: Excludes patterns override the includes. (component code = return isIncluded( name ) AND !isExcluded( name
56       * );)
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       * NOTE: Excludes patterns override the includes. (component code = return isIncluded( name ) AND !isExcluded( name
66       * );)
67       *
68       * @since 2.0-alpha-5
69       */
70      @Parameter( property = "mdep.unpack.excludes" )
71      private String excludes;
72  
73      /**
74       * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
75       * unpackArtifact.
76       *
77       * @throws MojoExecutionException with a message if an error occurs.
78       * @see ArtifactItem
79       * @see #getArtifactItems
80       * @see #unpackArtifact(ArtifactItem)
81       */
82      public void execute()
83          throws MojoExecutionException
84      {
85          if ( isSkip() )
86          {
87              return;
88          }
89  
90          List<ArtifactItem> processedItems = getProcessedArtifactItems( false );
91          for ( ArtifactItem artifactItem : processedItems )
92          {
93              if ( artifactItem.isNeedsProcessing() )
94              {
95                  unpackArtifact( artifactItem );
96              }
97              else
98              {
99                  this.getLog().info( artifactItem.getArtifact().getFile().getName() + " already unpacked." );
100             }
101         }
102     }
103 
104     /**
105      * This method gets the Artifact object and calls DependencyUtil.unpackFile.
106      *
107      * @param artifactItem containing the information about the Artifact to unpack.
108      * @throws MojoExecutionException with a message if an error occurs.
109      * @see #getArtifact
110      * @see DependencyUtil#unpackFile(Artifact, File, File, ArchiverManager, Log)
111      */
112     private void unpackArtifact( ArtifactItem artifactItem )
113         throws MojoExecutionException
114     {
115         MarkerHandler handler = new UnpackFileMarkerHandler( artifactItem, this.markersDirectory );
116 
117         unpack( artifactItem.getArtifact().getFile(), artifactItem.getOutputDirectory(), artifactItem.getIncludes(),
118                 artifactItem.getExcludes() );
119         handler.setMarker();
120     }
121 
122     ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
123     {
124         MarkerHandler handler = new UnpackFileMarkerHandler( item, this.markersDirectory );
125 
126         return new MarkerFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
127                                      handler );
128     }
129 
130     protected List<ArtifactItem> getProcessedArtifactItems( boolean removeVersion )
131         throws MojoExecutionException
132     {
133         List<ArtifactItem> items = super.getProcessedArtifactItems( removeVersion );
134         for ( ArtifactItem artifactItem : items )
135         {
136             if ( StringUtils.isEmpty( artifactItem.getIncludes() ) )
137             {
138                 artifactItem.setIncludes( getIncludes() );
139             }
140             if ( StringUtils.isEmpty( artifactItem.getExcludes() ) )
141             {
142                 artifactItem.setExcludes( getExcludes() );
143             }
144         }
145         return items;
146     }
147 
148     /**
149      * @return Returns the markersDirectory.
150      */
151     public File getMarkersDirectory()
152     {
153         return this.markersDirectory;
154     }
155 
156     /**
157      * @param theMarkersDirectory The markersDirectory to set.
158      */
159     public void setMarkersDirectory( File theMarkersDirectory )
160     {
161         this.markersDirectory = theMarkersDirectory;
162     }
163 
164     /**
165      * @return Returns a comma separated list of excluded items
166      */
167     public String getExcludes()
168     {
169         return this.excludes;
170     }
171 
172     /**
173      * @param excludes A comma separated list of items to exclude i.e. **\/*.xml, **\/*.properties
174      */
175     public void setExcludes( String excludes )
176     {
177         this.excludes = excludes;
178     }
179 
180     /**
181      * @return Returns a comma separated list of included items
182      */
183     public String getIncludes()
184     {
185         return this.includes;
186     }
187 
188     /**
189      * @param includes A comma separated list of items to include i.e. **\/*.xml, **\/*.properties
190      */
191     public void setIncludes( String includes )
192     {
193         this.includes = includes;
194     }
195 }