View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.fromDependencies;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.plugins.annotations.ResolutionScope;
33  import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
34  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
35  import org.apache.maven.plugins.dependency.utils.ResolverUtil;
36  import org.apache.maven.plugins.dependency.utils.UnpackUtil;
37  import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
38  import org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
39  import org.apache.maven.project.MavenProject;
40  import org.apache.maven.project.ProjectBuilder;
41  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
42  import org.codehaus.plexus.components.io.filemappers.FileMapper;
43  import org.sonatype.plexus.build.incremental.BuildContext;
44  
45  /**
46   * Goal that unpacks the project dependencies from the repository to a defined location.
47   *
48   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
49   * @since 1.0
50   */
51  // CHECKSTYLE_OFF: LineLength
52  @Mojo(
53          name = "unpack-dependencies",
54          requiresDependencyResolution = ResolutionScope.TEST,
55          defaultPhase = LifecyclePhase.PROCESS_SOURCES,
56          threadSafe = true)
57  // CHECKSTYLE_ON: LineLength
58  public class UnpackDependenciesMojo extends AbstractFromDependenciesMojo {
59  
60      /**
61       * A comma separated list of file patterns to include when unpacking the artifact. i.e.
62       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
63       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
64       *
65       * @since 2.0
66       */
67      @Parameter(property = "mdep.unpack.includes")
68      private String includes;
69  
70      /**
71       * A comma separated list of file patterns to exclude when unpacking the artifact. i.e.
72       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
73       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
74       *
75       * @since 2.0
76       */
77      @Parameter(property = "mdep.unpack.excludes")
78      private String excludes;
79  
80      /**
81       * Ignore to set file permissions when unpacking a dependency.
82       *
83       * @since 2.7
84       */
85      @Parameter(property = "dependency.ignorePermissions", defaultValue = "false")
86      private boolean ignorePermissions;
87  
88      /**
89       * Encoding of artifacts.
90       *
91       * @since 3.0
92       */
93      @Parameter(property = "mdep.unpack.encoding")
94      private String encoding;
95  
96      /**
97       * {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall happen.
98       *
99       * @since 3.1.2
100      */
101     @Parameter(property = "mdep.unpack.filemappers")
102     private FileMapper[] fileMappers;
103 
104     private final UnpackUtil unpackUtil;
105 
106     @Inject
107     public UnpackDependenciesMojo(
108             MavenSession session,
109             BuildContext buildContext,
110             MavenProject project,
111             ResolverUtil resolverUtil,
112             ProjectBuilder projectBuilder,
113             ArtifactHandlerManager artifactHandlerManager,
114             UnpackUtil unpackUtil) {
115         super(session, buildContext, project, resolverUtil, projectBuilder, artifactHandlerManager);
116         this.unpackUtil = unpackUtil;
117     }
118 
119     /**
120      * Main entry into mojo. This method gets the dependencies and iterates through each one passing it to
121      * DependencyUtil.unpackFile().
122      *
123      * @throws MojoExecutionException with a message if an error occurs
124      * @see #getDependencySets(boolean)
125      */
126     @Override
127     protected void doExecute() throws MojoExecutionException {
128         DependencyStatusSets dss = getDependencySets(this.failOnMissingClassifierArtifact);
129 
130         for (Artifact artifact : dss.getResolvedDependencies()) {
131             File destDir = DependencyUtil.getFormattedOutputDirectory(
132                     useSubDirectoryPerScope,
133                     useSubDirectoryPerType,
134                     useSubDirectoryPerArtifact,
135                     useRepositoryLayout,
136                     stripVersion,
137                     stripType,
138                     outputDirectory,
139                     artifact);
140             unpackUtil.unpack(
141                     artifact.getFile(),
142                     artifact.getType(),
143                     destDir,
144                     getIncludes(),
145                     getExcludes(),
146                     getEncoding(),
147                     ignorePermissions,
148                     getFileMappers(),
149                     getLog());
150             DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(artifact, this.markersDirectory);
151             handler.setMarker();
152         }
153 
154         for (Artifact artifact : dss.getSkippedDependencies()) {
155             getLog().info(artifact.getId() + " already exists in destination.");
156         }
157     }
158 
159     @Override
160     protected ArtifactsFilter getMarkedArtifactFilter() {
161         return new MarkerFileFilter(
162                 this.overWriteReleases,
163                 this.overWriteSnapshots,
164                 this.overWriteIfNewer,
165                 new DefaultFileMarkerHandler(this.markersDirectory));
166     }
167 
168     /**
169      * @return returns a comma separated list of excluded items
170      */
171     public String getExcludes() {
172         return DependencyUtil.cleanToBeTokenizedString(this.excludes);
173     }
174 
175     /**
176      * @param excludes a comma separated list of items to exclude i.e. <code>**\/*.xml, **\/*.properties</code>
177      */
178     public void setExcludes(String excludes) {
179         this.excludes = excludes;
180     }
181 
182     /**
183      * @return returns a comma separated list of included items
184      */
185     public String getIncludes() {
186         return DependencyUtil.cleanToBeTokenizedString(this.includes);
187     }
188 
189     /**
190      * @param includes a comma separated list of items to include i.e. <code>**\/*.xml, **\/*.properties</code>
191      */
192     public void setIncludes(String includes) {
193         this.includes = includes;
194     }
195 
196     /**
197      * @param encoding the encoding to set
198      * @since 3.0
199      */
200     public void setEncoding(String encoding) {
201         this.encoding = encoding;
202     }
203 
204     /**
205      * @return returns the encoding
206      * @since 3.0
207      */
208     public String getEncoding() {
209         return this.encoding;
210     }
211 
212     /**
213      * @return {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall
214      *         happen
215      * @since 3.1.2
216      */
217     public FileMapper[] getFileMappers() {
218         return this.fileMappers;
219     }
220 
221     /**
222      * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no
223      *                   rewriting shall happen
224      * @since 3.1.2
225      */
226     public void setFileMappers(FileMapper[] fileMappers) {
227         this.fileMappers = fileMappers;
228     }
229 }