Skip to content

Commit 39318d1

Browse files
committed
Merge branch '3.5.x'
Closes gh-47167
2 parents 08afe86 + 700904c commit 39318d1

File tree

10 files changed

+199
-55
lines changed

10 files changed

+199
-55
lines changed

build-plugin/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AotTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Stephane Nicoll
3838
* @author Andy Wilkinson
3939
* @author Scott Frederick
40+
* @author Moritz Halbritter
4041
*/
4142
@ExtendWith(MavenBuildExtension.class)
4243
class AotTests {
@@ -102,6 +103,15 @@ void whenAotRunsWithArgumentsSourcesAreGenerated(MavenBuild mavenBuild) {
102103
});
103104
}
104105

106+
@TestTemplate
107+
void whenAotRunsWithSystemPropertiesSourcesAreGenerated(MavenBuild mavenBuild) {
108+
mavenBuild.project("aot-system-properties").goals("package").execute((project) -> {
109+
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
110+
assertThat(collectRelativePaths(aotDirectory.resolve("sources")))
111+
.contains(Path.of("org", "test", "TestProfileConfiguration__BeanDefinitions.java"));
112+
});
113+
}
114+
105115
@TestTemplate
106116
void whenAotRunsWithJvmArgumentsSourcesAreGenerated(MavenBuild mavenBuild) {
107117
mavenBuild.project("aot-jvm-arguments").goals("package").execute((project) -> {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.maven.it</groupId>
6+
<artifactId>aot-system-properties</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>@java.version@</maven.compiler.source>
11+
<maven.compiler.target>@java.version@</maven.compiler.target>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>@project.groupId@</groupId>
17+
<artifactId>@project.artifactId@</artifactId>
18+
<version>@project.version@</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>process-aot</goal>
23+
</goals>
24+
<configuration>
25+
<systemPropertyVariables>
26+
<spring.profiles.active>abc</spring.profiles.active>
27+
</systemPropertyVariables>
28+
</configuration>
29+
</execution>
30+
</executions>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot</artifactId>
38+
<version>@project.version@</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>jakarta.servlet</groupId>
42+
<artifactId>jakarta.servlet-api</artifactId>
43+
<version>@jakarta-servlet.version@</version>
44+
<scope>provided</scope>
45+
</dependency>
46+
</dependencies>
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.test;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Import;
22+
23+
@Configuration(proxyBeanMethods = false)
24+
@Import(TestProfileConfiguration.class)
25+
public class SampleApplication {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(SampleApplication.class, args);
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.test;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Profile;
22+
23+
@Configuration(proxyBeanMethods = false)
24+
@Profile("abc")
25+
class TestProfileConfiguration {
26+
27+
@Bean
28+
public String abc() {
29+
return "abc";
30+
}
31+
32+
}

build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Map.Entry;
2829
import java.util.Set;
2930
import java.util.stream.Collectors;
3031

@@ -39,6 +40,7 @@
3940
import org.jspecify.annotations.Nullable;
4041

4142
import org.springframework.boot.loader.tools.FileUtils;
43+
import org.springframework.util.StringUtils;
4244

4345
/**
4446
* Base class to run a Spring Boot application.
@@ -310,17 +312,20 @@ private Map<String, String> determineEnvironmentVariables() {
310312
* @return a {@link RunArguments} defining the JVM arguments
311313
*/
312314
protected RunArguments resolveJvmArguments() {
313-
StringBuilder stringBuilder = new StringBuilder();
315+
List<@Nullable String> arguments = new ArrayList<>();
314316
if (this.systemPropertyVariables != null) {
315-
stringBuilder.append(this.systemPropertyVariables.entrySet()
316-
.stream()
317-
.map((e) -> SystemPropertyFormatter.format(e.getKey(), e.getValue()))
318-
.collect(Collectors.joining(" ")));
317+
for (Entry<String, String> systemProperty : this.systemPropertyVariables.entrySet()) {
318+
String argument = SystemPropertyFormatter.format(systemProperty.getKey(), systemProperty.getValue());
319+
if (StringUtils.hasText(argument)) {
320+
arguments.add(argument);
321+
}
322+
}
319323
}
320324
if (this.jvmArguments != null) {
321-
stringBuilder.append(" ").append(this.jvmArguments);
325+
String[] jvmArguments = RunArguments.parseArgs(this.jvmArguments);
326+
arguments.addAll(Arrays.asList(jvmArguments));
322327
}
323-
return new RunArguments(stringBuilder.toString());
328+
return new RunArguments(arguments);
324329
}
325330

326331
private void addJvmArgs(List<String> args) {
@@ -427,21 +432,4 @@ private void logArguments(String name, String[] args) {
427432
}
428433
}
429434

430-
/**
431-
* Format System properties.
432-
*/
433-
static class SystemPropertyFormatter {
434-
435-
static String format(@Nullable String key, @Nullable String value) {
436-
if (key == null) {
437-
return "";
438-
}
439-
if (value == null || value.isEmpty()) {
440-
return String.format("-D%s", key);
441-
}
442-
return String.format("-D%s=\"%s\"", key, value);
443-
}
444-
445-
}
446-
447435
}

build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CommandLineBuilder.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.Map.Entry;
2425
import java.util.Objects;
2526

2627
import org.jspecify.annotations.Nullable;
2728

29+
import org.springframework.util.StringUtils;
30+
2831
/**
2932
* Helper class to build the command-line arguments of a java process.
3033
*
@@ -57,10 +60,12 @@ CommandLineBuilder withJvmArguments(String... jvmArguments) {
5760

5861
CommandLineBuilder withSystemProperties(@Nullable Map<String, String> systemProperties) {
5962
if (systemProperties != null) {
60-
systemProperties.entrySet()
61-
.stream()
62-
.map((e) -> SystemPropertyFormatter.format(e.getKey(), e.getValue()))
63-
.forEach(this.options::add);
63+
for (Entry<String, String> systemProperty : systemProperties.entrySet()) {
64+
String option = SystemPropertyFormatter.format(systemProperty.getKey(), systemProperty.getValue());
65+
if (StringUtils.hasText(option)) {
66+
this.options.add(option);
67+
}
68+
}
6469
}
6570
return this;
6671
}
@@ -90,21 +95,4 @@ List<String> build() {
9095
return commandLine;
9196
}
9297

93-
/**
94-
* Format System properties.
95-
*/
96-
private static final class SystemPropertyFormatter {
97-
98-
static String format(@Nullable String key, @Nullable String value) {
99-
if (key == null) {
100-
return "";
101-
}
102-
if (value == null || value.isEmpty()) {
103-
return String.format("-D%s", key);
104-
}
105-
return String.format("-D%s=\"%s\"", key, value);
106-
}
107-
108-
}
109-
11098
}

build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Arrays;
2020
import java.util.Deque;
2121
import java.util.LinkedList;
22-
import java.util.Objects;
2322

2423
import org.codehaus.plexus.util.cli.CommandLineUtils;
2524
import org.jspecify.annotations.Nullable;
@@ -39,9 +38,18 @@ class RunArguments {
3938
this(parseArgs(arguments));
4039
}
4140

42-
RunArguments(String[] args) {
41+
@SuppressWarnings("NullAway") // Maven can't handle nullable arrays
42+
RunArguments(@Nullable String[] args) {
43+
this((args != null) ? Arrays.asList(args) : null);
44+
}
45+
46+
RunArguments(@Nullable Iterable<@Nullable String> args) {
4347
if (args != null) {
44-
Arrays.stream(args).filter(Objects::nonNull).forEach(this.args::add);
48+
for (String arg : args) {
49+
if (arg != null) {
50+
this.args.add(arg);
51+
}
52+
}
4553
}
4654
}
4755

@@ -53,7 +61,7 @@ String[] asArray() {
5361
return this.args.toArray(new String[0]);
5462
}
5563

56-
private static String[] parseArgs(@Nullable String arguments) {
64+
static String[] parseArgs(@Nullable String arguments) {
5765
if (arguments == null || arguments.trim().isEmpty()) {
5866
return NO_ARGS;
5967
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.maven;
18+
19+
import org.jspecify.annotations.Nullable;
20+
21+
/**
22+
* Format System properties.
23+
*
24+
* @author Phillip Webb
25+
* @author Stephane Nicoll
26+
*/
27+
final class SystemPropertyFormatter {
28+
29+
private SystemPropertyFormatter() {
30+
}
31+
32+
static String format(@Nullable String key, @Nullable String value) {
33+
if (key == null) {
34+
return "";
35+
}
36+
if (value == null || value.isEmpty()) {
37+
return String.format("-D%s", key);
38+
}
39+
return String.format("-D%s=%s", key, value);
40+
}
41+
42+
}

build-plugin/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/CommandLineBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void buildWithNullSystemPropertyIsIgnored() {
7676
@Test
7777
void buildWithSystemProperty() {
7878
assertThat(CommandLineBuilder.forMainClass(CLASS_NAME).withSystemProperties(Map.of("flag", "enabled")).build())
79-
.containsExactly("-Dflag=\"enabled\"", CLASS_NAME);
79+
.containsExactly("-Dflag=enabled", CLASS_NAME);
8080
}
8181

8282
@Test

build-plugin/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/SystemPropertyFormatterTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
import org.springframework.boot.maven.AbstractRunMojo.SystemPropertyFormatter;
22-
2321
import static org.assertj.core.api.Assertions.assertThat;
2422

2523
/**
26-
* Tests for {@link AbstractRunMojo.SystemPropertyFormatter}.
24+
* Tests for {@link SystemPropertyFormatter}.
2725
*/
2826
class SystemPropertyFormatterTests {
2927

@@ -39,7 +37,7 @@ void parseOnlyKey() {
3937

4038
@Test
4139
void parseKeyWithValue() {
42-
assertThat(SystemPropertyFormatter.format("key1", "value1")).isEqualTo("-Dkey1=\"value1\"");
40+
assertThat(SystemPropertyFormatter.format("key1", "value1")).isEqualTo("-Dkey1=value1");
4341
}
4442

4543
@Test
@@ -49,7 +47,7 @@ void parseKeyWithEmptyValue() {
4947

5048
@Test
5149
void parseKeyWithOnlySpaces() {
52-
assertThat(SystemPropertyFormatter.format("key1", " ")).isEqualTo("-Dkey1=\" \"");
50+
assertThat(SystemPropertyFormatter.format("key1", " ")).isEqualTo("-Dkey1= ");
5351
}
5452

5553
}

0 commit comments

Comments
 (0)