Skip to content

Commit 3fb3669

Browse files
[MDEP-859] Code refactor - UnpackUtil
Methods for unpack artifacts are in AbstractDependencyMojo but are only used by unpack and unpack-dependencies mojos. We can create separate component and use only where is needed.
1 parent 7c75bc9 commit 3fb3669

File tree

10 files changed

+262
-243
lines changed

10 files changed

+262
-243
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ under the License.
389389
</systemPropertyVariables>
390390
</configuration>
391391
</plugin>
392+
<plugin>
393+
<groupId>org.eclipse.sisu</groupId>
394+
<artifactId>sisu-maven-plugin</artifactId>
395+
</plugin>
392396
</plugins>
393397
</build>
394398

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
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-
def buildLog = new File( basedir, 'build.log' )
20-
assert buildLog.exists()
21-
assert buildLog.length() != 0
22-
assert buildLog.text.contains( "[DEBUG] Found unArchiver by type: " )
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+
def buildLog = new File( basedir, 'build.log' )
20+
assert buildLog.exists()
21+
assert buildLog.length() != 0
22+
assert buildLog.text.contains( "[DEBUG] Found unArchiver: org.apache.maven.archiver.LogUnArchiver by type: custom-ear" )

src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java

Lines changed: 0 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23-
import java.lang.reflect.Field;
2423
import java.util.List;
2524

26-
import org.apache.commons.lang3.StringUtils;
27-
import org.apache.maven.artifact.Artifact;
2825
import org.apache.maven.artifact.repository.ArtifactRepository;
2926
import org.apache.maven.execution.MavenSession;
3027
import org.apache.maven.plugin.AbstractMojo;
@@ -36,26 +33,13 @@
3633
import org.apache.maven.project.DefaultProjectBuildingRequest;
3734
import org.apache.maven.project.MavenProject;
3835
import org.apache.maven.project.ProjectBuildingRequest;
39-
import org.codehaus.plexus.archiver.ArchiverException;
40-
import org.codehaus.plexus.archiver.UnArchiver;
41-
import org.codehaus.plexus.archiver.manager.ArchiverManager;
42-
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
43-
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
44-
import org.codehaus.plexus.components.io.filemappers.FileMapper;
45-
import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
4636
import org.codehaus.plexus.util.FileUtils;
47-
import org.codehaus.plexus.util.ReflectionUtils;
4837
import org.sonatype.plexus.build.incremental.BuildContext;
4938

5039
/**
5140
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
5241
*/
5342
public abstract class AbstractDependencyMojo extends AbstractMojo {
54-
/**
55-
* To look up Archiver/UnArchiver implementations
56-
*/
57-
@Component
58-
private ArchiverManager archiverManager;
5943

6044
/**
6145
* For IDE build support
@@ -72,14 +56,6 @@ public abstract class AbstractDependencyMojo extends AbstractMojo {
7256
@Parameter(defaultValue = "false")
7357
private boolean skipDuringIncrementalBuild;
7458

75-
/**
76-
* ignore to set file permissions when unpacking a dependency
77-
*
78-
* @since 2.7
79-
*/
80-
@Parameter(property = "dependency.ignorePermissions", defaultValue = "false")
81-
private boolean ignorePermissions;
82-
8359
/**
8460
* POM
8561
*/
@@ -155,13 +131,6 @@ public final void execute() throws MojoExecutionException, MojoFailureException
155131
*/
156132
protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
157133

158-
/**
159-
* @return Returns the archiverManager.
160-
*/
161-
public ArchiverManager getArchiverManager() {
162-
return this.archiverManager;
163-
}
164-
165134
/**
166135
* Does the actual copy of the file and logging.
167136
*
@@ -188,146 +157,6 @@ protected void copyFile(File artifact, File destFile) throws MojoExecutionExcept
188157
}
189158
}
190159

191-
/**
192-
* @param artifact {@link Artifact}
193-
* @param location The location.
194-
* @param encoding The encoding.
195-
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
196-
* shall happen.
197-
* @throws MojoExecutionException in case of an error.
198-
*/
199-
protected void unpack(Artifact artifact, File location, String encoding, FileMapper[] fileMappers)
200-
throws MojoExecutionException {
201-
unpack(artifact, location, null, null, encoding, fileMappers);
202-
}
203-
204-
/**
205-
* Unpacks the archive file.
206-
*
207-
* @param artifact File to be unpacked.
208-
* @param location Location where to put the unpacked files.
209-
* @param includes Comma separated list of file patterns to include i.e. <code>**&#47;.xml,
210-
* **&#47;*.properties</code>
211-
* @param excludes Comma separated list of file patterns to exclude i.e. <code>**&#47;*.xml,
212-
* **&#47;*.properties</code>
213-
* @param encoding Encoding of artifact. Set {@code null} for default encoding.
214-
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
215-
* shall happen.
216-
* @throws MojoExecutionException In case of errors.
217-
*/
218-
protected void unpack(
219-
Artifact artifact,
220-
File location,
221-
String includes,
222-
String excludes,
223-
String encoding,
224-
FileMapper[] fileMappers)
225-
throws MojoExecutionException {
226-
unpack(artifact, artifact.getType(), location, includes, excludes, encoding, fileMappers);
227-
}
228-
229-
/**
230-
* @param artifact {@link Artifact}
231-
* @param type The type.
232-
* @param location The location.
233-
* @param includes includes list.
234-
* @param excludes excludes list.
235-
* @param encoding the encoding.
236-
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
237-
* shall happen.
238-
* @throws MojoExecutionException in case of an error.
239-
*/
240-
protected void unpack(
241-
Artifact artifact,
242-
String type,
243-
File location,
244-
String includes,
245-
String excludes,
246-
String encoding,
247-
FileMapper[] fileMappers)
248-
throws MojoExecutionException {
249-
File file = artifact.getFile();
250-
try {
251-
logUnpack(file, location, includes, excludes);
252-
253-
location.mkdirs();
254-
if (!location.exists()) {
255-
throw new MojoExecutionException(
256-
"Location to write unpacked files to could not be created: " + location);
257-
}
258-
259-
if (file.isDirectory()) {
260-
// usual case is a future jar packaging, but there are special cases: classifier and other packaging
261-
throw new MojoExecutionException("Artifact has not been packaged yet. When used on reactor artifact, "
262-
+ "unpack should be executed after packaging: see MDEP-98.");
263-
}
264-
265-
UnArchiver unArchiver;
266-
267-
try {
268-
unArchiver = archiverManager.getUnArchiver(type);
269-
getLog().debug("Found unArchiver by type: " + unArchiver);
270-
} catch (NoSuchArchiverException e) {
271-
unArchiver = archiverManager.getUnArchiver(file);
272-
getLog().debug("Found unArchiver by extension: " + unArchiver);
273-
}
274-
275-
if (encoding != null && unArchiver instanceof ZipUnArchiver) {
276-
((ZipUnArchiver) unArchiver).setEncoding(encoding);
277-
getLog().info("Unpacks '" + type + "' with encoding '" + encoding + "'.");
278-
}
279-
280-
unArchiver.setIgnorePermissions(ignorePermissions);
281-
282-
unArchiver.setSourceFile(file);
283-
284-
unArchiver.setDestDirectory(location);
285-
286-
if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) {
287-
// Create the selectors that will filter
288-
// based on include/exclude parameters
289-
// MDEP-47
290-
IncludeExcludeFileSelector[] selectors =
291-
new IncludeExcludeFileSelector[] {new IncludeExcludeFileSelector()};
292-
293-
if (StringUtils.isNotEmpty(excludes)) {
294-
selectors[0].setExcludes(excludes.split(","));
295-
}
296-
297-
if (StringUtils.isNotEmpty(includes)) {
298-
selectors[0].setIncludes(includes.split(","));
299-
}
300-
301-
unArchiver.setFileSelectors(selectors);
302-
}
303-
if (this.silent) {
304-
silenceUnarchiver(unArchiver);
305-
}
306-
307-
unArchiver.setFileMappers(fileMappers);
308-
309-
unArchiver.extract();
310-
} catch (NoSuchArchiverException e) {
311-
throw new MojoExecutionException("Unknown archiver type", e);
312-
} catch (ArchiverException e) {
313-
throw new MojoExecutionException("Error unpacking file: " + file + " to: " + location, e);
314-
}
315-
buildContext.refresh(location);
316-
}
317-
318-
private void silenceUnarchiver(UnArchiver unArchiver) {
319-
// dangerous but handle any errors. It's the only way to silence the unArchiver.
320-
try {
321-
Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses("logger", unArchiver.getClass());
322-
323-
field.setAccessible(true);
324-
325-
field.set(unArchiver, this.getLog());
326-
} catch (Exception e) {
327-
// was a nice try. Don't bother logging because the log is silent.
328-
}
329-
}
330-
331160
/**
332161
* @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
333162
* repositories, used to resolve artifacts.
@@ -359,13 +188,6 @@ public MavenProject getProject() {
359188
return this.project;
360189
}
361190

362-
/**
363-
* @param archiverManager The archiverManager to set.
364-
*/
365-
public void setArchiverManager(ArchiverManager archiverManager) {
366-
this.archiverManager = archiverManager;
367-
}
368-
369191
/**
370192
* @return {@link #skip}
371193
*/
@@ -399,34 +221,4 @@ public void setSilent(boolean silent) {
399221
setLog(new DependencySilentLog());
400222
}
401223
}
402-
403-
private void logUnpack(File file, File location, String includes, String excludes) {
404-
if (!getLog().isInfoEnabled()) {
405-
return;
406-
}
407-
408-
StringBuilder msg = new StringBuilder();
409-
msg.append("Unpacking ");
410-
msg.append(file);
411-
msg.append(" to ");
412-
msg.append(location);
413-
414-
if (includes != null && excludes != null) {
415-
msg.append(" with includes \"");
416-
msg.append(includes);
417-
msg.append("\" and excludes \"");
418-
msg.append(excludes);
419-
msg.append("\"");
420-
} else if (includes != null) {
421-
msg.append(" with includes \"");
422-
msg.append(includes);
423-
msg.append("\"");
424-
} else if (excludes != null) {
425-
msg.append(" with excludes \"");
426-
msg.append(excludes);
427-
msg.append("\"");
428-
}
429-
430-
getLog().info(msg.toString());
431-
}
432224
}

src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import org.apache.commons.lang3.StringUtils;
2525
import org.apache.maven.plugin.MojoExecutionException;
2626
import org.apache.maven.plugin.MojoFailureException;
27+
import org.apache.maven.plugins.annotations.Component;
2728
import org.apache.maven.plugins.annotations.LifecyclePhase;
2829
import org.apache.maven.plugins.annotations.Mojo;
2930
import org.apache.maven.plugins.annotations.Parameter;
31+
import org.apache.maven.plugins.dependency.utils.UnpackUtil;
3032
import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
3133
import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
3234
import org.apache.maven.plugins.dependency.utils.markers.MarkerHandler;
@@ -42,6 +44,9 @@
4244
@Mojo(name = "unpack", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true)
4345
public class UnpackMojo extends AbstractFromConfigurationMojo {
4446

47+
@Component
48+
UnpackUtil unpackUtil;
49+
4550
/**
4651
* Directory to store flag files after unpack
4752
*/
@@ -68,6 +73,14 @@ public class UnpackMojo extends AbstractFromConfigurationMojo {
6873
@Parameter(property = "mdep.unpack.excludes")
6974
private String excludes;
7075

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+
7184
/**
7285
* {@link FileMapper} to be used for rewriting each target path, or {@code null} if no rewriting shall happen.
7386
*
@@ -122,14 +135,16 @@ protected void doExecute() throws MojoExecutionException, MojoFailureException {
122135
private void unpackArtifact(ArtifactItem artifactItem) throws MojoExecutionException {
123136
MarkerHandler handler = new UnpackFileMarkerHandler(artifactItem, this.markersDirectory);
124137

125-
unpack(
126-
artifactItem.getArtifact(),
138+
unpackUtil.unpack(
139+
artifactItem.getArtifact().getFile(),
127140
artifactItem.getType(),
128141
artifactItem.getOutputDirectory(),
129142
artifactItem.getIncludes(),
130143
artifactItem.getExcludes(),
131144
artifactItem.getEncoding(),
132-
artifactItem.getFileMappers());
145+
ignorePermissions,
146+
artifactItem.getFileMappers(),
147+
getLog());
133148
handler.setMarker();
134149
}
135150

0 commit comments

Comments
 (0)