如何在Spring配置的应用程序中使用Maven和各种application.properties生成不同测试区域的工件

Cro*_*wie 5 java continuous-integration spring maven

我想使用Maven来处理不同本地和测试区域的工件生成.我相信我可以使用不同的配置文件,但我不确定.

在Maven中,我可以选择不同的目录来选择打包时使用的文件(例如application.properties)吗?我该如何设置?

我想要的是在我的项目中为资源提供以下文件夹

  • 本地
  • 构建服务器
  • 开发
  • SYS

每个文件夹应包含不同版本的application.resources,它是Spring中的一个文件,可用于处理用于变量的硬编码字符串.对于本地构建,我们的开发人员也可以使用不同的操作系统 我是否应该要求我也想在不同的操作系统上实现它.

主要成果是:

  • 从IDE内部控制Maven生命周期阶段(IntelliJ)
  • 不会使阶段和团队流程复杂化
  • 保持每个开发人员的一致性
  • 在运行阶段(例如安装)时,使每个开发人员/区域的不同配置显示为不可见

理想情况下,我会根据最佳实践(Duvall,Matyas,Glover)设置我的项目.

jfc*_*edo 3

如果您使用 Spring boot,有一个简单的方法可以做到这一点。

在maven中创建两个配置文件,并在每个配置文件中设置一个属性,其中包含要执行的Spring配置文件的名称。

    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- Default spring profile to use -->
            <spring.profiles.active>dev</spring.profiles.active>
            <!-- Default environment -->
            <environment>develop</environment>
        </properties>
    </profile>
Run Code Online (Sandbox Code Playgroud)

在 application.properties 中,添加以下属性: spring.profiles.active=${spring.profiles.active}

使用此模式 application-profile.properties 为每个配置文件创建一个 application.property。例如:application-dev.properties application-prod.properties

确保在资源插件中启用过滤:

  ...
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
 ...
Run Code Online (Sandbox Code Playgroud)

另一种方法是在maven执行期间创建一个名为activeprofile.properties的文件。Spring Boot 会查找此文件来加载活动配置文件。您可以按如下方式创建该文件:

   <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <configuration>
                        <target>
                            <echo message="spring.profiles.active=${spring.profiles.active}" file="target/classes/config/activeprofile.properties" />
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)