From de714224a910513027bfd78b1db5e73394417258 Mon Sep 17 00:00:00 2001 From: spencergibb Date: Mon, 8 Feb 2021 13:26:25 -0500 Subject: [PATCH] Replace static version field with ClassPathResource. Previously, the default version was loaded from a java constant. This version used to be updated by hand after the release train was published to maven central, then built and deployed by hand. Spring-Cloud-Cli is now built and deployed automatically, but the default version hasn't been updated. The default version was only used if the version couldn't be read from the manifest (which should be the version used during release. The constant was replaced with the contents of a file that is updated at build time. This process works for development and release without any manual intervention. Fixes gh-174 --- .../spring-cloud-launcher-deployer/pom.xml | 6 +++++ .../deployer/DeployerApplication.java | 23 +++++++++++++++---- .../main/resources/META-INF/cli-version.txt | 1 + .../deployer/DeployerApplicationTests.java | 22 +++++++++++------- 4 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 spring-cloud-launcher/spring-cloud-launcher-deployer/src/main/resources/META-INF/cli-version.txt diff --git a/spring-cloud-launcher/spring-cloud-launcher-deployer/pom.xml b/spring-cloud-launcher/spring-cloud-launcher-deployer/pom.xml index 81f1bf2..2036806 100644 --- a/spring-cloud-launcher/spring-cloud-launcher-deployer/pom.xml +++ b/spring-cloud-launcher/spring-cloud-launcher-deployer/pom.xml @@ -87,6 +87,12 @@ + + + src/main/resources + true + + org.springframework.boot diff --git a/spring-cloud-launcher/spring-cloud-launcher-deployer/src/main/java/org/springframework/cloud/launcher/deployer/DeployerApplication.java b/spring-cloud-launcher/spring-cloud-launcher-deployer/src/main/java/org/springframework/cloud/launcher/deployer/DeployerApplication.java index c6ec793..2c776f1 100644 --- a/spring-cloud-launcher/spring-cloud-launcher-deployer/src/main/java/org/springframework/cloud/launcher/deployer/DeployerApplication.java +++ b/spring-cloud-launcher/spring-cloud-launcher-deployer/src/main/java/org/springframework/cloud/launcher/deployer/DeployerApplication.java @@ -16,6 +16,9 @@ package org.springframework.cloud.launcher.deployer; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -31,7 +34,10 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.logback.LogbackLoggingSystem; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.io.ClassPathResource; import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; +import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; /** @@ -42,8 +48,6 @@ public class DeployerApplication { private static final Logger logger = LoggerFactory .getLogger(DeployerApplication.class); - private static final String DEFAULT_VERSION = "2.2.0.RELEASE"; - private String[] args; public DeployerApplication(String... args) { @@ -103,8 +107,19 @@ public class DeployerApplication { String getVersion() { Package pkg = DeployerApplication.class.getPackage(); - return (pkg != null ? pkg.getImplementationVersion() == null ? DEFAULT_VERSION - : pkg.getImplementationVersion() : DEFAULT_VERSION); + return (pkg != null ? pkg.getImplementationVersion() == null ? getDefaultVersion() + : pkg.getImplementationVersion() : getDefaultVersion()); + } + + String getDefaultVersion() { + try (InputStream in = new ClassPathResource("META-INF/cli-version.txt").getInputStream()) { + return StreamUtils.copyToString(in, StandardCharsets.UTF_8); + } + catch (IOException e) { + ReflectionUtils.rethrowRuntimeException(e); + } + // not reachable since exception rethrown at runtime + return null; } private void launch() { diff --git a/spring-cloud-launcher/spring-cloud-launcher-deployer/src/main/resources/META-INF/cli-version.txt b/spring-cloud-launcher/spring-cloud-launcher-deployer/src/main/resources/META-INF/cli-version.txt new file mode 100644 index 0000000..73f30eb --- /dev/null +++ b/spring-cloud-launcher/spring-cloud-launcher-deployer/src/main/resources/META-INF/cli-version.txt @@ -0,0 +1 @@ +@project.version@ diff --git a/spring-cloud-launcher/spring-cloud-launcher-deployer/src/test/java/org/springframework/cloud/launcher/deployer/DeployerApplicationTests.java b/spring-cloud-launcher/spring-cloud-launcher-deployer/src/test/java/org/springframework/cloud/launcher/deployer/DeployerApplicationTests.java index 76928ec..f3a350e 100644 --- a/spring-cloud-launcher/spring-cloud-launcher-deployer/src/test/java/org/springframework/cloud/launcher/deployer/DeployerApplicationTests.java +++ b/spring-cloud-launcher/spring-cloud-launcher-deployer/src/test/java/org/springframework/cloud/launcher/deployer/DeployerApplicationTests.java @@ -20,8 +20,7 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; -import static org.hamcrest.CoreMatchers.containsString; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Spencer Gibb @@ -36,28 +35,35 @@ public class DeployerApplicationTests { public void testDefaultLibrary() throws Exception { DeployerApplication wrapper = new DeployerApplication(); if (System.getProperty("project.version") != null) { - assertThat(wrapper.getVersion(), - containsString(System.getProperty("project.version"))); + assertThat(wrapper.getVersion()) + .contains(System.getProperty("project.version")); } } @Test public void testCreateClassLoaderAndListDeployables() throws Exception { new DeployerApplication("--launcher.list=true").run(); - assertThat(output.toString(), containsString("configserver")); + assertThat(output.toString()).contains("configserver"); } @Test public void testNonOptionArgsPassedDown() throws Exception { new DeployerApplication("--launcher.list=true", "--spring.profiles.active=test") .run(); - assertThat(output.toString(), containsString("foo")); + assertThat(output.toString()).contains("foo"); } @Test public void testInvalidDeployableFails() throws Exception { new DeployerApplication("--launcher.deploy=foo,bar").run(); - assertThat(output.toString(), - containsString("The following are not valid: 'foo,bar'")); + assertThat(output.toString()) + .contains("The following are not valid: 'foo,bar'"); + } + + @Test + public void defaultVersionReadFromFile() { + String defaultVersion = new DeployerApplication("--launcher.deploy=foo,bar").getDefaultVersion(); + // starts with one or more digits then a . + assertThat(defaultVersion).isNotBlank().containsPattern("^\\d+\\..*"); } }