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