Junchen Liu
Junchen Liu

Reputation: 5614

Maven Profile with different config files

my project structure looks like this

-src/main/java
-src/main/resources
 -dev
  -app.properties
 -prod
  -app.properties
 -sandbox
   -app.properties

and I defined a maven profiles like this

<profiles>
    <profile>
      <id>dev</id>
      <properties>
        <build.profile>dev</build.profile>
      </properties>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/dev</directory>
          </resource>
        </resources>
      </build>
    </profile>
    <profile>
      <id>prod</id>
      <properties>
        <build.profile>prod</build.profile>
      </properties>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/prod</directory>
          </resource>
        </resources>
      </build>
    </profile>
    <profile>
      <id>sandbox</id>
      <properties>
        <build.profile>sandbox</build.profile>
      </properties>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/sandbox</directory>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>

what I am trying to do is to copy the right property from the right sub folder, when I triggered the right profile, e.g mvn -pdev install. will let the maven copy the app.properties file from the src/main/resource/dev folder then place into the jar I am building

my jar looks like this:

  +dev
  +prod
  +sandbox
  +META-INF
  +com
  -app.properties

the problem is 5 folders with a single properties file, I don't want the dev prod sandbox folders.

Upvotes: 3

Views: 3524

Answers (2)

Junchen Liu
Junchen Liu

Reputation: 5614

dunni's answer will work but I did this way

  1. putting the sub folders into a folder called src/main/profiles, if you are using eclipse, its important to add this folder as "source folder". so the project looks like this

    -src/main/resource

    -src/main/profiles

    -prod
    -dev 
    -ist
    

Note : you can put anything where is common and never changes cross all profiles into the resource folder, etc log4j.properties then put the profile specific ones into each sub folders respectively.

and the final profile section looks like this

<profiles>
    <profile>
      <id>dev</id>
      <properties>
        <build.profile>dev</build.profile>
      </properties>
      <build>
        <resources>
          <resource>
            <directory>src/main/profiles/dev</directory>
          </resource>
        </resources>
      </build>
    </profile>
    <profile>
      <id>prod</id>
      <properties>
        <build.profile>prod</build.profile>
      </properties>
      <build>
        <resources>
          <resource>
            <directory>src/main/profiles/prod</directory>
          </resource>
        </resources>
      </build>
    </profile>
    <profile>
      <id>sandbox</id>
      <properties>
        <build.profile>sandbox</build.profile>
      </properties>
      <build>
        <resources>
          <resource>
            <directory>src/main/profiles/sandbox</directory>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>

reason I did this way is because Maven by default is trying to copy everything from the /resource folder into the jar.

but using the jar plugin I can get what I want, but I am trying to keep my pom simple.

the main problem I have faced there, is how to excluding files from the /resource folder. please comment if anyone knows better way to do it

Upvotes: 2

dunni
dunni

Reputation: 44515

Maybe you have a plugin which isn't respecting the resource entries in the build section and using src/main/resources as resource folder. In that case i would change the folder structure like this:

src  
 - main  
  - resources-dev
  - resources-prod
  - resources-sandbox

and change the folder names in the build sections. This way you don't interfere with the standard maven folder structure.

Upvotes: 1

Related Questions