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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
29  import org.apache.maven.plugin.dependency.utils.filters.ArtifactItemFilter;
30  import org.apache.maven.plugin.dependency.utils.filters.MarkerFileFilter;
31  import org.apache.maven.plugin.dependency.utils.markers.MarkerHandler;
32  import org.apache.maven.plugin.dependency.utils.markers.UnpackFileMarkerHandler;
33  import org.apache.maven.plugin.logging.Log;
34  import org.codehaus.plexus.archiver.manager.ArchiverManager;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  /**
38   * Goal that retrieves a list of artifacts from the repository and unpacks them
39   * in a defined location.
40   * 
41   * @since 1.0
42   * @goal unpack
43   * @phase process-sources
44   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
45   * @version $Id: UnpackMojo.java 728546 2008-12-21 22:56:51Z bentmann $
46   */
47  public final class UnpackMojo
48      extends AbstractFromConfigurationMojo
49  {
50  
51      /**
52       * Directory to store flag files after unpack
53       * 
54       * @parameter expression="${project.build.directory}/dependency-maven-plugin-markers"
55       */
56      private File markersDirectory;
57      
58      /**
59       * A comma separated list of file patterns to include when unpacking the
60       * artifact.  i.e.  **\/*.xml,**\/*.properties NOTE: Excludes patterns override the includes. (component code = return isIncluded( name ) AND !isExcluded( name );)
61       *  @since 2.0-alpha-5
62       * @parameter expression="${mdep.unpack.includes}"
63       */
64      private String includes;
65  
66      /**
67       * A comma separated list of file patterns to exclude when unpacking the
68       * artifact.  i.e.  **\/*.xml,**\/*.properties NOTE: Excludes patterns override the includes. (component code = return isIncluded( name ) AND !isExcluded( name );)
69       * @since 2.0-alpha-5
70       * @parameter expression="${mdep.unpack.excludes}"
71       */
72      private String excludes;
73  
74      /**
75       * Main entry into mojo. This method gets the ArtifactItems and iterates
76       * through each one passing it to unpackArtifact.
77       * 
78       * @throws MojoExecutionException
79       *             with a message if an error occurs.
80       * 
81       * @see ArtifactItem
82       * @see #getArtifactItems
83       * @see #unpackArtifact(ArtifactItem)
84       */
85      public void execute()
86          throws MojoExecutionException
87      {
88          ArrayList processedItems = getProcessedArtifactItems( false );
89          Iterator iter = processedItems.iterator();
90          while ( iter.hasNext() )
91          {
92              ArtifactItem artifactItem = (ArtifactItem) iter.next();
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
108      *            containing the information about the Artifact to unpack.
109      * 
110      * @throws MojoExecutionException
111      *             with a message if an error occurs.
112      * 
113      * @see #getArtifact
114      * @see DependencyUtil#unpackFile(Artifact, File, File, ArchiverManager,
115      *      Log)
116      */
117     private void unpackArtifact( ArtifactItem artifactItem )
118         throws MojoExecutionException
119     {
120         MarkerHandler handler = new UnpackFileMarkerHandler( artifactItem, this.markersDirectory );
121         
122         unpack( artifactItem.getArtifact().getFile(), artifactItem.getOutputDirectory(), artifactItem.getIncludes(), artifactItem.getExcludes() );
123         handler.setMarker();
124 
125     }
126 
127     ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
128     {
129         MarkerHandler handler = new UnpackFileMarkerHandler( item, this.markersDirectory );
130 
131         return new MarkerFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(),
132                                      this.isOverWriteIfNewer(), handler );
133     }
134     
135     protected ArrayList getProcessedArtifactItems(boolean removeVersion)
136     	throws MojoExecutionException 
137     {
138     	ArrayList items = super.getProcessedArtifactItems( removeVersion );
139     	Iterator iter = items.iterator();
140         while ( iter.hasNext() )
141         {
142             ArtifactItem artifactItem = (ArtifactItem) iter.next();
143             if ( StringUtils.isEmpty(artifactItem.getIncludes()) )
144             {
145                 artifactItem.setIncludes( getIncludes() );
146             }
147             if ( StringUtils.isEmpty(artifactItem.getExcludes()) )
148             {
149                 artifactItem.setExcludes( getExcludes() );
150             }
151         }
152     	return items;
153     }
154 
155     /**
156      * @return Returns the markersDirectory.
157      */
158     public File getMarkersDirectory()
159     {
160         return this.markersDirectory;
161     }
162 
163     /**
164      * @param theMarkersDirectory
165      *            The markersDirectory to set.
166      */
167     public void setMarkersDirectory( File theMarkersDirectory )
168     {
169         this.markersDirectory = theMarkersDirectory;
170     }
171     
172    
173     /**
174      * @return Returns a comma separated list of excluded items
175      */
176     public String getExcludes ()
177     {
178         return this.excludes;
179     }
180     
181     /**
182      * @param excludes 
183      * 			A comma separated list of items to exclude 
184      * 			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
201      * 			A comma separated list of items to include 
202      * 			i.e.  **\/*.xml, **\/*.properties
203      */
204     public void setIncludes ( String includes )
205     {
206         this.includes = includes;
207     }
208 }