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 c0e89499 [MDEP-923] Extract copyFile method from 
AbstractDependencyMojo (#389)
c0e89499 is described below

commit c0e894992131df3df40fdbd8b5c71ebf6c88e9b3
Author: Slawomir Jaranowski <s.jaranow...@gmail.com>
AuthorDate: Mon May 27 18:54:27 2024 +0200

    [MDEP-923] Extract copyFile method from AbstractDependencyMojo (#389)
---
 .../plugins/dependency/AbstractDependencyMojo.java | 37 -----------
 .../dependency/fromConfiguration/CopyMojo.java     | 14 ++++-
 .../fromDependencies/CopyDependenciesMojo.java     | 20 +++++-
 .../resolvers/ResolveDependenciesMojo.java         |  8 +++
 .../dependency/resolvers/ResolvePluginsMojo.java   |  8 +++
 .../maven/plugins/dependency/utils/CopyUtil.java   | 73 ++++++++++++++++++++++
 .../dependency/AbstractDependencyMojoTestCase.java |  6 +-
 .../fromDependencies/TestCopyDependenciesMojo.java |  4 +-
 8 files changed, 124 insertions(+), 46 deletions(-)

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 c57bc99d..11482499 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java
@@ -18,8 +18,6 @@
  */
 package org.apache.maven.plugins.dependency;
 
-import java.io.File;
-import java.io.IOException;
 import java.util.List;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -33,7 +31,6 @@ 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.util.FileUtils;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
@@ -94,14 +91,6 @@ public abstract class AbstractDependencyMojo extends 
AbstractMojo {
     @Parameter(property = "silent", defaultValue = "false")
     private boolean silent;
 
-    /**
-     * Output absolute filename for resolved artifacts
-     *
-     * @since 2.0
-     */
-    @Parameter(property = "outputAbsoluteArtifactFilename", defaultValue = 
"false")
-    protected boolean outputAbsoluteArtifactFilename;
-
     /**
      * Skip plugin execution completely.
      *
@@ -131,32 +120,6 @@ public abstract class AbstractDependencyMojo extends 
AbstractMojo {
      */
     protected abstract void doExecute() throws MojoExecutionException, 
MojoFailureException;
 
-    /**
-     * Does the actual copy of the file and logging.
-     *
-     * @param artifact represents the file to copy.
-     * @param destFile file name of destination file.
-     * @throws MojoExecutionException with a message if an error occurs.
-     */
-    protected void copyFile(File artifact, File destFile) throws 
MojoExecutionException {
-        try {
-            getLog().info("Copying "
-                    + (this.outputAbsoluteArtifactFilename ? 
artifact.getAbsolutePath() : artifact.getName()) + " to "
-                    + destFile);
-
-            if (artifact.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, "
-                        + "copy should be executed after packaging: see 
MDEP-187.");
-            }
-
-            FileUtils.copyFile(artifact, destFile);
-            buildContext.refresh(destFile);
-        } catch (IOException e) {
-            throw new MojoExecutionException("Error copying artifact from " + 
artifact + " to " + destFile, e);
-        }
-    }
-
     /**
      * @return Returns a new ProjectBuildingRequest populated from the current 
session and the current project remote
      *         repositories, used to resolve artifacts.
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
index 9cf85e21..b4d5bcab 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
@@ -19,13 +19,16 @@
 package org.apache.maven.plugins.dependency.fromConfiguration;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.List;
 
 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.CopyUtil;
 import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
 import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
 
@@ -38,6 +41,8 @@ import 
org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
 @Mojo(name = "copy", defaultPhase = LifecyclePhase.PROCESS_SOURCES, 
requiresProject = false, threadSafe = true)
 public class CopyMojo extends AbstractFromConfigurationMojo {
 
+    @Component
+    private CopyUtil copyUtil;
     /**
      * Strip artifact version during copy
      */
@@ -109,12 +114,17 @@ public class CopyMojo extends 
AbstractFromConfigurationMojo {
      *
      * @param artifactItem containing the information about the Artifact to 
copy.
      * @throws MojoExecutionException with a message if an error occurs.
-     * @see #copyFile(File, File)
+     * @see CopyUtil#copyFile(File, File)
      */
     protected void copyArtifact(ArtifactItem artifactItem) throws 
MojoExecutionException {
         File destFile = new File(artifactItem.getOutputDirectory(), 
artifactItem.getDestFileName());
 
-        copyFile(artifactItem.getArtifact().getFile(), destFile);
+        try {
+            copyUtil.copyFile(artifactItem.getArtifact().getFile(), destFile);
+        } catch (IOException e) {
+            throw new MojoExecutionException(
+                    "Failed copy " + artifactItem.getArtifact().getFile() + " 
to " + destFile, e);
+        }
     }
 
     @Override
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java
index ebf766e5..d92f5674 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java
@@ -19,6 +19,7 @@
 package org.apache.maven.plugins.dependency.fromDependencies;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
@@ -31,6 +32,7 @@ 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.CopyUtil;
 import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
@@ -63,6 +65,9 @@ public class CopyDependenciesMojo extends 
AbstractFromDependenciesMojo {
     @Parameter(property = "mdep.copyPom", defaultValue = "false")
     protected boolean copyPom = true;
 
+    @Component
+    private CopyUtil copyUtil;
+
     /**
      *
      */
@@ -202,7 +207,7 @@ public class CopyDependenciesMojo extends 
AbstractFromDependenciesMojo {
      * @param theUseBaseVersion specifies if the baseVersion of the artifact 
should be used instead of the version.
      * @param removeClassifier specifies if the classifier should be removed 
from the file name when copying.
      * @throws MojoExecutionException with a message if an error occurs.
-     * @see #copyFile(File, File)
+     * @see CopyUtil#copyFile(File, File)
      * @see DependencyUtil#getFormattedOutputDirectory(boolean, boolean, 
boolean, boolean, boolean, boolean, File, Artifact)
      */
     protected void copyArtifact(
@@ -227,7 +232,11 @@ public class CopyDependenciesMojo extends 
AbstractFromDependenciesMojo {
                 artifact);
         File destFile = new File(destDir, destFileName);
 
-        copyFile(artifact.getFile(), destFile);
+        try {
+            copyUtil.copyFile(artifact.getFile(), destFile);
+        } catch (IOException e) {
+            throw new MojoExecutionException("Failed copy " + 
artifact.getFile() + " to " + destFile, e);
+        }
     }
 
     /**
@@ -267,7 +276,12 @@ public class CopyDependenciesMojo extends 
AbstractFromDependenciesMojo {
                         DependencyUtil.getFormattedFileName(
                                 pomArtifact, removeVersion, prependGroupId, 
useBaseVersion, removeClassifier));
                 if (!pomDestFile.exists()) {
-                    copyFile(pomArtifact.getFile(), pomDestFile);
+                    try {
+                        copyUtil.copyFile(pomArtifact.getFile(), pomDestFile);
+                    } catch (IOException e) {
+                        throw new MojoExecutionException(
+                                "Failed copy " + pomArtifact.getFile() + " to 
" + pomDestFile, e);
+                    }
                 }
             }
         }
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
index 98e0d521..6f49a476 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
@@ -70,6 +70,14 @@ public class ResolveDependenciesMojo extends 
AbstractResolveMojo {
     @Parameter(property = "mdep.outputScope", defaultValue = "true")
     protected boolean outputScope;
 
+    /**
+     * Output absolute filename for resolved artifacts
+     *
+     * @since 2.0
+     */
+    @Parameter(property = "outputAbsoluteArtifactFilename", defaultValue = 
"false")
+    private boolean outputAbsoluteArtifactFilename;
+
     /**
      * Only used to store results for integration test validation
      */
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
index bb2d7c57..69994d96 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
@@ -49,6 +49,14 @@ public class ResolvePluginsMojo extends AbstractResolveMojo {
     @Parameter(property = "outputEncoding", defaultValue = 
"${project.reporting.outputEncoding}")
     private String outputEncoding;
 
+    /**
+     * Output absolute filename for resolved artifacts
+     *
+     * @since 2.0
+     */
+    @Parameter(property = "outputAbsoluteArtifactFilename", defaultValue = 
"false")
+    private boolean outputAbsoluteArtifactFilename;
+
     /**
      * Main entry into mojo. Gets the list of dependencies and iterates 
through displaying the resolved version.
      *
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/utils/CopyUtil.java 
b/src/main/java/org/apache/maven/plugins/dependency/utils/CopyUtil.java
new file mode 100644
index 00000000..8078081e
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/dependency/utils/CopyUtil.java
@@ -0,0 +1,73 @@
+/*
+ * 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 java.io.IOException;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.codehaus.plexus.util.FileUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonatype.plexus.build.incremental.BuildContext;
+
+/**
+ * Provide a copyFile method in one place.
+ *
+ * @since 3.7.0
+ */
+@Named
+@Singleton
+public class CopyUtil {
+
+    private final Logger logger = LoggerFactory.getLogger(CopyUtil.class);
+
+    private final BuildContext buildContext;
+
+    @Inject
+    public CopyUtil(BuildContext buildContext) {
+        this.buildContext = buildContext;
+    }
+
+    /**
+     * Does the actual copy of the file and logging.
+     *
+     * @param source represents the file to copy.
+     * @param destination file name of destination file.
+     * @throws IOException with a message if an error occurs.
+     *
+     * @since 3.7.0
+     */
+    public void copyFile(File source, File destination) throws IOException, 
MojoExecutionException {
+        logger.info("Copying {} to {}", source, destination);
+
+        if (source.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, "
+                    + "copy should be executed after packaging: see 
MDEP-187.");
+        }
+
+        FileUtils.copyFile(source, destination);
+        buildContext.refresh(destination);
+    }
+}
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java
 
b/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java
index 0a4d55cc..5fb0180e 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java
@@ -26,12 +26,14 @@ import org.apache.maven.plugin.LegacySupport;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import 
org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
+import org.apache.maven.plugins.dependency.utils.CopyUtil;
 import 
org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 import org.eclipse.aether.DefaultRepositorySystemSession;
 import org.eclipse.aether.RepositorySystem;
 import org.eclipse.aether.RepositorySystemSession;
 import org.eclipse.aether.repository.LocalRepository;
 import org.eclipse.aether.repository.LocalRepositoryManager;
+import org.sonatype.plexus.build.incremental.DefaultBuildContext;
 
 public abstract class AbstractDependencyMojoTestCase extends 
AbstractMojoTestCase {
 
@@ -67,8 +69,8 @@ public abstract class AbstractDependencyMojoTestCase extends 
AbstractMojoTestCas
         }
     }
 
-    protected void copyFile(AbstractDependencyMojo mojo, File artifact, File 
destFile) throws MojoExecutionException {
-        mojo.copyFile(artifact, destFile);
+    protected void copyFile(File artifact, File destFile) throws 
MojoExecutionException, IOException {
+        new CopyUtil(new DefaultBuildContext()).copyFile(artifact, destFile);
     }
 
     protected void installLocalRepository(LegacySupport legacySupport) throws 
ComponentLookupException {
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java
index f83743a0..c2f7c694 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java
@@ -80,14 +80,14 @@ public class TestCopyDependenciesMojo extends 
AbstractDependencyMojoTestCase {
         assertFalse(handle.isMarkerSet());
     }
 
-    public void testCopyFile() throws MojoExecutionException, IOException {
+    public void testCopyFile() throws Exception {
         File src = File.createTempFile("copy", null);
 
         File dest = new File(mojo.outputDirectory, "toMe.jar");
 
         assertFalse(dest.exists());
 
-        copyFile(mojo, src, dest);
+        copyFile(src, dest);
         assertTrue(dest.exists());
     }
 

Reply via email to