Search code examples
javamavenmaven-profiles

How to use different config files for different environments in a java maven vaadin project?


I want to setup different build profiles in a vaadin based maven application (war file), which should use different config files, e.g. for database config. The simplest would be to replace the dev config file with the prod config file.

I tried to achieve this with maven-profiles, I already looked through a lot of solutions, but I don't know how to name my directories according to the profiles, because my properties file is in a different place.

My application uses the standard maven directory layout for java files and resources, but the config files are in some subfolder hierarchy:

/myvaadinapp/src/main/resources/xxx/xxx/xxx/myapp/project/xxxx/db/config/config.properties

Question: Where do I have to create the profiles folder and where do I put the different config files?

My profile config:

<profiles>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <build.profile.id>prod</build.profile.id>
            </properties>
        </profile>
    </profiles>

Solution

  • Add these in your pom:

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/profiles/${build.profile.id}</directory>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
    </build>
    

    If you use prod profile, put the prod profile related resource files in src/main/profiles/prod directory and put common resource files for all profiles in src/main/resources directory.