0
votes

I have the following deployed structure:

├── bin
│   ├── stop.sh
│   └── start.sh
├── config
│   ├── application-dev.properties
│   ├── application-local.properties
│   ├── application.properties
│   └── logback.xml
├── lib
    ├── myjar.jar
|__ logs

....

My start script is like:

jar -jar ../lib/myjar.jar --spring.profiles.active=dev --spring.config.location=file:./../config/

The active profile is picked up but it seems the spring.config.location is ignored and is taken from inside the packaged jar.

I've read all about external configuration here - https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html but and have tried some variations, including the classpath e.g)

--spring.config.location=file:./../config/,classpath:/

But it just doesn't work. I've also tried using the option with -D and that doesn't work either.

Thanks for any help

1

1 Answers

-1
votes

Command run from jar with parametr:

java -jar -Dfile.location=myfile.properties your_app.jar

In your application you can get value:

System.getProperty("file.location");

And full example class configuration:

@Configuration
@ComponentScan
public class MyConfig{

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties2() throws IOException {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        PropertiesFactoryBean properties = new PropertiesFactoryBean();

        Resource resource = new FileSystemResource(System.getProperty("file.location"));

        properties.setLocation(resource);
        propertySourcesPlaceholderConfigurer.setProperties(properties.getObject());
        return propertySourcesPlaceholderConfigurer;
    }

}

In classes (@Component and extended it) you can use variables from properties file:

@Value("${myperty.host}")
private String host;