I am using Apache Maven and Spring. In the src/main/resources folder I have a properties file. These property values can have different values.
I am using PropertyPlaceholderConfigurer.
@Configuration
public class ResourceConfig {
@Bean
public PropertyPlaceholderConfigurer properties( ) {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
ppc.setLocations(new ClassPathResource[] {new ClassPathResource("propertiesFile"), new ClassPathResource("myapp.properties")});
return ppc;
}
}
I replace these values at runtime:
@Configuration
public class DataSourceConfig {
@Value("${jdbc.url}")
private String jdbcUrlDefault;
// there are more @Value
}
This is just a sample. I have a main method:
public static void main(String[] args) {
// accept a properties file and override those values defined in DataSourceConfig class
ApplicationContext application = new AnnotationConfigApplicationContext("packaageThatContainsConfiguration");
DataSourceConfig dataSourceConfig = (DataSourceConfig) application.getBean("dataSourceConfig");
System.out.println(dataSourceConfig.getJdbcUrlDefault());
}
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>server-app</finalName>
</build>
When Apache Maven builds the application the properties file will be on the classpath. I want to override the properties value with the properties file provided.
I want to accept a properties file through the main program that gets replaced, so the Spring side starts the PropertyPlaceholderConfigurer.
I have a properties file containing the value that I want.
When I run the following command on the Command prompt:
java -jar server-app-jar-with-dependencies.jar -D myapp.properties "C:\Users\user\Desktop\myapp.properties"
It prints out the original value that is placed in src/main/resources/myapp.properties
I want to print the value that is on the Desktop myapp.properties.
What am I doing wrong?