diff --git a/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtils.java b/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtils.java index 91f65a25..0a22982c 100644 --- a/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtils.java +++ b/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtils.java @@ -88,7 +88,10 @@ private static void loadMavenArtifact(Element artifactElt, MavenArtifact mavenAr if (mavenArtifact.getBaseVersion() == null || mavenArtifact.getBaseVersion().isEmpty()) { mavenArtifact.setBaseVersion(mavenArtifact.getVersion()); } - mavenArtifact.setSnapshot(Boolean.parseBoolean(artifactElt.getAttribute("snapshot"))); + String snapshot = artifactElt.getAttribute("snapshot"); + mavenArtifact.setSnapshot(snapshot != null && !snapshot.trim().isEmpty() + ? Boolean.parseBoolean(artifactElt.getAttribute("snapshot")) + : mavenArtifact.getBaseVersion().contains("SNAPSHOT")); mavenArtifact.setType(artifactElt.getAttribute("type")); if (mavenArtifact.getType() == null || mavenArtifact.getType().isEmpty()) { // workaround: sometimes we use "XmlUtils.newMavenArtifact()" on "project" elements, in this case, "packaging" is defined but "type" is not defined @@ -450,11 +453,17 @@ public static List listGeneratedArtifacts(Element mavenSpyLogs, b pomArtifact.setGroupId(projectArtifact.getGroupId()); pomArtifact.setArtifactId(projectArtifact.getArtifactId()); pomArtifact.setBaseVersion(projectArtifact.getBaseVersion()); - pomArtifact.setVersion(projectArtifact.getVersion()); pomArtifact.setSnapshot(projectArtifact.isSnapshot()); pomArtifact.setType("pom"); pomArtifact.setExtension("pom"); pomArtifact.setFile(projectElt.getAttribute("file")); + Element artifactDeployedEvent = XmlUtils.getArtifactDeployedEvent(artifactDeployedEvents, pomArtifact.getFile()); + if (artifactDeployedEvent == null) { + // artifact has not been deployed ("mvn deploy") + pomArtifact.setVersion(projectArtifact.getVersion()); + } else { + pomArtifact.setVersion(XmlUtils.getUniqueChildElement(artifactDeployedEvent, "artifact").getAttribute("version")); + } result.add(pomArtifact); @@ -473,10 +482,11 @@ public static List listGeneratedArtifacts(Element mavenSpyLogs, b } else { mavenArtifact.setFile(StringUtils.trim(fileElt.getTextContent())); - Element artifactDeployedEvent = XmlUtils.getArtifactDeployedEvent(artifactDeployedEvents, mavenArtifact.getFile()); - if(artifactDeployedEvent == null) { + artifactDeployedEvent = XmlUtils.getArtifactDeployedEvent(artifactDeployedEvents, mavenArtifact.getFile()); + if (artifactDeployedEvent == null) { // artifact has not been deployed ("mvn deploy") } else { + mavenArtifact.setVersion(XmlUtils.getUniqueChildElement(artifactDeployedEvent, "artifact").getAttribute("version")); mavenArtifact.setRepositoryUrl(XmlUtils.getUniqueChildElement(artifactDeployedEvent, "repository").getAttribute("url")); } } diff --git a/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtilsTest.java b/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtilsTest.java index 6317a5c8..476806c6 100644 --- a/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtilsTest.java +++ b/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtilsTest.java @@ -315,6 +315,65 @@ public void test_listGeneratedArtifacts() throws Exception { } } } + + @Test + public void test_listGeneratedArtifacts_deploy_2_8() throws Exception { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-2.8.xml"); + in.getClass(); // check non null + Element mavenSpyLogs = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in).getDocumentElement(); + + List generatedArtifacts = XmlUtils.listGeneratedArtifacts(mavenSpyLogs, false); + + assertThat(generatedArtifacts.size(), is(2)); + + assertThat(generatedArtifacts.get(0).getGroupId(), is("com.acme.maven.plugins")); + assertThat(generatedArtifacts.get(0).getArtifactId(), is("postgresql-compare-maven-plugin")); + assertThat(generatedArtifacts.get(0).getExtension(), is("pom")); + assertThat(generatedArtifacts.get(0).getClassifier(), is(emptyOrNullString())); + assertThat(generatedArtifacts.get(0).getBaseVersion(), is("1.0.2-SNAPSHOT")); + assertThat(generatedArtifacts.get(0).getType(), is("pom")); + assertThat(generatedArtifacts.get(0).getVersion(), is("1.0.2-20220904.210621-1")); + assertThat(generatedArtifacts.get(0).isSnapshot(), is(true)); + + assertThat(generatedArtifacts.get(1).getGroupId(), is("com.acme.maven.plugins")); + assertThat(generatedArtifacts.get(1).getArtifactId(), is("postgresql-compare-maven-plugin")); + assertThat(generatedArtifacts.get(1).getExtension(), is("jar")); + assertThat(generatedArtifacts.get(1).getClassifier(), is(emptyOrNullString())); + assertThat(generatedArtifacts.get(1).getBaseVersion(), is("1.0.2-SNAPSHOT")); + assertThat(generatedArtifacts.get(1).getType(), is("maven-plugin")); + assertThat(generatedArtifacts.get(1).getVersion(), is("1.0.2-20220904.210621-1")); + assertThat(generatedArtifacts.get(1).isSnapshot(), is(true)); + } + + @Test + public void test_listGeneratedArtifacts_deploy_3_0() throws Exception { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-3.0.xml"); + in.getClass(); // check non null + Element mavenSpyLogs = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in).getDocumentElement(); + + List generatedArtifacts = XmlUtils.listGeneratedArtifacts(mavenSpyLogs, false); + + assertThat(generatedArtifacts.size(), is(2)); + + assertThat(generatedArtifacts.get(0).getGroupId(), is("com.acme.maven.plugins")); + assertThat(generatedArtifacts.get(0).getArtifactId(), is("postgresql-compare-maven-plugin")); + assertThat(generatedArtifacts.get(0).getExtension(), is("pom")); + assertThat(generatedArtifacts.get(0).getClassifier(), is(emptyOrNullString())); + assertThat(generatedArtifacts.get(0).getBaseVersion(), is("1.0.2-SNAPSHOT")); + assertThat(generatedArtifacts.get(0).getType(), is("pom")); + assertThat(generatedArtifacts.get(0).getVersion(), is("1.0.2-20220904.210621-1")); + assertThat(generatedArtifacts.get(0).isSnapshot(), is(true)); + + assertThat(generatedArtifacts.get(1).getGroupId(), is("com.acme.maven.plugins")); + assertThat(generatedArtifacts.get(1).getArtifactId(), is("postgresql-compare-maven-plugin")); + assertThat(generatedArtifacts.get(1).getExtension(), is("jar")); + assertThat(generatedArtifacts.get(1).getClassifier(), is(emptyOrNullString())); + assertThat(generatedArtifacts.get(1).getBaseVersion(), is("1.0.2-SNAPSHOT")); + assertThat(generatedArtifacts.get(1).getType(), is("maven-plugin")); + assertThat(generatedArtifacts.get(1).getVersion(), is("1.0.2-20220904.210621-1")); + assertThat(generatedArtifacts.get(1).isSnapshot(), is(true)); + } + @Test public void test_listGeneratedArtifacts_including_generated_artifacts() throws Exception { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml"); diff --git a/jenkins-plugin/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-2.8.xml b/jenkins-plugin/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-2.8.xml new file mode 100644 index 00000000..25d077a5 --- /dev/null +++ b/jenkins-plugin/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-2.8.xml @@ -0,0 +1,64 @@ + + + + + + + ${altDeploymentRepository} + ${altReleaseDeploymentRepository} + ${altSnapshotDeploymentRepository} + ${deployAtEnd} + ${settings.offline} + ${project} + ${reactorProjects} + ${retryFailedDeploymentCount} + ${session} + ${maven.deploy.skip} + + + + + + + + + + + + + + + + ${altDeploymentRepository} + ${altReleaseDeploymentRepository} + ${altSnapshotDeploymentRepository} + ${deployAtEnd} + ${settings.offline} + ${project} + ${reactorProjects} + ${retryFailedDeploymentCount} + ${session} + ${maven.deploy.skip} + + + acme + https://server/maven-snapshots/ + + + + + + + + + /home/jenkins/workspace/gresql-compare-maven-plugin_main/target/postgresql-compare-maven-plugin-1.0.2-SNAPSHOT.jar + + + + + + + + + + diff --git a/jenkins-plugin/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-3.0.xml b/jenkins-plugin/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-3.0.xml new file mode 100644 index 00000000..c302b6c4 --- /dev/null +++ b/jenkins-plugin/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-3.0.xml @@ -0,0 +1,64 @@ + + + + + + + ${altDeploymentRepository} + ${altReleaseDeploymentRepository} + ${altSnapshotDeploymentRepository} + ${deployAtEnd} + ${settings.offline} + ${project} + ${reactorProjects} + ${retryFailedDeploymentCount} + ${session} + ${maven.deploy.skip} + + + + + + + + + + + + + + + + ${altDeploymentRepository} + ${altReleaseDeploymentRepository} + ${altSnapshotDeploymentRepository} + ${deployAtEnd} + ${settings.offline} + ${project} + ${reactorProjects} + ${retryFailedDeploymentCount} + ${session} + ${maven.deploy.skip} + + + acme + https://server/maven-snapshots/ + + + + + + + + + /home/jenkins/workspace/gresql-compare-maven-plugin_main/target/postgresql-compare-maven-plugin-1.0.2-SNAPSHOT.jar + + + + + + + + + +