Skip to content

Commit

Permalink
Ensure that version is not overridden to empty if set in pom
Browse files Browse the repository at this point in the history
Fixes gh-143
  • Loading branch information
dsyer committed Jun 9, 2020
1 parent 253931a commit 054d521
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public void close() throws IOException {
}
}, options);
return process(model);
}
finally {
} finally {
input.close();
}
}
Expand All @@ -93,19 +92,15 @@ static Model process(Model model, Properties properties) {
Set<String> exclusions = new HashSet<>();
for (String name : properties.stringPropertyNames()) {
if (name.startsWith("boms.")) {
String bom = replacePlaceholder(properties,
properties.getProperty(name));
String bom = replacePlaceholder(properties, properties.getProperty(name));
DefaultArtifact artifact = artifact(bom);
if (model.getDependencyManagement() == null) {
model.setDependencyManagement(new DependencyManagement());
}
boolean replaced = false;
for (Dependency dependency : model.getDependencyManagement()
.getDependencies()) {
if (ObjectUtils.nullSafeEquals(artifact.getArtifactId(),
dependency.getArtifactId())
&& ObjectUtils.nullSafeEquals(artifact.getGroupId(),
dependency.getGroupId())) {
for (Dependency dependency : model.getDependencyManagement().getDependencies()) {
if (ObjectUtils.nullSafeEquals(artifact.getArtifactId(), dependency.getArtifactId())
&& ObjectUtils.nullSafeEquals(artifact.getGroupId(), dependency.getGroupId())) {
dependency.setVersion(artifact.getVersion());
}
}
Expand All @@ -116,35 +111,26 @@ static Model process(Model model, Properties properties) {
if (!replaced) {
model.getDependencyManagement().addDependency(bom(artifact));
}
}
else if (name.startsWith("dependencies.")) {
String pom = replacePlaceholder(properties,
properties.getProperty(name));
} else if (name.startsWith("dependencies.")) {
String pom = replacePlaceholder(properties, properties.getProperty(name));
DefaultArtifact artifact = artifact(pom);
boolean replaced = false;
for (Dependency dependency : model.getDependencies()) {
if (ObjectUtils.nullSafeEquals(artifact.getArtifactId(),
dependency.getArtifactId())
&& ObjectUtils.nullSafeEquals(artifact.getGroupId(),
dependency.getGroupId())
&& ObjectUtils.nullSafeEquals(artifact.getClassifier(),
dependency.getClassifier())
&& artifact.getVersion() != null) {
dependency.setVersion(
StringUtils.hasLength(artifact.getVersion())
? artifact.getVersion()
: null);
if (ObjectUtils.nullSafeEquals(artifact.getArtifactId(), dependency.getArtifactId())
&& ObjectUtils.nullSafeEquals(artifact.getGroupId(), dependency.getGroupId())
&& equalOrEmpty(artifact.getClassifier(), dependency.getClassifier())) {
if (StringUtils.hasText(artifact.getVersion())) {
dependency.setVersion(artifact.getVersion());
}
dependency.setScope("runtime");
replaced = true;
}
}
if (!replaced) {
model.getDependencies().add(dependency(artifact));
}
}
else if (name.startsWith("exclusions.")) {
String pom = replacePlaceholder(properties,
properties.getProperty(name));
} else if (name.startsWith("exclusions.")) {
String pom = replacePlaceholder(properties, properties.getProperty(name));
exclusions.add(pom);
}
}
Expand All @@ -164,22 +150,22 @@ else if (name.startsWith("exclusions.")) {
}
}
for (Dependency dependency : new ArrayList<>(model.getDependencies())) {
if ("test".equals(dependency.getScope())
|| "provided".equals(dependency.getScope())) {
if ("test".equals(dependency.getScope()) || "provided".equals(dependency.getScope())) {
model.getDependencies().remove(dependency);
}
}
return model;
}

private static boolean equalOrEmpty(String first, String second) {
return ObjectUtils.nullSafeEquals(first, second) || (!StringUtils.hasText(first) && !StringUtils.hasText(second));
}

static boolean isSameArtifact(Dependency target, Dependency dependency) {
boolean classifierMatch = (target.getClassifier() == null
&& dependency.getClassifier() == null)
|| (target.getClassifier() == null ? false
: target.getClassifier().equals(dependency.getClassifier()));
boolean classifierMatch = (target.getClassifier() == null && dependency.getClassifier() == null)
|| (target.getClassifier() == null ? false : target.getClassifier().equals(dependency.getClassifier()));
return dependency.getGroupId().equals(target.getGroupId())
&& dependency.getArtifactId().equals(target.getArtifactId())
&& classifierMatch;
&& dependency.getArtifactId().equals(target.getArtifactId()) && classifierMatch;
}

private static String replacePlaceholder(Properties properties, String value) {
Expand All @@ -206,16 +192,12 @@ private static boolean isParentBom(Model model, DefaultArtifact artifact) {
if (model.getParent() == null) {
return false;
}
if (ObjectUtils.nullSafeEquals(artifact.getArtifactId(),
model.getParent().getArtifactId())
&& ObjectUtils.nullSafeEquals(artifact.getGroupId(),
model.getParent().getGroupId())) {
if (ObjectUtils.nullSafeEquals(artifact.getArtifactId(), model.getParent().getArtifactId())
&& ObjectUtils.nullSafeEquals(artifact.getGroupId(), model.getParent().getGroupId())) {
return true;
}
if (ObjectUtils.nullSafeEquals("spring-boot-starter-parent",
model.getParent().getArtifactId())
&& ObjectUtils.nullSafeEquals(artifact.getArtifactId(),
"spring-boot-dependencies")) {
if (ObjectUtils.nullSafeEquals("spring-boot-starter-parent", model.getParent().getArtifactId())
&& ObjectUtils.nullSafeEquals(artifact.getArtifactId(), "spring-boot-dependencies")) {
return true;
}
return false;
Expand All @@ -225,19 +207,14 @@ static Dependency dependency(DefaultArtifact artifact) {
Dependency dependency = new Dependency();
dependency.setGroupId(artifact.getGroupId());
dependency.setArtifactId(artifact.getArtifactId());
dependency.setVersion(
StringUtils.hasLength(artifact.getVersion()) ? artifact.getVersion()
: null);
dependency.setClassifier(
StringUtils.hasLength(artifact.getClassifier()) ? artifact.getClassifier()
: null);
dependency.setVersion(StringUtils.hasLength(artifact.getVersion()) ? artifact.getVersion() : null);
dependency.setClassifier(StringUtils.hasLength(artifact.getClassifier()) ? artifact.getClassifier() : null);
dependency.setType(artifact.getExtension());
return dependency;
}

static DefaultArtifact artifact(String coordinates) {
Pattern p = Pattern
.compile("([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?(:([^: ]+))?");
Pattern p = Pattern.compile("([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?(:([^: ]+))?");
Matcher m = p.matcher(coordinates);
Assert.isTrue(m.matches(), "Bad artifact coordinates " + coordinates
+ ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]][:<version>]");
Expand All @@ -258,8 +235,7 @@ static DefaultArtifact artifact(String coordinates) {
version = classifier;
classifier = "";
}
}
else {
} else {
if (version == null && isVersion(extension)) {
version = extension;
extension = DEFAULT_EXTENSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public void petclinic() throws Exception {
.filteredOn("artifact.artifactId", "spring-boot-starter-test").isEmpty();
}

@Test
public void launcher() throws Exception {
Resource resource = new ClassPathResource("apps/launcher/pom.xml");
// Resource resource = new UrlResource("jar:file:/home/dsyer/.m2/repository/org/springframework/cloud/launcher/spring-cloud-launcher-deployer/2.2.1.RELEASE/spring-cloud-launcher-deployer-2.2.1.RELEASE.jar!/META-INF/maven/org.springframework.cloud.launcher/spring-cloud-launcher-deployer/pom.xml");
Properties properties = new Properties();
properties.setProperty(ThinJarLauncher.THIN_PROFILE, "thin");
List<Dependency> dependencies = resolver.dependencies(resource, properties);
// System.err.println(dependencies);
assertThat(dependencies.size()).isGreaterThan(1);
}



@Test
public void mavenProfiles() throws Exception {
Resource resource = new ClassPathResource("apps/profiles/pom.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ public void classpath() throws Exception {
.contains("spring-web-4.3.3.RELEASE.jar" + File.pathSeparator);
}

@Test
public void launcher() throws Exception {
String[] args = new String[] { "--thin.classpath",
"--thin.archive=src/test/resources/apps/launcher" };
ThinJarLauncher.main(args);
assertThat(output.toString())
.contains("spring-cloud-deployer-thin-1.0.22.RELEASE.jar" + File.pathSeparator);
}

@Test
public void fatClasspath() throws Exception {
String[] args = new String[] { "--thin.classpath",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exclusions.spring-cloud-deployer-local: org.springframework.cloud:spring-cloud-deployer-local
dependencies.spring-cloud-deployer-thin: org.springframework.cloud:spring-cloud-deployer-thin
137 changes: 137 additions & 0 deletions launcher/src/test/resources/apps/launcher/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-launcher</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<groupId>org.springframework.cloud.launcher</groupId>
<artifactId>spring-cloud-launcher-deployer</artifactId>
<version>2.2.1.RELEASE</version>
<name>spring-cloud-launcher-deployer</name>
<description>Spring Cloud Launcher Deployer</description>
<url>https://projects.spring.io/spring-cloud/spring-cloud-launcher/spring-cloud-launcher-deployer/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>https://www.spring.io</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
<comments>Copyright 2014-2015 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

https://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.</comments>
</license>
</licenses>
<developers>
<developer>
<id>dsyer</id>
<name>Dave Syer</name>
<email>dsyer at pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
<roles>
<role>lead</role>
</roles>
</developer>
<developer>
<id>sgibb</id>
<name>Spencer Gibb</name>
<email>sgibb at pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
<roles>
<role>lead</role>
</roles>
</developer>
<developer>
<id>mgrzejszczak</id>
<name>Marcin Grzejszczak</name>
<email>mgrzejszczak at pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
<roles>
<role>developer</role>
</roles>
</developer>
<developer>
<id>rbaxter</id>
<name>Ryan Baxter</name>
<email>rbaxter at pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
<roles>
<role>developer</role>
</roles>
</developer>
<developer>
<id>omaciaszeksharma</id>
<name>Olga Maciaszek-Sharma</name>
<email>omaciaszeksharma at pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
<roles>
<role>developer</role>
</roles>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/spring-cloud/spring-cloud-cli.git/spring-cloud-launcher/spring-cloud-launcher-deployer</connection>
<developerConnection>scm:git:ssh://[email protected]/spring-cloud/spring-cloud-cli.git/spring-cloud-launcher/spring-cloud-launcher-deployer</developerConnection>
<url>https://github.com/spring-cloud/spring-cloud-cli/spring-cloud-launcher/spring-cloud-launcher-deployer</url>
</scm>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader</artifactId>
<version>2.1.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-deployer-thin</artifactId>
<version>1.0.22.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>spring-boot-test-support</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-deployer-resource-support</artifactId>
<version>2.0.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.2.RELEASE</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>

0 comments on commit 054d521

Please sign in to comment.