Skip to content

Commit

Permalink
[MDEP-838] "Artifact has not been packaged yet" error message is not …
Browse files Browse the repository at this point in the history
…very helpful

This closes #412
  • Loading branch information
michael-o committed Jun 8, 2024
1 parent 9902456 commit f090b5e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
Expand Down Expand Up @@ -108,16 +109,18 @@ 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 CopyUtil#copyFile(File, File)
* @see CopyUtil#copyArtifactFile(Artifact, File)
*/
protected void copyArtifact(ArtifactItem artifactItem) throws MojoExecutionException {
File destFile = new File(artifactItem.getOutputDirectory(), artifactItem.getDestFileName());

try {
copyUtil.copyFile(artifactItem.getArtifact().getFile(), destFile);
copyUtil.copyArtifactFile(artifactItem.getArtifact(), destFile);
} catch (IOException e) {
throw new MojoExecutionException(
"Failed copy " + artifactItem.getArtifact().getFile() + " to " + destFile, e);
"Failed to copy artifact '" + artifactItem.getArtifact() + "' ("
+ artifactItem.getArtifact().getFile() + ") to " + destFile,
e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,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 CopyUtil#copyFile(File, File)
* @see CopyUtil#copyArtifactFile(Artifact, File)
* @see DependencyUtil#getFormattedOutputDirectory(boolean, boolean, boolean, boolean, boolean, boolean, File, Artifact)
*/
protected void copyArtifact(
Expand All @@ -227,9 +227,10 @@ protected void copyArtifact(
File destFile = new File(destDir, destFileName);

try {
copyUtil.copyFile(artifact.getFile(), destFile);
copyUtil.copyArtifactFile(artifact, destFile);
} catch (IOException e) {
throw new MojoExecutionException("Failed copy " + artifact.getFile() + " to " + destFile, e);
throw new MojoExecutionException(
"Failed to copy artifact '" + artifact + "' (" + artifact.getFile() + ") to " + destFile, e);
}
}

Expand Down Expand Up @@ -271,10 +272,12 @@ public void copyPoms(File destDir, Set<Artifact> artifacts, boolean removeVersio
pomArtifact, removeVersion, prependGroupId, useBaseVersion, removeClassifier));
if (!pomDestFile.exists()) {
try {
copyUtil.copyFile(pomArtifact.getFile(), pomDestFile);
copyUtil.copyArtifactFile(pomArtifact, pomDestFile);
} catch (IOException e) {
throw new MojoExecutionException(
"Failed copy " + pomArtifact.getFile() + " to " + pomDestFile, e);
"Failed to copy artifact '" + pomArtifact + "' (" + pomArtifact.getFile() + ") to "
+ pomDestFile,
e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.File;
import java.io.IOException;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.FileUtils;
import org.slf4j.Logger;
Expand All @@ -50,20 +51,23 @@ public CopyUtil(BuildContext buildContext) {
}

/**
* Does the actual copy of the file and logging.
* Does the actual copy of the artifact (file) and logging.
*
* @param source represents the file to copy.
* @param sourceArtifact represents the artifact (file) to copy.
* @param destination file name of destination file.
* @throws IOException with a message if an error occurs.
* @throws IOException if copy has failed
* @throws MojoExecutionException if artifact file is a directory (which has not been packaged yet)
*
* @since 3.7.0
*/
public void copyFile(File source, File destination) throws IOException, MojoExecutionException {
logger.info("Copying {} to {}", source, destination);
public void copyArtifactFile(Artifact sourceArtifact, File destination) throws IOException, MojoExecutionException {
logger.info("Copying artifact '{}' ({}) to {}", sourceArtifact, sourceArtifact.getFile(), destination);

File source = sourceArtifact.getFile();
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, "
throw new MojoExecutionException("Artifact '" + sourceArtifact
+ "' has not been packaged yet (is a directory). When used on reactor artifact, "
+ "copy should be executed after packaging: see MDEP-187.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
Expand Down Expand Up @@ -63,14 +64,14 @@ protected void tearDown() {
FileUtils.deleteDirectory(testDir);
} catch (IOException e) {
e.printStackTrace();
fail("Trying to remove directory:" + testDir + System.lineSeparator() + e);
fail("Trying to remove directory: " + testDir + System.lineSeparator() + e);
}
assertFalse(testDir.exists());
}
}

protected void copyFile(File artifact, File destFile) throws MojoExecutionException, IOException {
new CopyUtil(new DefaultBuildContext()).copyFile(artifact, destFile);
protected void copyArtifactFile(Artifact sourceArtifact, File destFile) throws MojoExecutionException, IOException {
new CopyUtil(new DefaultBuildContext()).copyArtifactFile(sourceArtifact, destFile);
}

protected void installLocalRepository(LegacySupport legacySupport) throws ComponentLookupException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.stubs.ArtifactStub;
import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
Expand Down Expand Up @@ -80,14 +81,19 @@ public void assertNoMarkerFile(Artifact artifact) throws MojoExecutionException
assertFalse(handle.isMarkerSet());
}

public void testCopyFile() throws Exception {
public void testCopyArtifactFile() throws Exception {
final Artifact srcArtifact = new ArtifactStub();
srcArtifact.setGroupId("org.apache.maven.plugins");
srcArtifact.setArtifactId("maven-dependency-plugin-dummy");
srcArtifact.setVersion("1.0");
File src = File.createTempFile("copy", null);
srcArtifact.setFile(src);

File dest = new File(mojo.outputDirectory, "toMe.jar");

assertFalse(dest.exists());

copyFile(src, dest);
copyArtifactFile(srcArtifact, dest);
assertTrue(dest.exists());
}

Expand Down

0 comments on commit f090b5e

Please sign in to comment.