java - Maven填充属性文件切换配置文件而不在类路径中添加文件

标签 java maven properties classpath environment

我想用取决于环境配置文件(dev、prod...)的值填充属性文件 common.properties,但我不希望将该文件添加到类路径中。

我读了几个主题,例如 read Maven variable from properties file using profile

所以我设法用配置文件填充属性:

<profiles>
    <profile>
    <id>dev</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <build>
        <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>${properties-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>read-project-properties</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>    
                        <configuration>
                            <files>
                                <file>config/common_dev.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    </profile>
</profiles>

<build>
    <resources>
        <resource>
            <directory>config</directory>
            <filtering>true</filtering>
        </resource>
        ...
    </resources>
    ...
</build>

在模块中,我有一个文件夹配置,其中包含文件 common.properties 和 common_dev.properties:

common_dev.properties:
    common.reception.directory.norm = PHASE1V1
common.properties:
    common.reception.directory.norm = ${common.reception.directory.norm}

common.properties 已用 mvn clean install 填充,但 config 目录成为 Eclipse 中的源文件夹,已添加该目录的类路径条目。 这是我的问题,common.properties 文件是在应用程序中获取的,环境变量包含其路径,而不是从类路径中读取。 它有效,但对项目开发人员来说却令人困惑。

我还尝试使用properties-maven-plugin的write-project-properties和write-active-profile-properties,但第一个将我所有的项目属性写入文件中,第二个不写入任何内容,因为read-project-属性将属性读取为项目属性而不是配置文件属性。

有办法做我想做的事吗?

最佳答案

您可以尝试使用 maven eclipse 插件来专门配置 eclipse 以排除特定的文件夹/文件。

关于java - Maven填充属性文件切换配置文件而不在类路径中添加文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13762955/

相关文章:

java - Android 如何将 Bundle 发送到另一个 Activity?

java - 循环永远不会开始

java - 使用 AspectJ 编译器而不是 Javac 编译时出错

java - Spring Boot 中的外部 YAML 属性文件和普通属性文件

java - 显示某个数组列表,点击某个按钮

java - 加载 Jar 外部的文件

java - 如何使用 maven-jar-plugin 创建带有 .class 文件的测试 jar?

java - Spring Boot 应用程序在 IDE 中运行时工作,但当部署为 war 文件时,请求失败并出现 500 错误

java - 如何在 java 中加载一次属性文件并在整个应用程序中使用它

ios - 属性另一个UIViewController为null