简体   繁体   中英

Dealing with per-developer resources such as database.properties or log4j.properties in a java project

We are 4 developers working on a maven-based java web project . We each have a specific database.properties file that points to a per-developer database schema.

The problem we currently have is that we sometimes inadvertently commit our per-developper database.properties or log4j.properties files with the obvious issue that this causes.

On the other hand, if we drop those files into the /lib directory of Tomcat, we might forget to include them to the production application that gets shipped.

Can anyone please provide advice or suggest best practices in order to sort this problem?

You should start using Maven Profiles and Maven Resource Filtering together.

If you combine these two techniques then each user could have his profile defined in the %USER_HOME%/.m2/settings.xml file while the database.properties and log4j.properties files use resource filtering to get the user specific properties from the user profile properties.

I've been using this technique to support different databases by setting up a structure like this:

src
  |-main
       |-filters
       |       |-derby.properties
       |       |-h2.properties
       |       |-mssql.properties
       |       |-mysql.properties
       |-templates
                 |-db.properties

And in my pom.xml

<profiles>
    <profile>
        <id>derby</id>
        <build>
            <filters>
                <filter>src/main/filters/derby.properties</filter>
            </filters>
            <resources>
                <resource>
                    <directory>src/main/templates</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>mysql</id>
        <build>
            <filters>
                <filter>src/main/filters/mysql.properties</filter>
            </filters>
            <resources>
                <resource>
                    <directory>src/main/templates</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>mssql</id>
        <build>
            <filters>
                <filter>src/main/filters/mssql.properties</filter>
            </filters>
            <resources>
                <resource>
                    <directory>src/main/templates</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>h2</id>
        <build>
            <filters>
                <filter>src/main/filters/h2.properties</filter>
            </filters>
            <resources>
                <resource>
                    <directory>src/main/templates</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

You should be able to do the same thing with your files.

使用Git,只有在清除本地引用时才推送到主服务器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM