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, String, File, String, String, String, FileMapper[])
92       * @see #unpack(Artifact, File, String, FileMapper[])
93       * @see #unpack(Artifact, File, String, String, String, FileMapper[])
94       */
95      @Override
96      protected void doExecute()
97          throws MojoExecutionException
98      {
99          DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
100 
101         for ( Artifact artifact : dss.getResolvedDependencies() )
102         {
103             File destDir;
104             destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
105                                                                   useSubDirectoryPerArtifact, useRepositoryLayout,
106                                                                   stripVersion, outputDirectory, artifact );
107             unpack( artifact, destDir, getIncludes(), getExcludes(), getEncoding(), getFileMappers() );
108             DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( artifact, this.markersDirectory );
109             handler.setMarker();
110         }
111 
112         for ( Artifact artifact : dss.getSkippedDependencies() )
113         {
114             getLog().info( artifact.getId() + " already exists in destination." );
115         }
116     }
117 
118     @Override
119     protected ArtifactsFilter getMarkedArtifactFilter()
120     {
121         return new MarkerFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
122                                      new DefaultFileMarkerHandler( this.markersDirectory ) );
123     }
124 
125     /**
126      * @return Returns a comma separated list of excluded items
127      */
128     public String getExcludes()
129     {
130         return DependencyUtil.cleanToBeTokenizedString( this.excludes );
131     }
132 
133     /**
134      * @param excludes A comma separated list of items to exclude i.e. <code>**\/*.xml, **\/*.properties</code>
135      */
136     public void setExcludes( String excludes )
137     {
138         this.excludes = excludes;
139     }
140 
141     /**
142      * @return Returns a comma separated list of included items
143      */
144     public String getIncludes()
145     {
146         return DependencyUtil.cleanToBeTokenizedString( this.includes );
147     }
148 
149     /**
150      * @param includes A comma separated list of items to include i.e. <code>**\/*.xml, **\/*.properties</code>
151      */
152     public void setIncludes( String includes )
153     {
154         this.includes = includes;
155     }
156 
157     /**
158      * @param encoding The encoding to set.
159      * @since 3.0
160      */
161     public void setEncoding( String encoding )
162     {
163         this.encoding = encoding;
164     }
165 
166     /**
167      * @return Returns the encoding.
168      * @since 3.0
169      */
170     public String getEncoding()
171     {
172         return this.encoding;
173     }
174 
175     /**
176      * @return {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall
177      *         happen.
178      *
179      * @since 3.1.2
180      */
181     public FileMapper[] getFileMappers()
182     {
183         return this.fileMappers;
184     }
185 
186     /**
187      * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no
188      *                   rewriting shall happen.
189      *
190      * @since 3.1.2
191      */
192     public void setFileMappers( FileMapper[] fileMappers )
193     {
194         this.fileMappers = fileMappers;
195     }
196 }