0

On my local machine I am creating Java EE Maven project on Tomcat7 that collaborates with local MySQL database.

For now I have all db connection settings like (user, password, host, dbname, ... etc) hardcoded in DatabaseQueries.java directly in Java EE Maven project, but I know it is not right and I want to take out this setting from project for example to db.properties file, so when I deploy the project on my remote Tomcat7 server it will use different db.properties file with corresponing settings or if I share the project in GitHub, other people will not see my db username and password.

Thus I have few questions:

  1. What is the best practice of externalizing the db settings from Maven project ?

  2. Does Maven project has already build-in functionality to do this ?

  3. How to be sure, that when I export a project as a *.WAR file it will not include this db.properties file ?

  4. Is it correct to use the name as "db.properties" for this purposes ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Renat Gatin
  • 6,053
  • 5
  • 37
  • 58

2 Answers2

2

For example , in your project add 4 folders :

  1. Your Project\src\main\resources\

    \local > db.properties
    \integration > db.properties
    \deploy > db.properties
    \Default > db.properties
    
  2. in pom.xml add :

     <properties>
         <param>Default</param>
     </properties>
    

    and

     <build>
         <resources>
             <resource>
                 <directory>src/main/resources/${param}</directory>           
             </resource>
         </resources>
     </build> 
    
     if : mvn clean install   : classpath => db.properties(from Default)
    
     if : mvn clean install -Dparam=local : classpath => db.properties(from local)
    
     if : mvn clean install -Dparam=integration : classpath => db.properties(from integration)
    
     if : mvn clean install -Dparam=deploy : classpath => db.properties(from deploy)
    

Much better than using profiles is more extensible without touching the pom.

halfer
  • 19,824
  • 17
  • 99
  • 186
question_maven_com
  • 2,457
  • 16
  • 21
1

In your case just local and remote :

    local/db.properties
            user=aaaa
            password=aaaa
            host=127.0.0.0

    remote/db.properties
            user=rrrrr
            password=rrrrr
            host=1.2.3.4
halfer
  • 19,824
  • 17
  • 99
  • 186
question_maven_com
  • 2,457
  • 16
  • 21