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