Maven:不同配置文件的战争生成和集成测试

Maven:不同配置文件的战争生成和集成测试,maven,Maven,在通过所有集成测试之后,我正在尝试为生产环境生成应用程序的war文件 这是我的pom.xml: <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.

在通过所有集成测试之后,我正在尝试为生产环境生成应用程序的war文件

这是我的pom.xml:

<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>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <!-- Profile configuration -->
    <profiles>
        <!-- The configuration of the development profile -->
        <profile>
            <id>dev</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
                    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 production profile -->
        <profile>
            <id>prod</id>
            <properties>
                <build.profile.id>prod</build.profile.id>
            </properties>
        </profile>
        <!-- The configuration of the test profile -->
        <profile>
            <id>integration-test</id>
            <properties>
                <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>

    <build>

        <filters>
            <!--
                Ensures that the config.properties file is always loaded from the
                configuration directory of the active Maven profile.
            -->
            <filter>utils/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>
                <excludes>
                    <exclude>**/*.xlt</exclude> <!-- maven corrupt template files otherwise -->
                </excludes>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xlt</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- 
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                     -->
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <packagingExcludes>%regex[css/(?!styles).*.css]</packagingExcludes> <!-- Include, separated by comma, with JS minified: app/**/*.js -->
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <!-- Plugin to execute unit tests -->
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.20.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 to execute integration tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20.1</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 to add extra directories to search during the execution of tests -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <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>
        </plugins>
    </build>

</project>

4.0.0
0.0.1-快照
战争
发展
发展
真的
假的
戳
戳
集成测试
集成测试
假的
真的
utils/profiles/${build.profile.id}/config.properties
真的
src/main/resources
**/*.xlt
假的
src/main/resources
**/*.xlt
org.apache.maven.plugins
maven编译器插件
3.5.1
1.8
1.8
org.apache.maven.plugins
maven战争插件
2.6
%正则表达式[css/(?!样式)。*.css]
网络内容
假的
org.apache.maven.plugins
maven surefire插件
2.20.1
${skip.unit.tests}
**/IT*.java
org.apache.maven.plugins
maven故障保护插件
2.20.1
集成测试
集成测试
验证
${skip.integration.tests}
org.codehaus.mojo
构建助手maven插件
3.0.0
添加集成测试源
生成测试源
添加测试源
src/集成测试/java
添加集成测试资源
生成测试资源
添加测试资源
真的
src/集成测试/资源
正如您所见,我使用了两种不同的配置文件:“prod”和“integrationtest”

如果我执行:
mvn verify-pinintegration test
,则执行整个过程,触发集成测试并生成war文件。问题是,当我使用“集成测试”概要文件时(因为我使用不同的config.properties来执行集成测试),最终的war文件包含的属性文件的值错误,它们具有“集成测试”属性而不是“生产”属性

如果我手动激活“生产”配置文件(使用Eclipse)并执行:
mvn war:war
,则成功生成生产的war文件,但不会触发集成测试

我要做的是将这两个步骤结合在一个单独的步骤中,我的意思是,执行整个过程,使用“集成测试”配置文件执行集成测试,并使用“生产”配置文件生成war文件

我觉得我错过了一些关于Maven的东西,希望你能指导我

谢谢。

您可以在产品配置文件中插入
false
。如果不希望每次使用prod概要文件构建时都执行集成测试,可以在命令行上使用

mvn install -Pprod -Dskip.integration.tests=false
您可以在产品配置文件中插入
false
。如果不希望每次使用prod概要文件构建时都执行集成测试,可以在命令行上使用

mvn install -Pprod -Dskip.integration.tests=false

集成测试使用一个特殊的config.properties文件,该文件与生产环境的config.properties文件不同,请检查“我的”部分。这样,集成测试只能通过配置文件“集成测试”成功执行,我无法将“prod”配置文件用于当前配置。作为一个临时解决方案,我正在使用概要文件“集成测试”运行“验证”,如果构建成功,则使用概要文件“prod”和“skip.integration.tests=true”执行“验证”。我的目的是一步完成最终构建集成测试使用一个特殊的config.properties文件,该文件与生产环境的config.properties文件不同,请检查“我的”部分。这样,集成测试就只需要简单的测试