From 4f91059766bbf698aeb4c3225c25ea5de65723f2 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 15 Oct 2020 08:28:51 +0200 Subject: [PATCH] #152 - Fix IoProperties.javaHome setter. We now correctly set the javaHome variable instead of the workDir if the io.javaHome property is set. Include the property in the template file and add the maven.parallelize=true flag as well. Closes: #152 --- application-local.template | 7 ++ .../data/release/io/IoProperties.java | 21 +++++- .../release/io/IoPropertiesUnitTests.java | 74 +++++++++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/springframework/data/release/io/IoPropertiesUnitTests.java diff --git a/application-local.template b/application-local.template index 277a68c..a40e2a0 100644 --- a/application-local.template +++ b/application-local.template @@ -5,8 +5,15 @@ git.email= git.password= github.api.url=https://api.github.com +# IO + +# Optionally set the JavaHome path used for exeuting maven goals (eg. /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/) +# If the property does not exist, is empty or points to an invalid directory the OS default JavaHome will be used. +#io.javaHome= + # Maven maven.mavenHome= +maven.parallelize=true # Deployment deployment.repository-prefix= diff --git a/src/main/java/org/springframework/data/release/io/IoProperties.java b/src/main/java/org/springframework/data/release/io/IoProperties.java index 3aed34c..6e0eec2 100644 --- a/src/main/java/org/springframework/data/release/io/IoProperties.java +++ b/src/main/java/org/springframework/data/release/io/IoProperties.java @@ -22,10 +22,12 @@ import java.io.File; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; /** * @author Oliver Gierke * @author Mark Paluch + * @author Christoph Strobl */ @Slf4j @Data @@ -37,13 +39,26 @@ class IoProperties { public void setWorkDir(String workDir) { - log.info(String.format("Using %s as working directory!", workDir)); + log.info(String.format("🔧 Using %s as working directory!", workDir)); this.workDir = new File(workDir.replace("~", System.getProperty("user.home"))); } public void setJavaHome(String javaHome) { - log.info(String.format("Using %s as Java home!", javaHome)); - this.workDir = new File(javaHome.replace("~", System.getProperty("user.home"))); + if (!StringUtils.hasText(javaHome)) { + return; + } + + File javaHomeDir = new File(javaHome.replace("~", System.getProperty("user.home"))); + + if (!javaHomeDir.isDirectory()) { + log.warn(String.format( + "⚠️️ Property 'io.javaHome' does not point to a vaild directory ('%s')! Falling back to os.default.", + javaHomeDir.getPath())); + return; + } + + log.info(String.format("🔧 Setting javaHome to: '%s'.", javaHomeDir.getPath())); + this.javaHome = javaHomeDir; } } diff --git a/src/test/java/org/springframework/data/release/io/IoPropertiesUnitTests.java b/src/test/java/org/springframework/data/release/io/IoPropertiesUnitTests.java new file mode 100644 index 0000000..0b7259e --- /dev/null +++ b/src/test/java/org/springframework/data/release/io/IoPropertiesUnitTests.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 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. + */ +package org.springframework.data.release.io; + +import static org.assertj.core.api.Assertions.*; + +import java.io.File; +import java.io.IOException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * @author Christoph Strobl + */ +public class IoPropertiesUnitTests { + + @TempDir File tempDir; + IoProperties properties; + + @BeforeEach + void beforeEach() { + properties = new IoProperties(); + } + + @Test // GH-152 + void setJavaHomeToDir() { + + properties.setJavaHome(tempDir.getPath()); + + assertThat(properties.getJavaHome()).isNotNull().isDirectory(); + } + + @Test // GH-152 + void setJavaHomeIgnoresEmptyString() { + + properties.setJavaHome(""); + + assertThat(properties.getJavaHome()).isNull(); + } + + @Test // GH-152 + void setJavaHomeIgnoresFile() throws IOException { + + File f = new File(tempDir, "java.txt"); + f.createNewFile(); + + properties.setJavaHome(f.getPath()); + + assertThat(properties.getJavaHome()).isNull(); + } + + @Test // GH-152 + void setJavaHomeIgnoresDirectoryThatDoesNotExist() throws IOException { + + properties.setJavaHome(new File(tempDir, "java-bin").getPath()); + + assertThat(properties.getJavaHome()).isNull(); + } +}