From 14cc4c305af2fc53571afc8ef127d7bb23155ce5 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 15 Aug 2024 13:40:47 +0200 Subject: [PATCH] [MDEPLOY-311] Consider packaging in deploy-file mojo (#71) The packaging was not really considered. Borrow UT from PR #42, thanks! Changes: * honor packaging to calculate classifier (optionally) and extension * add param "extension" to override Maven calculated extension explicitly * switch to SLF4J logging --- https://issues.apache.org/jira/browse/MDEPLOY-311 --- .../maven/plugins/deploy/DeployFileMojo.java | 47 ++++++++++++++----- .../plugins/deploy/DeployFileMojoTest.java | 35 ++++++++++++++ .../deploy-file-packaging/plugin-config.xml | 36 ++++++++++++++ .../target/deploy-test-file-1.0-SNAPSHOT.jar | 1 + 4 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 src/test/resources/unit/deploy-file-packaging/plugin-config.xml create mode 100644 src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java index 66bf21f1..0dfc8c36 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java @@ -55,6 +55,8 @@ import org.eclipse.aether.deployment.DeploymentException; import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.util.artifact.SubArtifact; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Installs the artifact in the remote repository. @@ -63,6 +65,7 @@ */ @Mojo(name = "deploy-file", requiresProject = false, threadSafe = true) public class DeployFileMojo extends AbstractDeployMojo { + private final Logger log = LoggerFactory.getLogger(getClass()); /** * GroupId of the artifact to be deployed. Retrieved from POM file if specified. */ @@ -90,6 +93,15 @@ public class DeployFileMojo extends AbstractDeployMojo { @Parameter(property = "packaging") private String packaging; + /** + * Extension of the artifact to be deployed. If set, will override plugin own logic to detect extension. If not set, + * as Maven expected, packaging determines the artifact extension. + * + * @since 3.1.3 + */ + @Parameter(property = "extension") + private String extension; + /** * Description passed to a generated POM file (in case of generatePom=true) */ @@ -196,7 +208,7 @@ void initProperties() throws MojoExecutionException { JarEntry entry = jarEntries.nextElement(); if (pomEntry.matcher(entry.getName()).matches()) { - getLog().debug("Using " + entry.getName() + " as pomFile"); + log.debug("Using {} as pomFile", entry.getName()); foundPom = true; String base = file.getName(); if (base.indexOf('.') > 0) { @@ -215,7 +227,7 @@ void initProperties() throws MojoExecutionException { } if (!foundPom) { - getLog().info("pom.xml not found in " + file.getName()); + log.info("pom.xml not found in {}", file.getName()); } } catch (IOException e) { // ignore, artifact not packaged by Maven @@ -235,7 +247,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { if (Boolean.parseBoolean(skip) || ("releases".equals(skip) && !ArtifactUtils.isSnapshot(version)) || ("snapshots".equals(skip) && ArtifactUtils.isSnapshot(version))) { - getLog().info("Skipping artifact deployment"); + log.info("Skipping artifact deployment"); return; } @@ -266,18 +278,29 @@ public void execute() throws MojoExecutionException, MojoFailureException { DeployRequest deployRequest = new DeployRequest(); deployRequest.setRepository(remoteRepository); - boolean isFilePom = classifier == null && "pom".equals(packaging); - if (!isFilePom) { + String mainArtifactExtension; + if (classifier == null && "pom".equals(packaging)) { + mainArtifactExtension = "pom"; + } else { ArtifactType artifactType = session.getRepositorySession().getArtifactTypeRegistry().get(packaging); - if (artifactType != null - && (classifier == null || classifier.isEmpty()) - && !StringUtils.isEmpty(artifactType.getClassifier())) { - classifier = artifactType.getClassifier(); + if (artifactType != null) { + if (StringUtils.isEmpty(classifier) && !StringUtils.isEmpty(artifactType.getClassifier())) { + classifier = artifactType.getClassifier(); + } + mainArtifactExtension = artifactType.getExtension(); + } else { + mainArtifactExtension = packaging; } } + if (extension != null && !Objects.equals(extension, mainArtifactExtension)) { + log.warn( + "Main artifact extension should be '{}' but was overridden to '{}'", + mainArtifactExtension, + extension); + } Artifact mainArtifact = new DefaultArtifact( - groupId, artifactId, classifier, isFilePom ? "pom" : getExtension(file), version) + groupId, artifactId, classifier, extension != null ? extension : mainArtifactExtension, version) .setFile(file); deployRequest.addArtifact(mainArtifact); @@ -293,10 +316,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { deployRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", pomFile)); } else if (generatePom) { temporaryPom = generatePomFile(); - getLog().debug("Deploying generated POM"); + log.debug("Deploying generated POM"); deployRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", temporaryPom)); } else { - getLog().debug("Skipping deploying POM"); + log.debug("Skipping deploying POM"); } } diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java index b1083cb3..13748814 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java @@ -276,6 +276,41 @@ public void testDeployIfArtifactIsNotJar() throws Exception { assertTrue(file.exists()); } + public void testDeployFileIfPackagingIsSet() throws Exception { + File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-packaging/plugin-config.xml"); + mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom); + + openMocks = MockitoAnnotations.openMocks(this); + + assertNotNull(mojo); + + DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); + repositorySession.setLocalRepositoryManager( + new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer()) + .newInstance(repositorySession, new LocalRepository(LOCAL_REPO))); + when(session.getRepositorySession()).thenReturn(repositorySession); + + String packaging = (String) getVariableValueFromObject(mojo, "packaging"); + + String groupId = (String) getVariableValueFromObject(mojo, "groupId"); + + String artifactId = (String) getVariableValueFromObject(mojo, "artifactId"); + + String version = (String) getVariableValueFromObject(mojo, "version"); + + assertEquals("differentpackaging", packaging); + + mojo.execute(); + + File deployedArtifact = new File( + remoteRepo, + "deploy-file-packaging/" + groupId.replace('.', '/') + "/" + + artifactId + "/" + version + "/" + artifactId + "-" + + version + "." + packaging); + + assertTrue(deployedArtifact.exists()); + } + private void addFileToList(File file, List fileList) { if (!file.isDirectory()) { fileList.add(file.getName()); diff --git a/src/test/resources/unit/deploy-file-packaging/plugin-config.xml b/src/test/resources/unit/deploy-file-packaging/plugin-config.xml new file mode 100644 index 00000000..170cd1c9 --- /dev/null +++ b/src/test/resources/unit/deploy-file-packaging/plugin-config.xml @@ -0,0 +1,36 @@ + + + + + + + maven-deploy-plugin + + org.apache.maven.test + maven-deploy-file-test + 1.0 + differentpackaging + ${basedir}/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar + deploy-test + file://${basedir}/target/remote-repo/deploy-file-packaging + true + + + + + \ No newline at end of file diff --git a/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar b/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar new file mode 100644 index 00000000..6f5f2f81 --- /dev/null +++ b/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar @@ -0,0 +1 @@ +This is not an actual jar \ No newline at end of file