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.fromConfiguration;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.util.List;
25  
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.plugin.MojoFailureException;
30  import org.apache.maven.plugins.annotations.LifecyclePhase;
31  import org.apache.maven.plugins.annotations.Mojo;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.apache.maven.plugins.dependency.utils.UnpackUtil;
34  import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
35  import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
36  import org.apache.maven.plugins.dependency.utils.markers.MarkerHandler;
37  import org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
38  import org.apache.maven.project.MavenProject;
39  import org.codehaus.plexus.components.io.filemappers.FileMapper;
40  import org.eclipse.aether.RepositorySystem;
41  import org.sonatype.plexus.build.incremental.BuildContext;
42  
43  /**
44   * Goal that retrieves a list of artifacts from the repository and unpacks them in a defined location.
45   *
46   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
47   * @since 1.0
48   */
49  @Mojo(name = "unpack", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true)
50  public class UnpackMojo extends AbstractFromConfigurationMojo {
51  
52      private final UnpackUtil unpackUtil;
53  
54      /**
55       * Directory to store flag files after unpack.
56       */
57      @Parameter(
58              property = "markersDirectory",
59              defaultValue = "${project.build.directory}/dependency-maven-plugin-markers")
60      private File markersDirectory;
61  
62      /**
63       * A comma separated list of file patterns to include when unpacking the artifact. i.e.
64       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
65       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
66       *
67       * @since 2.0-alpha-5
68       */
69      @Parameter(property = "mdep.unpack.includes")
70      private String includes;
71  
72      /**
73       * A comma separated list of file patterns to exclude when unpacking the artifact. i.e. **\/*.xml,**\/*.properties
74       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
75       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
76       *
77       * @since 2.0-alpha-5
78       */
79      @Parameter(property = "mdep.unpack.excludes")
80      private String excludes;
81  
82      /**
83       * Ignore to set file permissions when unpacking a dependency.
84       *
85       * @since 2.7
86       */
87      @Parameter(property = "dependency.ignorePermissions", defaultValue = "false")
88      private boolean ignorePermissions;
89  
90      /**
91       * {@link FileMapper} to be used for rewriting each target path, or {@code null} if no rewriting shall happen.
92       *
93       * @since 3.1.2
94       */
95      @Parameter(property = "mdep.unpack.filemappers")
96      private FileMapper[] fileMappers;
97  
98      /**
99       * The artifact to unpack from command line. A string of the form
100      * <code>groupId:artifactId:version[:packaging[:classifier]]</code>. Use {@link #artifactItems} within the POM
101      * configuration.
102      */
103     @SuppressWarnings("unused") // marker-field, setArtifact(String) does the magic
104     @Parameter(property = "artifact")
105     private String artifact;
106 
107     @Inject
108     public UnpackMojo(
109             MavenSession session,
110             BuildContext buildContext,
111             MavenProject project,
112             ArtifactHandlerManager artifactHandlerManager,
113             UnpackUtil unpackUtil,
114             RepositorySystem repositorySystem) {
115         super(session, buildContext, project, artifactHandlerManager, repositorySystem);
116         this.unpackUtil = unpackUtil;
117     }
118 
119     /**
120      * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
121      * unpackArtifact.
122      *
123      * @throws MojoExecutionException with a message if an error occurs
124      * @see ArtifactItem
125      * @see #getArtifactItems
126      * @see #unpackArtifact(ArtifactItem)
127      */
128     @Override
129     protected void doExecute() throws MojoExecutionException, MojoFailureException {
130         if (isSkip()) {
131             return;
132         }
133 
134         verifyRequirements();
135 
136         List<ArtifactItem> processedItems = getProcessedArtifactItems(false);
137         for (ArtifactItem artifactItem : processedItems) {
138             if (artifactItem.isNeedsProcessing()) {
139                 unpackArtifact(artifactItem);
140             } else {
141                 this.getLog().info(artifactItem.getArtifact().getFile().getName() + " already unpacked.");
142             }
143         }
144     }
145 
146     /**
147      * This method gets the Artifact object and calls DependencyUtil.unpackFile.
148      *
149      * @param artifactItem containing the information about the Artifact to unpack
150      * @throws MojoExecutionException with a message if an error occurs
151      * @see #getArtifact
152      */
153     private void unpackArtifact(ArtifactItem artifactItem) throws MojoExecutionException {
154         MarkerHandler handler = new UnpackFileMarkerHandler(artifactItem, this.markersDirectory);
155 
156         unpackUtil.unpack(
157                 artifactItem.getArtifact().getFile(),
158                 artifactItem.getType(),
159                 artifactItem.getOutputDirectory(),
160                 artifactItem.getIncludes(),
161                 artifactItem.getExcludes(),
162                 artifactItem.getEncoding(),
163                 ignorePermissions,
164                 artifactItem.getFileMappers(),
165                 getLog());
166         handler.setMarker();
167     }
168 
169     @Override
170     ArtifactItemFilter getMarkedArtifactFilter(ArtifactItem item) {
171         MarkerHandler handler = new UnpackFileMarkerHandler(item, this.markersDirectory);
172 
173         return new MarkerFileFilter(
174                 this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(), handler);
175     }
176 
177     /**
178      * @param removeVersion removeVersion
179      * @return list of {@link ArtifactItem}
180      * @throws MojoExecutionException in case of an error
181      */
182     protected List<ArtifactItem> getProcessedArtifactItems(boolean removeVersion) throws MojoExecutionException {
183         List<ArtifactItem> items =
184                 super.getProcessedArtifactItems(new ProcessArtifactItemsRequest(removeVersion, false, false, false));
185         for (ArtifactItem artifactItem : items) {
186             if (artifactItem.getIncludes().isEmpty()) {
187                 artifactItem.setIncludes(getIncludes());
188             }
189             if (artifactItem.getExcludes().isEmpty()) {
190                 artifactItem.setExcludes(getExcludes());
191             }
192         }
193         return items;
194     }
195 
196     /**
197      * @return returns the markersDirectory
198      */
199     public File getMarkersDirectory() {
200         return this.markersDirectory;
201     }
202 
203     /**
204      * @param theMarkersDirectory the markersDirectory to set
205      */
206     public void setMarkersDirectory(File theMarkersDirectory) {
207         this.markersDirectory = theMarkersDirectory;
208     }
209 
210     /**
211      * @return returns a comma separated list of excluded items
212      */
213     public String getExcludes() {
214         return this.excludes;
215     }
216 
217     /**
218      * @param excludes a comma separated list of items to exclude i.e. **\/*.xml, **\/*.properties
219      */
220     public void setExcludes(String excludes) {
221         this.excludes = excludes;
222     }
223 
224     /**
225      * @return returns a comma separated list of included items
226      */
227     public String getIncludes() {
228         return this.includes;
229     }
230 
231     /**
232      * @param includes a comma separated list of items to include i.e. **\/*.xml, **\/*.properties
233      */
234     public void setIncludes(String includes) {
235         this.includes = includes;
236     }
237 
238     /**
239      * @return {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall
240      *         happen
241      * @since 3.1.2
242      */
243     public FileMapper[] getFileMappers() {
244         return this.fileMappers;
245     }
246 
247     /**
248      * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no
249      * rewriting shall happen
250      * @since 3.1.2
251      */
252     public void setFileMappers(FileMapper[] fileMappers) {
253         this.fileMappers = fileMappers;
254     }
255 }