java - Maven如何设置java中可见的属性

标签 java spring maven

这是我在 pom.xml 中的配置文件:

<!-- Production Profile -->
    <profile>
        <id>production</id>
        <activation>
            <property>
                <name>profile</name>
                <value>prod</value>
            </property>
        </activation>
        <properties>
            <build.profile.id>prod</build.profile.id>
        </properties>

    </profile>

    <!-- Development Profile -->
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>dev</build.profile.id>
        </properties>
    </profile>

    <!-- Integration Test Profile -->
    <!-- Use mvn -Dprofile=test -->
    <profile>
        <id>integration-test</id>
        <activation>
            <property>
                <name>profile</name>
                <value>test</value>
            </property>
        </activation>
        <properties>
            <!-- Used to locate the profile specific configuration file -->
            <build.profile.id>test</build.profile.id>
            <!-- Only integration tests are run. -->
            <skip.integration.tests>false</skip.integration.tests>
            <skip.unit.tests>true</skip.unit.tests>
        </properties>
    </profile>

接下来在我的 spring 上下文配置文件中我有:

@PropertySource("classpath:properties/app-${profile:dev}.properties")

所以这些技巧允许我使用不同的配置运行我的项目。我还有 3 个配置属性文件,该文件中有:

spring.profiles.active=integration-test
spring.profiles.active=prod
spring.profiles.active=dev

这个配置完美。 Activity 配置文件按照我的预期进行了更改,例如,当我运行 mvn clean install -Dprofile=test 时,我正在使用 app-test.properties 文件,并且我的 Activity 配置文件为 集成测试

但是这个技巧有一个问题。当有人运行 mvn install -Pintegration-test 时, Activity 配置文件是 dev 而不是integration-test。所以可以运行mvn install -Pintegration-test然后设置属性配置文件来测试??

最佳答案

您不应该在每个环境中使用 Maven 配置文件。使用 Maven,您可以构建 jar/war/ear。不同的环境不应该导致另一个 Artifact 。因此,您应该将配置拉到此 Artifact 之外。 How to pass value to maven pom.xml at rum time from java file?描述了如何解决这个问题的很多选项。

关于java - Maven如何设置java中可见的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22080489/

相关文章:

java - 在Java中执行系统命令将其打印到控制台

spring - 如何在没有 web.xml 的情况下将 Struts2 添加到 Web 应用程序?

java - 使用 Maven 部署 Tomcat

java - 如何使用 maven-jar-plugin 和 groovy 构建可执行 jar?

java - 无法在 Spring Boot 中运行任何单元测试

java - Android 文本,文本顶部/底部带有 Spannable 图像

java - BorderLayout.CENTER 内的 JScrollPane 不调整大小

java - 跳跃列表中的随机级别函数

java.lang.NoSuchMethodError : org. hibernate.SessionFactory.openSession()Lorg/hibernate/Session;

java - 在哪里可以找到 jpa 2.1 osgi 包