Pass values into spring from maven install -Denv=value, deploy on Tomcat

728 views Asked by At

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"));
   }

...

0

There are 0 answers