具有不同配置文件的Maven配置文件 [英] Maven Profile with different config files

查看:130
本文介绍了具有不同配置文件的Maven配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目结构如下所示:

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

这样定义了一个maven个人资料

<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>

我要做什么是在触发正确的配置文件时从正确的子文件夹复制正确的属性,例如 mvn -pdev install .将让Maven从 src/main/resource/dev 文件夹复制 app.properties 文件,然后将其放入要构建的jar

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

问题是5个带有单个属性文件的文件夹,我不希望dev prod沙箱文件夹.

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

推荐答案

邓尼的答案会起作用,但我是这样做的

dunni's answer will work but I did this way

  1. 将子文件夹放到一个名为src/main/profiles的文件夹中,如果您使用的是eclipse,则重要的是将此文件夹添加为源文件夹". 所以项目看起来像这样

  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/resource

-src/main/profiles

-src/main/profiles

-prod
-dev 
-ist

注意:您可以将所有常见且永不更改的东西放在所有配置文件中,并将其放置在资源文件夹中,等等log4j.properties 然后将特定于配置文件的文件分别放入每个子文件夹中.

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>

之所以这样做,是因为默认情况下Maven试图将/resource文件夹中的所有内容复制到jar中.

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

但是使用jar插件我可以得到我想要的东西,但是我试图保留我的 pom简单.

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

我在那里遇到的主要问题是如何从/resource文件夹中排除文件.如果有人知道更好的方法,请发表评论

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

这篇关于具有不同配置文件的Maven配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆