From b9c1c8bc486c7defb92171e1ea9169075b76695c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20P=C3=A9teri?= Date: Mon, 20 Nov 2023 14:27:01 +0100 Subject: [PATCH] [MDEPLOY-314] Include artifactId in DeployMojo#processProject messages Enhance messages on deployAtEnd --- https://issues.apache.org/jira/browse/MDEPLOY-314 --- .../maven/plugins/deploy/DeployMojo.java | 14 +- .../maven/plugins/deploy/DeployMojoTest.java | 141 +++++++++++++++++- 2 files changed, 148 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java index c3ea850a..79ee1d87 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java @@ -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" @@ -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"); } } diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java index 3b0df77e..5884ae11 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java @@ -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; @@ -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; @@ -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()); } } @@ -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();