Skip to content

Commit

Permalink
[MDEPLOY-314] Include artifactId in DeployMojo#processProject messages
Browse files Browse the repository at this point in the history
Enhance messages on deployAtEnd

---

https://issues.apache.org/jira/browse/MDEPLOY-314
  • Loading branch information
apeteri authored and cstamas committed Apr 24, 2024
1 parent 74d331b commit b9c1c8b
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ private void processProject(final MavenProject project, DeployRequest request) t
if (isFile(pomArtifact.getFile())) {
request.addArtifact(pomArtifact);
} else {
throw new MojoExecutionException("The project POM could not be attached");
throw new MojoExecutionException(
"The POM for project " + project.getArtifactId() + " could not be attached");
}

// is not packaged, is "incomplete"
Expand All @@ -319,18 +320,19 @@ private void processProject(final MavenProject project, DeployRequest request) t
} else if (!project.getAttachedArtifacts().isEmpty()) {
if (allowIncompleteProjects) {
getLog().warn("");
getLog().warn("The packaging plugin for this project did not assign");
getLog().warn("The packaging plugin for project " + project.getArtifactId() + " did not assign");
getLog().warn("a main file to the project but it has attachments. Change packaging to 'pom'.");
getLog().warn("");
getLog().warn("Incomplete projects like this will fail in future Maven versions!");
getLog().warn("");
} else {
throw new MojoExecutionException("The packaging plugin for this project did not assign "
+ "a main file to the project but it has attachments. Change packaging to 'pom'.");
throw new MojoExecutionException("The packaging plugin for project " + project.getArtifactId()
+ " did not assign a main file to the project but it has attachments. Change packaging"
+ " to 'pom'.");
}
} else {
throw new MojoExecutionException(
"The packaging for this project did not assign a file to the build artifact");
throw new MojoExecutionException("The packaging plugin for project " + project.getArtifactId()
+ " did not assign a file to the build artifact");
}
}

Expand Down
141 changes: 140 additions & 1 deletion src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.maven.execution.MavenSession;
Expand All @@ -36,10 +37,12 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.internal.impl.DefaultLocalPathComposer;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.junit.Ignore;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;

Expand Down Expand Up @@ -470,10 +473,46 @@ public void testDeployIfArtifactFileIsNull() throws Exception {

try {
mojo.execute();
fail("Did not throw mojo execution exception");
} catch (MojoExecutionException e) {
// expected, message should include artifactId
assertEquals(
"The packaging plugin for project maven-deploy-test did not assign a file to the build artifact",
e.getMessage());
}
}

public void testDeployIfProjectFileIsNull() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml");

DeployMojo mojo = (DeployMojo) lookupMojo("deploy", testPom);

MockitoAnnotations.initMocks(this);

ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);

setVariableValueToObject(mojo, "session", session);

assertNotNull(mojo);

MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
project.setGroupId("org.apache.maven.test");
project.setArtifactId("maven-deploy-test");
project.setVersion("1.0-SNAPSHOT");

project.setFile(null);
assertNull(project.getFile());

setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));

try {
mojo.execute();
fail("Did not throw mojo execution exception");
} catch (MojoExecutionException e) {
// expected
// expected, message should include artifactId
assertEquals("The POM for project maven-deploy-test could not be attached", e.getMessage());
}
}

Expand Down Expand Up @@ -568,6 +607,106 @@ public void testDeployWithAttachedArtifacts() throws Exception {
assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
}

public void testNonPomDeployWithAttachedArtifactsOnly() throws Exception {
File testPom = new File(
getBasedir(), "target/test-classes/unit/basic-deploy-with-attached-artifacts/" + "plugin-config.xml");

mojo = (DeployMojo) lookupMojo("deploy", testPom);

MockitoAnnotations.initMocks(this);

assertNotNull(mojo);

ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);

MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
project.setGroupId("org.apache.maven.test");
project.setArtifactId("maven-deploy-test");
project.setVersion("1.0-SNAPSHOT");

setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));

artifact = (DeployArtifactStub) project.getArtifact();
artifact.setFile(null);

try {
mojo.execute();
fail("Did not throw mojo execution exception");
} catch (MojoExecutionException e) {
// expected, message should include artifactId
assertEquals(
"The packaging plugin for project maven-deploy-test did not assign a main file to the project "
+ "but it has attachments. Change packaging to 'pom'.",
e.getMessage());
}
}

@Ignore("SCP is not part of Maven3 distribution. Aether handles transport extensions.")
public void _testBasicDeployWithScpAsProtocol() throws Exception {
String originalUserHome = System.getProperty("user.home");

// FIX THE DAMN user.home BEFORE YOU DELETE IT!!!
File altHome = new File(getBasedir(), "target/ssh-user-home");
altHome.mkdirs();

System.out.println("Testing user.home value for .ssh dir: " + altHome.getCanonicalPath());

Properties props = System.getProperties();
props.setProperty("user.home", altHome.getCanonicalPath());

System.setProperties(props);

File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-scp/plugin-config.xml");

mojo = (DeployMojo) lookupMojo("deploy", testPom);

assertNotNull(mojo);

RepositorySystem repositorySystem = mock(RepositorySystem.class);

setVariableValueToObject(mojo, "repositorySystem", repositorySystem);

File file = new File(
getBasedir(),
"target/test-classes/unit/basic-deploy-scp/target/" + "deploy-test-file-1.0-SNAPSHOT.jar");

assertTrue(file.exists());

MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");

setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));

artifact = (DeployArtifactStub) project.getArtifact();

artifact.setFile(file);

String altUserHome = System.getProperty("user.home");

if (altUserHome.equals(originalUserHome)) {
// this is *very* bad!
throw new IllegalStateException(
"Setting 'user.home' system property to alternate value did NOT work. Aborting test.");
}

File sshFile = new File(altUserHome, ".ssh");

System.out.println("Testing .ssh dir: " + sshFile.getCanonicalPath());

// delete first the .ssh folder if existing before executing the mojo
if (sshFile.exists()) {
FileUtils.deleteDirectory(sshFile);
}

mojo.execute();

assertTrue(sshFile.exists());

FileUtils.deleteDirectory(sshFile);
}

public void testLegacyAltDeploymentRepositoryWithDefaultLayout() throws Exception {
DeployMojo mojo = new DeployMojo();

Expand Down

0 comments on commit b9c1c8b

Please sign in to comment.