This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 3fb3669a [MDEP-859] Code refactor - UnpackUtil
3fb3669a is described below

commit 3fb3669a30d3e56b67095018d8017da654032e4f
Author: Slawomir Jaranowski <s.jaranow...@gmail.com>
AuthorDate: Thu Apr 20 00:08:25 2023 +0200

    [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.
---
 pom.xml                                            |   4 +
 src/it/projects/unpack-custom-ear/verify.groovy    |  44 ++---
 .../plugins/dependency/AbstractDependencyMojo.java | 208 ---------------------
 .../dependency/fromConfiguration/UnpackMojo.java   |  21 ++-
 .../fromDependencies/UnpackDependenciesMojo.java   |  26 ++-
 .../maven/plugins/dependency/utils/UnpackUtil.java | 188 +++++++++++++++++++
 .../TestIncludeExcludeUnpackMojo.java              |   5 -
 .../fromConfiguration/TestUnpackMojo.java          |   3 +-
 .../TestUnpackDependenciesMojo.java                |   3 +-
 .../TestUnpackDependenciesMojo2.java               |   3 +-
 10 files changed, 262 insertions(+), 243 deletions(-)

diff --git a/pom.xml b/pom.xml
index 6013603a..6970be4f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -389,6 +389,10 @@ under the License.
           </systemPropertyVariables>
         </configuration>
       </plugin>
+      <plugin>
+        <groupId>org.eclipse.sisu</groupId>
+        <artifactId>sisu-maven-plugin</artifactId>
+      </plugin>
     </plugins>
   </build>
 
diff --git a/src/it/projects/unpack-custom-ear/verify.groovy 
b/src/it/projects/unpack-custom-ear/verify.groovy
index 36eb8352..9b92ae56 100644
--- a/src/it/projects/unpack-custom-ear/verify.groovy
+++ b/src/it/projects/unpack-custom-ear/verify.groovy
@@ -1,22 +1,22 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-def buildLog = new File( basedir, 'build.log' )
-assert buildLog.exists()
-assert buildLog.length() != 0
-assert buildLog.text.contains( "[DEBUG] Found unArchiver by type: " )
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+def buildLog = new File( basedir, 'build.log' )
+assert buildLog.exists()
+assert buildLog.length() != 0
+assert buildLog.text.contains( "[DEBUG] Found unArchiver: 
org.apache.maven.archiver.LogUnArchiver by type: custom-ear" )
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java 
b/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java
index d1140fca..68cc286c 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java
@@ -20,11 +20,8 @@ package org.apache.maven.plugins.dependency;
 
 import java.io.File;
 import java.io.IOException;
-import java.lang.reflect.Field;
 import java.util.List;
 
-import org.apache.commons.lang3.StringUtils;
-import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.AbstractMojo;
@@ -36,26 +33,13 @@ import 
org.apache.maven.plugins.dependency.utils.DependencySilentLog;
 import org.apache.maven.project.DefaultProjectBuildingRequest;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.ProjectBuildingRequest;
-import org.codehaus.plexus.archiver.ArchiverException;
-import org.codehaus.plexus.archiver.UnArchiver;
-import org.codehaus.plexus.archiver.manager.ArchiverManager;
-import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
-import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
-import org.codehaus.plexus.components.io.filemappers.FileMapper;
-import 
org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.ReflectionUtils;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
  * @author <a href="mailto:bri...@apache.org";>Brian Fox</a>
  */
 public abstract class AbstractDependencyMojo extends AbstractMojo {
-    /**
-     * To look up Archiver/UnArchiver implementations
-     */
-    @Component
-    private ArchiverManager archiverManager;
 
     /**
      * For IDE build support
@@ -72,14 +56,6 @@ public abstract class AbstractDependencyMojo extends 
AbstractMojo {
     @Parameter(defaultValue = "false")
     private boolean skipDuringIncrementalBuild;
 
-    /**
-     * ignore to set file permissions when unpacking a dependency
-     *
-     * @since 2.7
-     */
-    @Parameter(property = "dependency.ignorePermissions", defaultValue = 
"false")
-    private boolean ignorePermissions;
-
     /**
      * POM
      */
@@ -155,13 +131,6 @@ public abstract class AbstractDependencyMojo extends 
AbstractMojo {
      */
     protected abstract void doExecute() throws MojoExecutionException, 
MojoFailureException;
 
-    /**
-     * @return Returns the archiverManager.
-     */
-    public ArchiverManager getArchiverManager() {
-        return this.archiverManager;
-    }
-
     /**
      * Does the actual copy of the file and logging.
      *
@@ -188,146 +157,6 @@ public abstract class AbstractDependencyMojo extends 
AbstractMojo {
         }
     }
 
-    /**
-     * @param artifact {@link Artifact}
-     * @param location The location.
-     * @param encoding The encoding.
-     * @param fileMappers {@link FileMapper}s to be used for rewriting each 
target path, or {@code null} if no rewriting
-     *                    shall happen.
-     * @throws MojoExecutionException in case of an error.
-     */
-    protected void unpack(Artifact artifact, File location, String encoding, 
FileMapper[] fileMappers)
-            throws MojoExecutionException {
-        unpack(artifact, location, null, null, encoding, fileMappers);
-    }
-
-    /**
-     * Unpacks the archive file.
-     *
-     * @param artifact File to be unpacked.
-     * @param location Location where to put the unpacked files.
-     * @param includes Comma separated list of file patterns to include i.e. 
<code>**&#47;.xml,
-     *                 **&#47;*.properties</code>
-     * @param excludes Comma separated list of file patterns to exclude i.e. 
<code>**&#47;*.xml,
-     *                 **&#47;*.properties</code>
-     * @param encoding Encoding of artifact. Set {@code null} for default 
encoding.
-     * @param fileMappers {@link FileMapper}s to be used for rewriting each 
target path, or {@code null} if no rewriting
-     *                    shall happen.
-     * @throws MojoExecutionException In case of errors.
-     */
-    protected void unpack(
-            Artifact artifact,
-            File location,
-            String includes,
-            String excludes,
-            String encoding,
-            FileMapper[] fileMappers)
-            throws MojoExecutionException {
-        unpack(artifact, artifact.getType(), location, includes, excludes, 
encoding, fileMappers);
-    }
-
-    /**
-     * @param artifact {@link Artifact}
-     * @param type The type.
-     * @param location The location.
-     * @param includes includes list.
-     * @param excludes excludes list.
-     * @param encoding the encoding.
-     * @param fileMappers {@link FileMapper}s to be used for rewriting each 
target path, or {@code null} if no rewriting
-     *                    shall happen.
-     * @throws MojoExecutionException in case of an error.
-     */
-    protected void unpack(
-            Artifact artifact,
-            String type,
-            File location,
-            String includes,
-            String excludes,
-            String encoding,
-            FileMapper[] fileMappers)
-            throws MojoExecutionException {
-        File file = artifact.getFile();
-        try {
-            logUnpack(file, location, includes, excludes);
-
-            location.mkdirs();
-            if (!location.exists()) {
-                throw new MojoExecutionException(
-                        "Location to write unpacked files to could not be 
created: " + location);
-            }
-
-            if (file.isDirectory()) {
-                // usual case is a future jar packaging, but there are special 
cases: classifier and other packaging
-                throw new MojoExecutionException("Artifact has not been 
packaged yet. When used on reactor artifact, "
-                        + "unpack should be executed after packaging: see 
MDEP-98.");
-            }
-
-            UnArchiver unArchiver;
-
-            try {
-                unArchiver = archiverManager.getUnArchiver(type);
-                getLog().debug("Found unArchiver by type: " + unArchiver);
-            } catch (NoSuchArchiverException e) {
-                unArchiver = archiverManager.getUnArchiver(file);
-                getLog().debug("Found unArchiver by extension: " + unArchiver);
-            }
-
-            if (encoding != null && unArchiver instanceof ZipUnArchiver) {
-                ((ZipUnArchiver) unArchiver).setEncoding(encoding);
-                getLog().info("Unpacks '" + type + "' with encoding '" + 
encoding + "'.");
-            }
-
-            unArchiver.setIgnorePermissions(ignorePermissions);
-
-            unArchiver.setSourceFile(file);
-
-            unArchiver.setDestDirectory(location);
-
-            if (StringUtils.isNotEmpty(excludes) || 
StringUtils.isNotEmpty(includes)) {
-                // Create the selectors that will filter
-                // based on include/exclude parameters
-                // MDEP-47
-                IncludeExcludeFileSelector[] selectors =
-                        new IncludeExcludeFileSelector[] {new 
IncludeExcludeFileSelector()};
-
-                if (StringUtils.isNotEmpty(excludes)) {
-                    selectors[0].setExcludes(excludes.split(","));
-                }
-
-                if (StringUtils.isNotEmpty(includes)) {
-                    selectors[0].setIncludes(includes.split(","));
-                }
-
-                unArchiver.setFileSelectors(selectors);
-            }
-            if (this.silent) {
-                silenceUnarchiver(unArchiver);
-            }
-
-            unArchiver.setFileMappers(fileMappers);
-
-            unArchiver.extract();
-        } catch (NoSuchArchiverException e) {
-            throw new MojoExecutionException("Unknown archiver type", e);
-        } catch (ArchiverException e) {
-            throw new MojoExecutionException("Error unpacking file: " + file + 
" to: " + location, e);
-        }
-        buildContext.refresh(location);
-    }
-
-    private void silenceUnarchiver(UnArchiver unArchiver) {
-        // dangerous but handle any errors. It's the only way to silence the 
unArchiver.
-        try {
-            Field field = 
ReflectionUtils.getFieldByNameIncludingSuperclasses("logger", 
unArchiver.getClass());
-
-            field.setAccessible(true);
-
-            field.set(unArchiver, this.getLog());
-        } catch (Exception e) {
-            // was a nice try. Don't bother logging because the log is silent.
-        }
-    }
-
     /**
      * @return Returns a new ProjectBuildingRequest populated from the current 
session and the current project remote
      *         repositories, used to resolve artifacts.
@@ -359,13 +188,6 @@ public abstract class AbstractDependencyMojo extends 
AbstractMojo {
         return this.project;
     }
 
-    /**
-     * @param archiverManager The archiverManager to set.
-     */
-    public void setArchiverManager(ArchiverManager archiverManager) {
-        this.archiverManager = archiverManager;
-    }
-
     /**
      * @return {@link #skip}
      */
@@ -399,34 +221,4 @@ public abstract class AbstractDependencyMojo extends 
AbstractMojo {
             setLog(new DependencySilentLog());
         }
     }
-
-    private void logUnpack(File file, File location, String includes, String 
excludes) {
-        if (!getLog().isInfoEnabled()) {
-            return;
-        }
-
-        StringBuilder msg = new StringBuilder();
-        msg.append("Unpacking ");
-        msg.append(file);
-        msg.append(" to ");
-        msg.append(location);
-
-        if (includes != null && excludes != null) {
-            msg.append(" with includes \"");
-            msg.append(includes);
-            msg.append("\" and excludes \"");
-            msg.append(excludes);
-            msg.append("\"");
-        } else if (includes != null) {
-            msg.append(" with includes \"");
-            msg.append(includes);
-            msg.append("\"");
-        } else if (excludes != null) {
-            msg.append(" with excludes \"");
-            msg.append(excludes);
-            msg.append("\"");
-        }
-
-        getLog().info(msg.toString());
-    }
 }
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
index af97beac..f6019940 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
@@ -24,9 +24,11 @@ import java.util.List;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.dependency.utils.UnpackUtil;
 import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
 import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
 import org.apache.maven.plugins.dependency.utils.markers.MarkerHandler;
@@ -42,6 +44,9 @@ import 
org.codehaus.plexus.components.io.filemappers.FileMapper;
 @Mojo(name = "unpack", defaultPhase = LifecyclePhase.PROCESS_SOURCES, 
requiresProject = false, threadSafe = true)
 public class UnpackMojo extends AbstractFromConfigurationMojo {
 
+    @Component
+    UnpackUtil unpackUtil;
+
     /**
      * Directory to store flag files after unpack
      */
@@ -68,6 +73,14 @@ public class UnpackMojo extends 
AbstractFromConfigurationMojo {
     @Parameter(property = "mdep.unpack.excludes")
     private String excludes;
 
+    /**
+     * ignore to set file permissions when unpacking a dependency
+     *
+     * @since 2.7
+     */
+    @Parameter(property = "dependency.ignorePermissions", defaultValue = 
"false")
+    private boolean ignorePermissions;
+
     /**
      * {@link FileMapper} to be used for rewriting each target path, or {@code 
null} if no rewriting shall happen.
      *
@@ -122,14 +135,16 @@ public class UnpackMojo extends 
AbstractFromConfigurationMojo {
     private void unpackArtifact(ArtifactItem artifactItem) throws 
MojoExecutionException {
         MarkerHandler handler = new UnpackFileMarkerHandler(artifactItem, 
this.markersDirectory);
 
-        unpack(
-                artifactItem.getArtifact(),
+        unpackUtil.unpack(
+                artifactItem.getArtifact().getFile(),
                 artifactItem.getType(),
                 artifactItem.getOutputDirectory(),
                 artifactItem.getIncludes(),
                 artifactItem.getExcludes(),
                 artifactItem.getEncoding(),
-                artifactItem.getFileMappers());
+                ignorePermissions,
+                artifactItem.getFileMappers(),
+                getLog());
         handler.setMarker();
     }
 
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
index ebcfa059..ba36a233 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
@@ -22,12 +22,14 @@ import java.io.File;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
+import org.apache.maven.plugins.dependency.utils.UnpackUtil;
 import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
 import 
org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
@@ -47,6 +49,10 @@ import 
org.codehaus.plexus.components.io.filemappers.FileMapper;
         threadSafe = true)
 // CHECKSTYLE_ON: LineLength
 public class UnpackDependenciesMojo extends AbstractFromDependenciesMojo {
+
+    @Component
+    UnpackUtil unpackUtil;
+
     /**
      * A comma separated list of file patterns to include when unpacking the 
artifact. i.e.
      * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns 
override the includes. (component code =
@@ -67,6 +73,14 @@ public class UnpackDependenciesMojo extends 
AbstractFromDependenciesMojo {
     @Parameter(property = "mdep.unpack.excludes")
     private String excludes;
 
+    /**
+     * ignore to set file permissions when unpacking a dependency
+     *
+     * @since 2.7
+     */
+    @Parameter(property = "dependency.ignorePermissions", defaultValue = 
"false")
+    private boolean ignorePermissions;
+
     /**
      * Encoding of artifacts.
      *
@@ -89,7 +103,6 @@ public class UnpackDependenciesMojo extends 
AbstractFromDependenciesMojo {
      *
      * @throws MojoExecutionException with a message if an error occurs.
      * @see #getDependencySets(boolean)
-     * @see #unpack(Artifact, File, String, FileMapper[])
      */
     @Override
     protected void doExecute() throws MojoExecutionException {
@@ -105,7 +118,16 @@ public class UnpackDependenciesMojo extends 
AbstractFromDependenciesMojo {
                     stripType,
                     outputDirectory,
                     artifact);
-            unpack(artifact, destDir, getIncludes(), getExcludes(), 
getEncoding(), getFileMappers());
+            unpackUtil.unpack(
+                    artifact.getFile(),
+                    artifact.getType(),
+                    destDir,
+                    getIncludes(),
+                    getExcludes(),
+                    getEncoding(),
+                    ignorePermissions,
+                    getFileMappers(),
+                    getLog());
             DefaultFileMarkerHandler handler = new 
DefaultFileMarkerHandler(artifact, this.markersDirectory);
             handler.setMarker();
         }
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/utils/UnpackUtil.java 
b/src/main/java/org/apache/maven/plugins/dependency/utils/UnpackUtil.java
new file mode 100644
index 00000000..e28bafcd
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/dependency/utils/UnpackUtil.java
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugins.dependency.utils;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import java.io.File;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
+import org.codehaus.plexus.archiver.ArchiverException;
+import org.codehaus.plexus.archiver.UnArchiver;
+import org.codehaus.plexus.archiver.manager.ArchiverManager;
+import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
+import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
+import org.codehaus.plexus.components.io.filemappers.FileMapper;
+import 
org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
+import org.sonatype.plexus.build.incremental.BuildContext;
+
+/**
+ * Provide unpack method in one place for {@link 
org.apache.maven.plugins.dependency.fromConfiguration.UnpackMojo}
+ * and {@link 
org.apache.maven.plugins.dependency.fromDependencies.UnpackDependenciesMojo}
+ */
+@Named
+@Singleton
+public class UnpackUtil {
+
+    /**
+     * To look up Archiver/UnArchiver implementations
+     */
+    private final ArchiverManager archiverManager;
+
+    /**
+     * For IDE build support
+     */
+    private final BuildContext buildContext;
+
+    /**
+     * Default constructor.
+     *
+     * @param archiverManager an archiver {@link ArchiverManager} to use
+     * @param buildContext    a build context
+     */
+    @Inject
+    public UnpackUtil(ArchiverManager archiverManager, BuildContext 
buildContext) {
+        this.archiverManager = archiverManager;
+        this.buildContext = buildContext;
+    }
+
+    /**
+     * @param file              file to unpack
+     * @param type              file / artifact type
+     * @param location          The location.
+     * @param includes          includes list.
+     * @param excludes          excludes list.
+     * @param encoding          the encoding.
+     * @param ignorePermissions ignore permissions
+     * @param fileMappers       {@link FileMapper}s to be used for rewriting 
each target path, or {@code null} if no
+     *                          rewriting
+     *                          shall happen.
+     * @param logger            a Mojo logger
+     * @throws MojoExecutionException in case of an error.
+     */
+    public void unpack(
+            File file,
+            String type,
+            File location,
+            String includes,
+            String excludes,
+            String encoding,
+            boolean ignorePermissions,
+            FileMapper[] fileMappers,
+            Log logger)
+            throws MojoExecutionException {
+        try {
+            logUnpack(logger, file, location, includes, excludes);
+
+            location.mkdirs();
+            if (!location.exists()) {
+                throw new MojoExecutionException(
+                        "Location to write unpacked files to could not be 
created: " + location);
+            }
+
+            if (file.isDirectory()) {
+                // usual case is a future jar packaging, but there are special 
cases: classifier and other packaging
+                throw new MojoExecutionException("Artifact has not been 
packaged yet. When used on reactor artifact, "
+                        + "unpack should be executed after packaging: see 
MDEP-98.");
+            }
+
+            UnArchiver unArchiver;
+
+            try {
+                unArchiver = archiverManager.getUnArchiver(type);
+                logger.debug("Found unArchiver: " + 
unArchiver.getClass().getName() + " by type: " + type);
+            } catch (NoSuchArchiverException e) {
+                unArchiver = archiverManager.getUnArchiver(file);
+                logger.debug("Found unArchiver: " + 
unArchiver.getClass().getName() + " by file extension: " + file);
+            }
+
+            if (encoding != null && unArchiver instanceof ZipUnArchiver) {
+                ((ZipUnArchiver) unArchiver).setEncoding(encoding);
+                logger.info("Unpacks '" + type + "' with encoding '" + 
encoding + "'.");
+            }
+
+            unArchiver.setIgnorePermissions(ignorePermissions);
+
+            unArchiver.setSourceFile(file);
+
+            unArchiver.setDestDirectory(location);
+
+            if (StringUtils.isNotEmpty(excludes) || 
StringUtils.isNotEmpty(includes)) {
+                // Create the selectors that will filter
+                // based on include/exclude parameters
+                // MDEP-47
+                IncludeExcludeFileSelector[] selectors =
+                        new IncludeExcludeFileSelector[] {new 
IncludeExcludeFileSelector()};
+
+                if (StringUtils.isNotEmpty(excludes)) {
+                    selectors[0].setExcludes(excludes.split(","));
+                }
+
+                if (StringUtils.isNotEmpty(includes)) {
+                    selectors[0].setIncludes(includes.split(","));
+                }
+
+                unArchiver.setFileSelectors(selectors);
+            }
+
+            unArchiver.setFileMappers(fileMappers);
+
+            unArchiver.extract();
+        } catch (NoSuchArchiverException e) {
+            throw new MojoExecutionException("Unknown archiver type", e);
+        } catch (ArchiverException e) {
+            throw new MojoExecutionException("Error unpacking file: " + file + 
" to: " + location, e);
+        }
+        buildContext.refresh(location);
+    }
+
+    private void logUnpack(Log logger, File file, File location, String 
includes, String excludes) {
+        if (logger.isInfoEnabled()) {
+            return;
+        }
+
+        StringBuilder msg = new StringBuilder();
+        msg.append("Unpacking ");
+        msg.append(file);
+        msg.append(" to ");
+        msg.append(location);
+
+        if (includes != null && excludes != null) {
+            msg.append(" with includes \"");
+            msg.append(includes);
+            msg.append("\" and excludes \"");
+            msg.append(excludes);
+            msg.append("\"");
+        } else if (includes != null) {
+            msg.append(" with includes \"");
+            msg.append(includes);
+            msg.append("\"");
+        } else if (excludes != null) {
+            msg.append(" with excludes \"");
+            msg.append(excludes);
+            msg.append("\"");
+        }
+
+        logger.info(msg.toString());
+    }
+}
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestIncludeExcludeUnpackMojo.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestIncludeExcludeUnpackMojo.java
index 0483dddf..31de6967 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestIncludeExcludeUnpackMojo.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestIncludeExcludeUnpackMojo.java
@@ -29,7 +29,6 @@ import org.apache.maven.plugin.LegacySupport;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
 import 
org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
-import org.codehaus.plexus.archiver.manager.ArchiverManager;
 
 public class TestIncludeExcludeUnpackMojo extends 
AbstractDependencyMojoTestCase {
     private final String PACKED_FILE = "test.zip";
@@ -51,8 +50,6 @@ public class TestIncludeExcludeUnpackMojo extends 
AbstractDependencyMojoTestCase
         mojo.setOutputDirectory(new File(this.testDir, "outputDirectory"));
         // mojo.silent = true;
 
-        // it needs to get the archivermanager
-        // stubFactory.setUnpackableFile( mojo.getArchiverManager() );
         // i'm using one file repeatedly to archive so I can test the name
         // programmatically.
         stubFactory.setSrcFile(new File(getBasedir() + File.separatorChar + 
PACKED_FILE_PATH));
@@ -63,8 +60,6 @@ public class TestIncludeExcludeUnpackMojo extends 
AbstractDependencyMojoTestCase
         assertNotNull(mojo);
         assertNotNull(mojo.getProject());
 
-        mojo.setArchiverManager(lookup(ArchiverManager.class));
-
         mojo.setMarkersDirectory(new File(this.testDir, "markers"));
         mojo.setArtifactItems(list);
 
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackMojo.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackMojo.java
index 8ca9ad8c..2adc2860 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackMojo.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackMojo.java
@@ -36,6 +36,7 @@ import 
org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
 import 
org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
 import 
org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.archiver.manager.ArchiverManager;
 
 public class TestUnpackMojo extends AbstractDependencyMojoTestCase {
 
@@ -55,7 +56,7 @@ public class TestUnpackMojo extends 
AbstractDependencyMojoTestCase {
         // MavenProject project = mojo.getProject();
         // init classifier things
         // it needs to get the archivermanager
-        stubFactory.setUnpackableFile(mojo.getArchiverManager());
+        stubFactory.setUnpackableFile(lookup(ArchiverManager.class));
         // i'm using one file repeatedly to archive so I can test the name
         // programmatically.
         stubFactory.setSrcFile(new File(
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesMojo.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesMojo.java
index 5780feeb..6e994830 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesMojo.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesMojo.java
@@ -38,6 +38,7 @@ import 
org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFacto
 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import 
org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.archiver.manager.ArchiverManager;
 
 public class TestUnpackDependenciesMojo extends AbstractDependencyMojoTestCase 
{
 
@@ -57,7 +58,7 @@ public class TestUnpackDependenciesMojo extends 
AbstractDependencyMojoTestCase {
         // mojo.silent = true;
 
         // it needs to get the archivermanager
-        stubFactory.setUnpackableFile(mojo.getArchiverManager());
+        stubFactory.setUnpackableFile(lookup(ArchiverManager.class));
         // i'm using one file repeatedly to archive so I can test the name
         // programmatically.
         stubFactory.setSrcFile(new File(getBasedir() + File.separatorChar + 
UNPACKABLE_FILE_PATH));
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesMojo2.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesMojo2.java
index 0811e792..515a87c2 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesMojo2.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesMojo2.java
@@ -30,6 +30,7 @@ import 
org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
 import 
org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.archiver.manager.ArchiverManager;
 
 public class TestUnpackDependenciesMojo2 extends 
AbstractDependencyMojoTestCase {
 
@@ -49,7 +50,7 @@ public class TestUnpackDependenciesMojo2 extends 
AbstractDependencyMojoTestCase
         // mojo.silent = true;
 
         // it needs to get the archivermanager
-        stubFactory.setUnpackableFile(mojo.getArchiverManager());
+        stubFactory.setUnpackableFile(lookup(ArchiverManager.class));
         // i'm using one file repeatedly to archive so I can test the name
         // programmatically.
         stubFactory.setSrcFile(new File(getBasedir() + File.separatorChar + 
UNPACKABLE_FILE_PATH));

Reply via email to