View Javadoc
1   package org.apache.maven.plugins.dependency.fromDependencies;
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.artifact.Artifact;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
25  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
26  import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
27  import org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
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.apache.maven.plugins.annotations.ResolutionScope;
32  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
33  import org.codehaus.plexus.components.io.filemappers.FileMapper;
34  
35  import java.io.File;
36  
37  /**
38   * Goal that unpacks the project dependencies from the repository to a defined location.
39   *
40   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
41   * @since 1.0
42   */
43  //CHECKSTYLE_OFF: LineLength
44  @Mojo( name = "unpack-dependencies", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true )
45  //CHECKSTYLE_ON: LineLength
46  public class UnpackDependenciesMojo
47      extends AbstractFromDependenciesMojo
48  {
49      /**
50       * A comma separated list of file patterns to include when unpacking the artifact. i.e.
51       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
52       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
53       *
54       * @since 2.0
55       */
56      @Parameter( property = "mdep.unpack.includes" )
57      private String includes;
58  
59      /**
60       * A comma separated list of file patterns to exclude when unpacking the artifact. i.e.
61       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
62       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
63       *
64       * @since 2.0
65       */
66      @Parameter( property = "mdep.unpack.excludes" )
67      private String excludes;
68  
69      /**
70       * Encoding of artifacts.
71       * 
72       * @since 3.0
73       */
74      @Parameter( property = "mdep.unpack.encoding" )
75      private String encoding;
76  
77      /**
78       * {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall happen.
79       *
80       * @since 3.1.2
81       */
82      @Parameter( property = "mdep.unpack.filemappers" )
83      private FileMapper[] fileMappers;
84  
85      /**
86       * Main entry into mojo. This method gets the dependencies and iterates through each one passing it to
87       * DependencyUtil.unpackFile().
88       *
89       * @throws MojoExecutionException with a message if an error occurs.
90       * @see #getDependencySets(boolean)
91       * @see #unpack(Artifact, File, String, FileMapper[])
92       */
93      @Override
94      protected void doExecute()
95          throws MojoExecutionException
96      {
97          DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
98  
99          for ( Artifact artifact : dss.getResolvedDependencies() )
100         {
101             File destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
102                                                                   useSubDirectoryPerArtifact, useRepositoryLayout,
103                                                                   stripVersion, outputDirectory, artifact );
104             unpack( artifact, destDir, getIncludes(), getExcludes(), getEncoding(), getFileMappers() );
105             DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( artifact, this.markersDirectory );
106             handler.setMarker();
107         }
108 
109         for ( Artifact artifact : dss.getSkippedDependencies() )
110         {
111             getLog().info( artifact.getId() + " already exists in destination." );
112         }
113     }
114 
115     @Override
116     protected ArtifactsFilter getMarkedArtifactFilter()
117     {
118         return new MarkerFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
119                                      new DefaultFileMarkerHandler( this.markersDirectory ) );
120     }
121 
122     /**
123      * @return Returns a comma separated list of excluded items
124      */
125     public String getExcludes()
126     {
127         return DependencyUtil.cleanToBeTokenizedString( this.excludes );
128     }
129 
130     /**
131      * @param excludes A comma separated list of items to exclude i.e. <code>**\/*.xml, **\/*.properties</code>
132      */
133     public void setExcludes( String excludes )
134     {
135         this.excludes = excludes;
136     }
137 
138     /**
139      * @return Returns a comma separated list of included items
140      */
141     public String getIncludes()
142     {
143         return DependencyUtil.cleanToBeTokenizedString( this.includes );
144     }
145 
146     /**
147      * @param includes A comma separated list of items to include i.e. <code>**\/*.xml, **\/*.properties</code>
148      */
149     public void setIncludes( String includes )
150     {
151         this.includes = includes;
152     }
153 
154     /**
155      * @param encoding The encoding to set.
156      * @since 3.0
157      */
158     public void setEncoding( String encoding )
159     {
160         this.encoding = encoding;
161     }
162 
163     /**
164      * @return Returns the encoding.
165      * @since 3.0
166      */
167     public String getEncoding()
168     {
169         return this.encoding;
170     }
171 
172     /**
173      * @return {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall
174      *         happen.
175      *
176      * @since 3.1.2
177      */
178     public FileMapper[] getFileMappers()
179     {
180         return this.fileMappers;
181     }
182 
183     /**
184      * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no
185      *                   rewriting shall happen.
186      *
187      * @since 3.1.2
188      */
189     public void setFileMappers( FileMapper[] fileMappers )
190     {
191         this.fileMappers = fileMappers;
192     }
193 }