Fixes different group id pom update and deployment skip for snapshot versions

fixes gh-70 gh-67
This commit is contained in:
Marcin Grzejszczak
2018-02-02 01:40:36 +01:00
parent eb795d3f42
commit 4742798f53
8 changed files with 279 additions and 4 deletions

View File

@@ -21,11 +21,13 @@ import java.io.FileWriter;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.change.AbstractVersionChanger;
@@ -33,6 +35,7 @@ import org.codehaus.mojo.versions.change.VersionChange;
import org.codehaus.mojo.versions.change.VersionChanger;
import org.codehaus.mojo.versions.change.VersionChangerFactory;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.stax2.XMLInputFactory2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -71,6 +74,49 @@ class PomUpdater {
return true;
}
boolean hasSkipDeployment(Model model) {
String property = model.getProperties()
.getProperty("maven.deploy.skip");
boolean hasSkipDeploymentProperty = Boolean.parseBoolean(property);
if (hasSkipDeploymentProperty) {
return true;
}
if (model.getBuild() == null) {
return false;
}
boolean plugins = model.getBuild()
.getPlugins()
.stream()
.filter(plugin -> "maven-deploy-plugin".equalsIgnoreCase(plugin.getArtifactId()))
.map(this::skipFromConfiguration)
.findFirst()
.orElse(false);
if (plugins) {
return true;
}
if (model.getBuild()
.getPluginManagement() == null) {
return false;
}
return model.getBuild()
.getPluginManagement()
.getPlugins()
.stream()
.filter(plugin -> "maven-deploy-plugin".equalsIgnoreCase(plugin.getArtifactId()))
.map(this::skipFromConfiguration)
.findFirst()
.orElse(false);
}
private Boolean skipFromConfiguration(Plugin plugin) {
if (!(plugin.getConfiguration() instanceof Xpp3Dom)) {
return false;
}
Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
Xpp3Dom skip = configuration.getChild("skip");
return skip != null && Boolean.parseBoolean(skip.getValue());
}
private File rootPom(File rootFolder) {
if (rootFolder.getName().endsWith(".xml")) {
return rootFolder;
@@ -130,7 +176,8 @@ class PomUpdater {
log.debug("Can't set the value for parent... Will return {}", sourceChanges);
return changes;
}
if (model.getGroupId() != null && !model.getGroupId().equals(rootProjectGroupId)) {
boolean skipDeployment = hasSkipDeployment(model);
if (!skipDeployment && model.getGroupId() != null && !model.getGroupId().equals(rootProjectGroupId)) {
log.info("Will not update the project's [{}] parent [{}] since its group id [{}] is not equal the parent group id [{}]",
model.getArtifactId(), model.getParent().getArtifactId(), model.getGroupId(), rootProjectGroupId);
return changes;

View File

@@ -23,8 +23,11 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Map;
import java.util.Scanner;
import org.apache.maven.model.Model;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.release.internal.ReleaserProperties;
@@ -128,7 +131,7 @@ public class ProjectPomUpdater {
}
ModelWrapper model = this.pomUpdater.updateModel(this.rootPom, file, this.versions);
this.pomUpdater.overwritePomIfDirty(model, this.versions, file);
if (this.assertSnapshots && !this.snapshotVersion) {
if (this.assertSnapshots && !this.snapshotVersion && !this.pomUpdater.hasSkipDeployment(model.model)) {
log.debug("Update is a non-snapshot one. Checking if no snapshot versions remained in the pom");
Scanner scanner = new Scanner(asString(path));
int lineNumber = 0;

View File

@@ -68,6 +68,25 @@ public class PomUpdateAcceptanceTests {
@Test
public void should_update_fail_when_after_updating_a_release_version_there_still_is_a_snapshot_version() throws Exception {
ReleaserProperties releaserProperties = branchReleaserProperties();
ProjectPomUpdater projectPomUpdater = new ProjectPomUpdater(releaserProperties);
Projects projects = projectPomUpdater.retrieveVersionsFromSCRelease();
projects.add(new ProjectVersion("spring-cloud-sleuth-samples", "0.0.5.RELEASE"));
File project = new File(this.temporaryFolder, "/spring-cloud-sleuth-with-unmatched-property/spring-cloud-sleuth-samples");
addBuildSnapshotToChildPom(project);
projectPomUpdater
.updateProjectFromSCRelease(project, projects, projects.forFile(project), true);
}
private void addBuildSnapshotToChildPom(File project) throws IOException {
File childPom = new File(project, "pom.xml");
String text = new String(Files.readAllBytes(childPom.toPath()));
Files.write(childPom.toPath(), text.replaceAll("1.19.2", "1.19.2.BUILD-SNAPSHOT").getBytes());
}
@Test
public void should_not_fail_update_when_after_updating_a_release_version_there_still_is_a_snapshot_version_in_a_non_deployable_module() throws Exception {
ReleaserProperties releaserProperties = branchReleaserProperties();
ProjectPomUpdater projectPomUpdater = new ProjectPomUpdater(releaserProperties);
Projects projects = projectPomUpdater.retrieveVersionsFromSCRelease();

View File

@@ -173,7 +173,7 @@ public class PomUpdaterTests {
}
@Test
public void should_only_update_the_props_when_group_ids_dont_match() throws Exception {
public void should_only_update_the_properties_section_when_group_ids_dont_match_and_there_is_no_skip_deployment() throws Exception {
File originalPom = pom("/projects/project/children", "pom_different_group.xml");
File pomInTemp = tmpFile("/project/children/pom_different_group.xml");
ModelWrapper rootPom = model("spring-cloud-sleuth", "org.springframework.cloud");
@@ -190,6 +190,60 @@ public class PomUpdaterTests {
.containsEntry("spring-cloud-sleuth.version", "0.0.3.BUILD-SNAPSHOT");
}
@Test
public void should_update_everything_when_group_ids_dont_match_and_there_is_skip_deployment_property() throws Exception {
File originalPom = pom("/projects/project/children", "pom_different_group_skip_deployment_prop.xml");
File pomInTemp = tmpFile("/project/children/pom_different_group_skip_deployment_prop.xml");
ModelWrapper rootPom = model("spring-cloud-sleuth", "org.springframework.cloud");
ModelWrapper model = this.pomUpdater.updateModel(rootPom, pomInTemp, this.versions);
File storedPom = this.pomUpdater.overwritePomIfDirty(model, this.versions, pomInTemp);
BDDAssertions.then(asString(storedPom)).isNotEqualTo(asString(originalPom));
Model overriddenPomModel = this.pomReader.readPom(storedPom);
BDDAssertions.then(overriddenPomModel.getVersion()).isEqualTo("1.2.2.BUILD-SNAPSHOT");
BDDAssertions.then(overriddenPomModel.getParent().getVersion()).isEqualTo("0.0.1");
// the rest is the same
BDDAssertions.then(overriddenPomModel.getProperties())
.containsEntry("spring-cloud-sleuth.version", "0.0.3.BUILD-SNAPSHOT");
}
@Test
public void should_update_everything_when_group_ids_dont_match_and_there_is_skip_in_deployment_plugin() throws Exception {
File originalPom = pom("/projects/project/children", "pom_different_group_skip_deployment_plugin.xml");
File pomInTemp = tmpFile("/project/children/pom_different_group_skip_deployment_plugin.xml");
ModelWrapper rootPom = model("spring-cloud-sleuth", "org.springframework.cloud");
ModelWrapper model = this.pomUpdater.updateModel(rootPom, pomInTemp, this.versions);
File storedPom = this.pomUpdater.overwritePomIfDirty(model, this.versions, pomInTemp);
BDDAssertions.then(asString(storedPom)).isNotEqualTo(asString(originalPom));
Model overriddenPomModel = this.pomReader.readPom(storedPom);
BDDAssertions.then(overriddenPomModel.getVersion()).isEqualTo("1.2.2.BUILD-SNAPSHOT");
BDDAssertions.then(overriddenPomModel.getParent().getVersion()).isEqualTo("0.0.1");
// the rest is the same
BDDAssertions.then(overriddenPomModel.getProperties())
.containsEntry("spring-cloud-sleuth.version", "0.0.3.BUILD-SNAPSHOT");
}
@Test
public void should_update_everything_when_group_ids_dont_match_and_there_is_skip_in_deployment_plugin_management() throws Exception {
File originalPom = pom("/projects/project/children", "pom_different_group_skip_deployment_plugin_mngmnt.xml");
File pomInTemp = tmpFile("/project/children/pom_different_group_skip_deployment_plugin_mngmnt.xml");
ModelWrapper rootPom = model("spring-cloud-sleuth", "org.springframework.cloud");
ModelWrapper model = this.pomUpdater.updateModel(rootPom, pomInTemp, this.versions);
File storedPom = this.pomUpdater.overwritePomIfDirty(model, this.versions, pomInTemp);
BDDAssertions.then(asString(storedPom)).isNotEqualTo(asString(originalPom));
Model overriddenPomModel = this.pomReader.readPom(storedPom);
BDDAssertions.then(overriddenPomModel.getVersion()).isEqualTo("1.2.2.BUILD-SNAPSHOT");
BDDAssertions.then(overriddenPomModel.getParent().getVersion()).isEqualTo("0.0.1");
// the rest is the same
BDDAssertions.then(overriddenPomModel.getProperties())
.containsEntry("spring-cloud-sleuth.version", "0.0.3.BUILD-SNAPSHOT");
}
@Test
public void should_not_update_the_project_if_group_doesnt_match() throws Exception {
File originalPom = pom("/projects/project/children", "pom_case_from_contract.xml");

View File

@@ -27,7 +27,6 @@ import org.springframework.util.FileSystemUtils;
*/
public class ProjectBuilderTests {
TestPomReader reader = new TestPomReader();
@Rule public TemporaryFolder tmp = new TemporaryFolder();
File temporaryFolder;
@Rule public OutputCapture outputCapture = new OutputCapture();

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>http-server-dsl</artifactId>
<version>1.2.2.BUILD-SNAPSHOT</version>
<name>Spring Cloud Contract Verifier Http Server Sample</name>
<description>Spring Cloud Contract Verifier Http Server Sample</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>
<properties>
<spring-cloud-sleuth.version>1.3.1.BUILD-SNAPSHOT</spring-cloud-sleuth.version>
</properties>
<build>
<plugins>
<plugin>
<!--skip deploy (this is just a test module) -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>http-server-dsl</artifactId>
<version>1.2.2.BUILD-SNAPSHOT</version>
<name>Spring Cloud Contract Verifier Http Server Sample</name>
<description>Spring Cloud Contract Verifier Http Server Sample</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>
<properties>
<spring-cloud-sleuth.version>1.3.1.BUILD-SNAPSHOT</spring-cloud-sleuth.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<!--skip deploy (this is just a test module) -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>http-server-dsl</artifactId>
<version>1.2.2.BUILD-SNAPSHOT</version>
<name>Spring Cloud Contract Verifier Http Server Sample</name>
<description>Spring Cloud Contract Verifier Http Server Sample</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>
<properties>
<spring-cloud-sleuth.version>1.3.1.BUILD-SNAPSHOT</spring-cloud-sleuth.version>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<build>
<plugins>
<plugin>
<!--skip deploy (this is just a test module) -->
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>