Skip to content

Commit

Permalink
[MDEP-923] Extract copyFile method from AbstractDependencyMojo
Browse files Browse the repository at this point in the history
this method is only needed in CopyMojo and CopyDependenciesMojo
so it is not needed in AbstractDependencyMojo
  • Loading branch information
slawekjaranowski committed May 10, 2024
1 parent 4311011 commit 7b3f845
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +31,6 @@
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;

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -131,32 +120,6 @@ public final void execute() throws MojoExecutionException, MojoFailureException
*/
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
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;

Expand All @@ -38,6 +41,8 @@
@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
*/
Expand Down Expand Up @@ -109,12 +114,12 @@ protected void doExecute() throws MojoExecutionException, MojoFailureException {
*
* @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(Log, File, File)
*/
protected void copyArtifact(ArtifactItem artifactItem) throws MojoExecutionException {
File destFile = new File(artifactItem.getOutputDirectory(), artifactItem.getDestFileName());

copyFile(artifactItem.getArtifact().getFile(), destFile);
copyUtil.copyFile(getLog(), artifactItem.getArtifact().getFile(), destFile);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
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.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;
Expand Down Expand Up @@ -63,6 +65,9 @@ public class CopyDependenciesMojo extends AbstractFromDependenciesMojo {
@Parameter(property = "mdep.copyPom", defaultValue = "false")
protected boolean copyPom = true;

@Component
private CopyUtil copyUtil;

/**
*
*/
Expand Down Expand Up @@ -202,7 +207,7 @@ protected void copyArtifact(
* @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(Log, File, File)
* @see DependencyUtil#getFormattedOutputDirectory(boolean, boolean, boolean, boolean, boolean, boolean, File, Artifact)
*/
protected void copyArtifact(
Expand All @@ -227,7 +232,7 @@ protected void copyArtifact(
artifact);
File destFile = new File(destDir, destFileName);

copyFile(artifact.getFile(), destFile);
copyUtil.copyFile(getLog(), artifact.getFile(), destFile);
}

/**
Expand Down Expand Up @@ -267,7 +272,7 @@ public void copyPoms(File destDir, Set<Artifact> artifacts, boolean removeVersio
DependencyUtil.getFormattedFileName(
pomArtifact, removeVersion, prependGroupId, useBaseVersion, removeClassifier));
if (!pomDestFile.exists()) {
copyFile(pomArtifact.getFile(), pomDestFile);
copyUtil.copyFile(getLog(), pomArtifact.getFile(), pomDestFile);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.FileUtils;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
* Provide a copyFile method in one place.
*/
@Named
@Singleton
public class CopyUtil {

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 MojoExecutionException with a message if an error occurs.
*/
public void copyFile(Log log, File source, File destination) throws MojoExecutionException {
try {
log.info("Copying " + source + " to " + 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);
} catch (IOException e) {
throw new MojoExecutionException("Error copying artifact from " + source + " to " + destination, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
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 {

Expand Down Expand Up @@ -68,7 +70,7 @@ protected void tearDown() {
}

protected void copyFile(AbstractDependencyMojo mojo, File artifact, File destFile) throws MojoExecutionException {
mojo.copyFile(artifact, destFile);
new CopyUtil(new DefaultBuildContext()).copyFile(mojo.getLog(), artifact, destFile);
}

protected void installLocalRepository(LegacySupport legacySupport) throws ComponentLookupException {
Expand Down

0 comments on commit 7b3f845

Please sign in to comment.