java - Maven集成-测试如何为不同的Profiles正确设置POM

标签 java integration-testing maven-3 maven-plugin maven-failsafe-plugin

我在控制 Maven 如何在 Dev 编译/测试步骤和 Integration-Test 编译/验证步骤期间复制资源文件时遇到问题。

我在 Maven 中创建了两个配置文件:DevIntegration-Test。我为每个配置文件创建了一个 config.properties 文件。在我的 src/test/resources 和 src/integration/resources 位置中,我有一个 myappconfig.properties 文件。我希望将各个配置文件 config.properties 中的正确值覆盖到这些文件中,并在每个配置文件的编译/测试/验证步骤期间复制到目标/测试类(Dev/集成测试)。

在针对 CompileTest 目标运行 Dev 配置文件时,以及针对 Integration- 运行时,我遇到了不需要的行为。在编译目标期间测试配置文件。我在下表中总结了预期和实际行为,因为它比全部写出来更容易。我还提供了 POM 文件列表和来自 Intellij 的屏幕截图。

似乎我需要一种方法来阻止在选择 DEV 配置文件时触发 build-helper-maven-plugin 。有没有办法以与我在运行 DEV 测试时跳过集成测试相同的方式进行设置?或者是其他问题?

config.property 文件的内容

1)profiles/dev/config.properties

server.message=hello_from_dev

2) 配置文件/集成测试/config.properties

server.message=hello_from_integration_test

3) src/test/resources/myappconfig.properties

## 开发测试 config.${build.profile.id}server.message=${server.message}

4) src/integration-test/resources/myappconfig.properties

## 集成测试 config.${build.profile.id}server.message=${server.message}

问题汇总表

enter image description here

POM 列表

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.stackoverflow</groupId>
  <artifactId>mavenintegrationtest</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>mavenintegrationtest</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <filters>
      <!--
                    Ensures that the config.properties file is always loaded from the
                    configuration directory of the active Maven profile.
    
                -->
      <filter>${basedir}/profiles/${build.profile.id}/config.properties</filter>
    </filters>

    <resources>
      <!--
                    Placeholders that are found from the files located in the configured resource
                    directories are replaced with the property values found from the profile
                    specific configuration file.
                -->
      <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
      </resource>
    </resources>

    <testResources>
      <!--
                      <testResource>
                        <filtering>true</filtering>
                        <directory>src/integration-test/resources</directory>
                      </testResource>
                -->
      <testResource>
        <filtering>true</filtering>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>

    <pluginManagement>
      <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>build-helper-maven-plugin</artifactId>
          <version>3.0.0</version>
          <executions>
            <!-- Add executions here -->
            <!-- Add a new source directory to our build -->
            <execution>
              <id>add-integration-test-sources</id>
              <phase>generate-test-sources</phase>
              <goals>
                <goal>add-test-source</goal>
              </goals>
              <configuration>
                <!-- Configures the source directory of our integration tests -->
                <sources>
                  <source>src/integration-test/java</source>
                </sources>
              </configuration>
            </execution>
            <!-- Add a new resource directory to our build -->
            <execution>
              <id>add-integration-test-resources</id>
              <phase>generate-test-resources</phase>
              <goals>
                <goal>add-test-resource</goal>
              </goals>
              <configuration>
                <!-- Configures the resource directory of our integration tests -->
                <resources>
                  <!--
                                            Placeholders that are found from the files located in the configured resource
                                            directories are replaced with the property values found from the profile
                                            specific configuration file.
                                        -->
                  <resource>
                    <filtering>true</filtering>
                    <directory>src/integration-test/resources</directory>
                  </resource>
                </resources>
              </configuration>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
          <configuration>
            <!-- Skips unit tests if the value of skip.unit.tests property is true -->
            <skipTests>${skip.unit.tests}</skipTests>
            <!-- Excludes integration tests when unit tests are run -->
            <excludes>
              <exclude>**/IT*.java</exclude>
            </excludes>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          <version>3.0.0-M3</version>
          <executions>
            <!--
                                Invokes both the integration-test and the verify goals of the
                                Failsafe Maven plugin
                            -->
            <execution>
              <id>integration-tests</id>
              <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
              </goals>
              <configuration>
                <!--
                                        Skips integration tests if the value of skip.integration.tests
                                        property is true
                                    -->
                <skipTests>${skip.integration.tests}</skipTests>
              </configuration>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M3</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>
    </plugins>
  </build>
  <!-- Profile configuration -->
  <profiles>
    <!-- The configuration of the development profile -->
    <profile>
      <id>dev</id>
      <!-- The development profile is active by default -->
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <!--
                        Specifies the build.profile.id property that must be equal than the name of
                        the directory that contains the profile specific configuration file.
                        Because the name of the directory that contains the configuration file of the
                        development profile is dev, we must set the value of the build.profile.id
                        property to dev.
                    -->
        <build.profile.id>dev</build.profile.id>
        <!--
                        Only unit tests are run when the development profile is active
                     -->
        <skip.integration.tests>true</skip.integration.tests>
        <skip.unit.tests>false</skip.unit.tests>
      </properties>
    </profile>
    <!-- The configuration of the integration-test profile -->
    <profile>
      <id>integration-test</id>
      <properties>
        <!--
                        Specifies the build.profile.id property that must be equal than the name of
                        the directory that contains the profile specific configuration file.
                        Because the name of the directory that contains the configuration file of the
                        production profile is prod, we must set the value of the build.profile.id
                        property to prod.
                    -->
        <build.profile.id>integration-test</build.profile.id>
        <!--
                        Only integration tests are run when the integration-test profile is active
                    -->
        <skip.integration.tests>false</skip.integration.tests>
        <skip.unit.tests>true</skip.unit.tests>
      </properties>
    </profile>
  </profiles>

</project>

Intellij 截图

enter image description here enter image description here

最佳答案

我尝试使用 @Jorge Campos 给我的提示来更改我的 POM。我认为它工作正常。请在下面查找它是否可以帮助某人。 谢谢你的建议@Jorge Campos - 确实这些要点应该给你,但我不能给他们评论。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>maven-compilerpluginoverride</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>mavencompilerpluginoverride</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>

    <build>

        <filters>
            <!--
                Ensures that the config.properties file is always loaded from the
                configuration directory of the active Maven profile.
            -->
            <filter>profiles/${build.profile.id}/config.properties</filter>
        </filters>

        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M1</version>
                    <configuration>
                        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
                        <skipTests>${skip.unit.tests}</skipTests>
                        <!-- Excludes integration tests when unit tests are run -->
                        <excludes>
                            <exclude>**/IT*.java</exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>

                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>

        </plugins>

    </build>

    <profiles>
        <!-- The Configuration of the development profile -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!--
                    Specifies the build.profile.id property that must be equal than the name of
                    the directory that contains the profile specific configuration file.
                    Because the name of the directory that contains the configuration file of the
                    development profile is dev, we must set the value of the build.profile.id
                    property to dev.
                -->
                <build.profile.id>dev</build.profile.id>
                <!--
                    Only unit tests are run when the development profile is active
                -->
                <skip.integration.tests>true</skip.integration.tests>
                <skip.unit.tests>false</skip.unit.tests>
            </properties>
            <build>
                <resources>
                    <!--
                        Placeholders that are found from the files located in the configured resource
                        directories are replaced with the property values found from the profile
                        specific configuration file.
                    -->
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
                <testResources>
                    <testResource>
                        <directory>src/test/java</directory>
                        <excludes>
                            <exclude>**/*.java</exclude>
                        </excludes>
                    </testResource>
                    <testResource>
                        <directory>src/integration-test/java</directory>
                        <excludes>
                            <exclude>**/*.java</exclude>
                        </excludes>
                    </testResource>
                    <testResource>
                        <filtering>true</filtering>
                        <directory>src/test/resources</directory>
                    </testResource>
                </testResources>
            </build>
        </profile>
        <!-- The Configuration of the integration-test profile -->
        <profile>
            <id>integration-test</id>
            <properties>
                <!--
                    Specifies the build.profile.id property that must be equal than the name of
                    the directory that contains the profile specific configuration file.
                    Because the name of the directory that contains the configuration file of the
                    integration-test profile is integration-test, we must set the value of the
                    build.profile.id property to integration-test.
                -->
                <build.profile.id>integration-test</build.profile.id>
                <!--
                    Only integration tests are run when the integration-test profile is active
                -->
                <skip.integration.tests>false</skip.integration.tests>
                <skip.unit.tests>true</skip.unit.tests>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/test/</directory>
                        <excludes>
                            <exclude>**/*.java</exclude>
                        </excludes>
                    </resource>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/integration-test/resources</directory>
                    </resource>
                </resources>
                <testResources>
                </testResources>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>3.0.0</version>
                        <executions>
                            <!-- Add executions here -->
                            <!-- Add a new source directory to our build -->
                            <execution>
                                <id>add-integration-test-sources</id>
                                <phase>generate-test-sources</phase>
                                <goals>
                                    <goal>add-test-source</goal>
                                </goals>
                                <configuration>
                                    <!-- Configures the source directory of our integration tests -->
                                    <sources>
                                        <source>src/integration-test/java</source>
                                    </sources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>3.0.0-M1</version>
                        <executions>
                            <!--
                                Invokes both the integration-test and the verify goals of the
                                Failsafe Maven plugin
                            -->
                            <execution>
                                <id>integration-tests</id>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                                <configuration>
                                    <!--
                                        Skips integration tests if the value of skip.integration.tests
                                        property is true
                                    -->
                                    <skipTests>${skip.integration.tests}</skipTests>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>

            </build>
        </profile>
    </profiles>

</project>

screenshot showing the Dev Profile being used

screenshot showing the Integration-Test Profile being used

关于java - Maven集成-测试如何为不同的Profiles正确设置POM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654489/

相关文章:

spring-boot - 为什么我在使用 IgniteRepository 的 deleteAll 方法时遇到问题?

java - 为什么 maven 执行器插件在 maven 版本 3.6.1 时失败,但在 3.6.2 时通过?

java - 在 IntelliJ IDEA 2016.1 中找不到创建新 Maven 项目的选项

java - 允许向导仅从现有项目运行

groovy - 使用 Spock 和 Groovy 作为集成测试确保 REST

c# - 在 AutoFixture 自定义上调用 Dispose 方法

java - 如何测试持久层?

java - 为什么 Spark 运行时内存少于可用内存?

java - Jackson - 使用 self 引用序列化实体

java - 如何将数组发送到数组列表?