Maven 多配置文件部署在 WEB-INF/classes 中

发布于 2024-12-11 20:30:26 字数 339 浏览 0 评论 0原文

我需要在我的最后一场战争中包含一些内容:

我在 src/main/resources 中遇到了多个配置文件文件的情况:

 configuration.properties (local)
 configuration.dev.properties
 configuration.production.properties

所以,我希望当执行“-PProduction”时,文件配置.生产.属性是复制到 WEB-INF/classes 目录的 war 中并重命名为“configuration.properties”。我怎样才能得到它?

谢谢

随机化

I need to include some stuff in my final war:

I have a situation with multi profiles files in the src/main/resources:

 configuration.properties (local)
 configuration.dev.properties
 configuration.production.properties

so, I would like that when for example '-Pproduction' is executed the file configuration.production.properties is copied in the war at the dir WEB-INF/classes and renamed as 'configuration.properties'. How can I get that?

Thanks

Randomize

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一遍 2024-12-18 20:30:26

我不会这样做。

相反,我将有一个configuration.properties,保存根据配置文件不同的值的占位符:

numberOfThreads=${config.numberOfThreads} # depends on the profile
foo=bar #doesnt depend on the profile

然后使用资源插件的过滤功能,以便用从配置文件中获取的实际值替换占位符

filter-dev.properties :
    config.numberOfThreads=2
filter-prod.properties :
    config.numberOfThreads=16

:你的 pom :

<profile>
  <id>dev</id>
  <properties>
    <env>dev</env>
  </properties>
</profile>
<profile>
  <id>prod</id>
  <properties>
    <env>prod</env>
  </properties>
</profile>

<filters>
  <filter>src/main/filters/filter-${env}.properties</filter>
</filters>

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

I would not do it like this.

Instead, I would have a single configuration.properties, holding placeholders for the values that differ depending on the profile :

numberOfThreads=${config.numberOfThreads} # depends on the profile
foo=bar #doesnt depend on the profile

And then use the filtering capabilities of the resources plugin in order to replace the placeholder with actual values fetched from the profile:

filter-dev.properties :
    config.numberOfThreads=2
filter-prod.properties :
    config.numberOfThreads=16

And now in your pom :

<profile>
  <id>dev</id>
  <properties>
    <env>dev</env>
  </properties>
</profile>
<profile>
  <id>prod</id>
  <properties>
    <env>prod</env>
  </properties>
</profile>

<filters>
  <filter>src/main/filters/filter-${env}.properties</filter>
</filters>

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>
情未る 2024-12-18 20:30:26

资源可以包含在每个配置文件的基础上,我认为如果您按环境/配置文件对它们进行分组会更优雅:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>commons-logging.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>${env.resources.dir}</directory>
        </resource>
    </resources>
[...]
</build>
<profiles>
    <profile>
        <id>local</id>
        <properties>                
            <env.resources.dir>src/test/resources</env.resources.dir>
            [...]

我还不知道是否可以包含在每个配置文件的基础上的源(如果可以的话请帮忙 :D)。

Resources can be included on a per-profile basis and I think it would be more elegant if you would group them by environment/profile:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>commons-logging.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>${env.resources.dir}</directory>
        </resource>
    </resources>
[...]
</build>
<profiles>
    <profile>
        <id>local</id>
        <properties>                
            <env.resources.dir>src/test/resources</env.resources.dir>
            [...]

What I don't know yet is if I can include sources on a per profile basis (please help out if you can :D).

心碎无痕… 2024-12-18 20:30:26

你为什么不过滤它们。只有一个具有该名称的文件,您可以执行以下操作:

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

Why don't you filter them. Only one file with the name and you do:

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文