From e9143678bac6980000483693b3b2ed2d58407920 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Wed, 24 Apr 2024 15:40:23 +0200 Subject: [PATCH] [MINSTALL-195] Include artifactId in InstallMojo#processProject messages --- src/it/no-main-artifact-1/verify.groovy | 2 +- src/it/no-main-artifact-2/verify.groovy | 2 +- .../maven/plugins/install/InstallMojo.java | 14 ++-- .../plugins/install/InstallMojoTest.java | 65 ++++++++++++++++++- 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/it/no-main-artifact-1/verify.groovy b/src/it/no-main-artifact-1/verify.groovy index 9442aa7d..7274ba5c 100644 --- a/src/it/no-main-artifact-1/verify.groovy +++ b/src/it/no-main-artifact-1/verify.groovy @@ -19,5 +19,5 @@ def buildLog = new File ( basedir, "build.log") -assert buildLog.text.contains( "The packaging plugin for this project did not assign " +assert buildLog.text.contains( "The packaging plugin for project test did not assign " + "a main file to the project but it has attachments. Change packaging to 'pom'." ) \ No newline at end of file diff --git a/src/it/no-main-artifact-2/verify.groovy b/src/it/no-main-artifact-2/verify.groovy index 9442aa7d..7274ba5c 100644 --- a/src/it/no-main-artifact-2/verify.groovy +++ b/src/it/no-main-artifact-2/verify.groovy @@ -19,5 +19,5 @@ def buildLog = new File ( basedir, "build.log") -assert buildLog.text.contains( "The packaging plugin for this project did not assign " +assert buildLog.text.contains( "The packaging plugin for project test did not assign " + "a main file to the project but it has attachments. Change packaging to 'pom'." ) \ No newline at end of file diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index a2aaa9a0..35eb1c1d 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -207,7 +207,8 @@ private void processProject(MavenProject project, InstallRequest request) throws 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" @@ -218,18 +219,19 @@ private void processProject(MavenProject project, InstallRequest request) throws } 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/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java index 79397787..8176925a 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -155,6 +155,38 @@ public void testBasicInstallWithAttachedArtifacts() throws Exception { assertEquals(13, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); } + public void testNonPomInstallWithAttachedArtifactsOnly() throws Exception { + File testPom = new File( + getBasedir(), + "target/test-classes/unit/basic-install-test-with-attached-artifacts/" + "plugin-config.xml"); + + AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); + + assertNotNull(mojo); + + MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); + updateMavenProject(project); + + setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); + setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); + setVariableValueToObject(mojo, "session", createMavenSession()); + + artifact = (InstallArtifactStub) 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-install-test did not assign a main file to the project " + + "but it has attachments. Change packaging to 'pom'.", + e.getMessage()); + } + } + public void testUpdateReleaseParamSetToTrue() throws Exception { File testPom = new File(getBasedir(), "target/test-classes/unit/configured-install-test/plugin-config.xml"); @@ -210,15 +242,44 @@ public void testInstallIfArtifactFileIsNull() throws Exception { try { mojo.execute(); - fail("Did not throw mojo execution exception"); } catch (MojoExecutionException e) { - // expected + // expected, message should include artifactId + assertEquals( + "The packaging plugin for project maven-install-test did not assign a file to the build artifact", + e.getMessage()); } assertFalse(new File(LOCAL_REPO).exists()); } + public void testInstallIfProjectFileIsNull() throws Exception { + File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); + + AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); + + assertNotNull(mojo); + + MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); + updateMavenProject(project); + + setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); + setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); + setVariableValueToObject(mojo, "session", createMavenSession()); + + project.setFile(null); + + assertNull(project.getFile()); + + try { + mojo.execute(); + fail("Did not throw mojo execution exception"); + } catch (MojoExecutionException e) { + // expected, message should include artifactId + assertEquals("The POM for project maven-install-test could not be attached", e.getMessage()); + } + } + public void testInstallIfPackagingIsPom() throws Exception { File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test-packaging-pom/" + "plugin-config.xml");