This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch maven-deploy-plugin-4.x in repository https://gitbox.apache.org/repos/asf/maven-deploy-plugin.git
commit c780060d97cd03fde98be9219d28391d01eb3f7a Author: Guillaume Nodet <[email protected]> AuthorDate: Sat Oct 10 08:35:12 2026 +0000 Auto-detect module source hierarchy and skip main artifact at deploy When a project uses module source hierarchy (<source><module>…), the JAR plugin produces one JAR per JPMS module as attached artifacts and never assigns a file to the main artifact — the deploy plugin then fails because the main artifact path is a directory. Auto-detect this case by checking whether any enabled source root declares a module (via ProjectManager.getSourceRoots() / SourceRoot.module()), and skip the main artifact from the deploy request — deploying only the POM and the per-module JARs. The existing allowIncompleteProjects fallback is preserved unchanged for unrelated incomplete-project scenarios. This is a workaround for Maven 4.0. The proper fix is tracked in apache/maven#13396 (Packaging.producesMainArtifact() API for 4.1.0). --- .../apache/maven/plugins/deploy/DeployMojo.java | 33 ++++++++++------- .../maven/plugins/deploy/DeployMojoTest.java | 42 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 13 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 4433ace..18ad8df 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java @@ -33,6 +33,7 @@ import org.apache.maven.api.MojoExecution; import org.apache.maven.api.ProducedArtifact; import org.apache.maven.api.Project; import org.apache.maven.api.RemoteRepository; +import org.apache.maven.api.SourceRoot; import org.apache.maven.api.di.Inject; import org.apache.maven.api.model.DistributionManagement; import org.apache.maven.api.model.Plugin; @@ -473,20 +474,20 @@ public class DeployMojo extends AbstractDeployMojo { if (attachedArtifacts.isEmpty()) { throw new MojoException( "The packaging for this project did not assign a file to the build artifact"); + } else if (usesModuleSourceHierarchy(project, projectManager)) { + getLog().info("Module source hierarchy: no main artifact produced;" + + " deploying POM and per-module artifacts only."); + toDeploy.remove(deployable); + } else if (allowIncompleteProjects) { + getLog().warn(""); + getLog().warn("The packaging plugin for this project 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 { - if (allowIncompleteProjects) { - // The main artifact has no file: its path is e.g. the output directory of a module - // source hierarchy, where the JAR plugin attaches one JAR per Java module instead of - // assigning a single main JAR. Deploy the POM and the attachments only; do not hand - // the directory to the deployer, which would otherwise fail with "is a folder". - getLog().info("The project's main artifact has no file" - + " (e.g. a module source hierarchy produces one artifact per Java module);" - + " deploying the POM and the attached artifacts only."); - toDeploy.remove(deployable); - } else { - throw new MojoException("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 MojoException("The packaging plugin for this project did not assign " + + "a main file to the project but it has attachments. Change packaging to 'pom'."); } } else { throw new MojoException("The packaging for this project did not assign " @@ -742,6 +743,12 @@ public class DeployMojo extends AbstractDeployMojo { } } + private static boolean usesModuleSourceHierarchy(Project project, ProjectManager projectManager) { + return projectManager.getSourceRoots(project).stream() + .filter(SourceRoot::enabled) + .anyMatch(sr -> sr.module().isPresent()); + } + private boolean isValidPath(Artifact a) { return getArtifactManager().getPath(a).filter(Files::isRegularFile).isPresent(); } 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 1f23566..f174572 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java @@ -25,12 +25,14 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import org.apache.maven.api.Artifact; import org.apache.maven.api.ProducedArtifact; import org.apache.maven.api.Project; import org.apache.maven.api.RemoteRepository; +import org.apache.maven.api.SourceRoot; import org.apache.maven.api.di.Inject; import org.apache.maven.api.di.Priority; import org.apache.maven.api.di.Provides; @@ -68,6 +70,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** @@ -730,6 +733,45 @@ class DeployMojoTest { assertEquals("Ambiguous syntax for alternative repository.", e.getMessage()); } + @Test + @InjectMojo(goal = "deploy") + @MojoParameter(name = "deployAtEnd", value = "false") + void moduleSourceHierarchySkipsMainArtifactAtDeploy(DeployMojo mojo) throws Exception { + assertNotNull(mojo); + Project project = (Project) getVariableValueFromObject(mojo, "project"); + + // Simulate module source hierarchy: main artifact path is a directory (the build output dir) + artifactManager.setPath(project.getMainArtifact().get(), Paths.get(getBasedir(), "target/test-classes")); + + // Attach a per-module JAR as the real deployable artifact + ProducedArtifactStub moduleJar = new ProducedArtifactStub( + "org.apache.maven.test", "maven-deploy-test", "module.one", "1.0-SNAPSHOT", "jar"); + projectManager.attachArtifact( + project, + moduleJar, + Paths.get(getBasedir(), "target/test-classes/unit/maven-deploy-test-1.0-SNAPSHOT.jar")); + + // Stub a module source root on the projectManager + SourceRoot moduleRoot = mock(SourceRoot.class); + when(moduleRoot.enabled()).thenReturn(true); + when(moduleRoot.module()).thenReturn(Optional.of("org.example.module")); + when(projectManager.getSourceRoots(project)).thenReturn(List.of(moduleRoot)); + + ArtifactDeployerRequest request = execute(mojo); + + assertNotNull(request); + Collection<ProducedArtifact> artifacts = request.getArtifacts(); + // Main artifact (directory) must be excluded; POM and per-module JAR must be deployed + List<String> keys = artifacts.stream().map(Artifact::key).collect(Collectors.toList()); + assertFalse( + keys.contains("org.apache.maven.test:maven-deploy-test:jar:1.0-SNAPSHOT"), + "directory main artifact must be excluded from deploy request"); + assertTrue(keys.contains("org.apache.maven.test:maven-deploy-test:pom:1.0-SNAPSHOT"), "POM must be deployed"); + assertTrue( + keys.contains("org.apache.maven.test:maven-deploy-test:jar:module.one:1.0-SNAPSHOT"), + "per-module JAR must be deployed"); + } + private ArtifactDeployerRequest execute(DeployMojo mojo) { ArgumentCaptor<ArtifactDeployerRequest> requestCaptor = ArgumentCaptor.forClass(ArtifactDeployerRequest.class); doNothing().when(artifactDeployer).deploy(requestCaptor.capture());
