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