The application runs as expected when running as bootRun task in Eclipse IDE, but right clicking on project → Run As → Spring Boot App doesn’t replace the value of the property in the following prototype.
- build.gradle file
import org.apache.tools.ant.filters.ReplaceTokens
plugins {
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'eclipse'
}
group = 'com.sample.auto.expansion'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
// enable auto property expansion for passing gradle property to spring boot
// https://www.baeldung.com/spring-boot-auto-property-expansion
processResources {
duplicatesStrategy = 'include'
with copySpec {
from 'src/main/resources'
include '**/application*.properties'
include '**/application*.yaml'
include '**/application*.yml'
project.properties.findAll().each {
prop ->
if (prop.key != null) {
filter(ReplaceTokens, tokens: [(prop.key): prop.value.toString()])
filter(ReplaceTokens, tokens: [('project.' + prop.key): prop.value.toString()])
}
}
}
}
- gradle.properties
expansion.property=Hello Expansion Property!
- application.properties
com.test.value=@expansion.property@
- DemoApplication.java
package com.sample.auto.expansion.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Value("${com.test.value}")
String expansionProperty;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(expansionProperty);
}
}
The bootRun task results in expected output:
Hello Expansion Property!
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
Running it as Spring Boot App in Eclipse results literal output without the value being replaced.
2021-12-04 21:49:35.598 INFO 27293 --- [ main] c.s.auto.expansion.demo.DemoApplication : Started DemoApplication in 1.097 seconds (JVM running for 2.105)
@expansion.property@
I have also posted this issue in stackoverflow at java - How to pass gradle properties into Spring Boot and debug in Eclipse? - Stack Overflow