Maven:具有不同轮廓的战争生成和集成测试

时间:2017-09-19 15:47:20

标签: 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.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>

如您所见,我使用了两种不同的配置文件:'prod'和'integration-test'

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

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

我要做的是在一个步骤中结合这两个步骤,我的意思是,执行整个过程以使用'integration-test'配置文件执行集成测试,并使用'production'生成war文件轮廓。

我觉得我错过了关于Maven的一些事情,希望你能指导我。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在prod个人资料中插入<skip.integration.tests>false</skip.integration.tests>。如果您不希望每次使用prod配置文件构建时都执行集成测试,则可以使用

在命令行上手动设置此属性。
mvn install -Pprod -Dskip.integration.tests=false