0

I have a maven project that uses spring and I would like to insert a filename into Spring whose contents has various database information. The filename can either be test.properties, prod.properties or dev.properties. I have seen posted that one can type

"mvn install -DfileTarget=test.properties"

and have the system property be set when a user builds project. However I am getting the following error when I deploy the project on Tomcat 8.

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'fileTarget' in string value "file:${jamesTarget}"

My properties (test, prod, dev) files contains DB related config values:

db.driver = jdbc:\\.....
db.url = url of database.
db.user = username
db.passwd = passwd

My Java code is as follows. Is there something I need to add into the pom.xml? It seems like when deploying on Tomcat, the system property is not found, how would I set this leaving the ability for me to install different files at build time?

Thanks in advance

@Configuration
@PropertySource({ "classpath:${fileTarget}" })
public class DbConfig {
   @Autowired
   private Environment env;

   @Bean
   public DataSource dataSource() {

     System.out.println("driver : " + env.getProperty("db.driver"));
     System.out.println("driver : " + env.getProperty("db.driver"));
     System.out.println("user : " + env.getProperty("db.user"));
     System.out.println("passwd : " + env.getProperty("db.passwd"));
   }

...

John
  • 123
  • 1
  • 11
  • Leaving aside maven property substitution on source files, is there really a guarantee that `mvn install` will be run in each of the envs you said to ensure a system property is set on those envs? You setting a system property when you run `mvn install` locally or on a CI system does not mean the property will be set on all the systems/environments that your application is deployed to. – mystarrocks Nov 15 '14 at 02:17
  • Why aren't you just loading a file from a default location next to one from the class path(the default) this would allow you to specify the production ready properties inside the application and allow to override for each environment you like, without messing around with a environment property. – M. Deinum Nov 15 '14 at 08:53

0 Answers0