如何停止Maven的验证阶段重建工件? [英] How to stop Maven's verify phase rebuilding the artifact?

查看:248
本文介绍了如何停止Maven的验证阶段重建工件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下一个使用Maven构建的Java项目,

Imagine a Java project built using Maven for which I have:


  • 一些快速运行的单元测试:

    • 开发人员应在提交之前运行

    • 我的CI服务器(Hudson,FWIW)应在检测到新提交后运行,以防出现故障时几乎立即给出反馈


    • 如果开发人员可以运行选择,例如重现和修复故障

    • 我的CI服务器应在成功运行单元测试后运行

    这似乎是一种典型的情况。目前,我正在运行:

    This seems like a typical scenario. Currently, I'm running:


    • 处于测试阶段的单元测试

    • 验证阶段的验收测试

    配置了两个CI作业,两个作业均指向项目的VCS分支:

    There are two CI jobs configured, both pointing to the project's VCS branch:


    1. Commit Stage,运行 mvn package(编译和单元测试代码,构建工件),如果成功,将触发:

    2. 自动验收测试,运行 mvn verify(设置,运行和拆除验收测试)

    问题在于作业2单元将再次测试并重新构建被测工件(因为验证阶段会自动调用打包阶段)。出于某些原因(重要性降低),这是不可取的:

    The problem is that job 2 unit tests and builds the artifact-under-test all over again (because the verify phase automatically invokes the package phase). This is undesirable for several reasons (in decreasing importance):


    • 作业2创建的工件可能与作业1创建的工件不同(例如,如果在此期间进行了新的提交)

    • 延长了进行提交的开发人员的反馈循环(即,他们需要更长的时间才能发现他们破坏了版本)

    • 浪费CI服务器上的资源

    所以我的问题是,如何配置作业2使用作业1创建的工件?

    So my question is, how can I configure job 2 to use the artifact created by job 1?

    我意识到我只能拥有一个运行 mvn verify的CI作业,该作业只会创建一次工件,但我想使用上述单独的CI作业来实现Farley风格的部署管道。

    万一它可以帮助任何人,这是公认的答案中项目2的完整Maven 2 POM:

    In case it helps anyone, here's the full Maven 2 POM of "project 2" in the accepted answer:

    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example.cake</groupId>
        <artifactId>cake-acceptance</artifactId>
        <version>1.0</version>
        <packaging>jar</packaging>
        <name>Cake Shop Acceptance Tests</name>
        <description>
            Runs the automated acceptance tests for the Cake Shop web application.
        </description>
        <build>
            <plugins>
                <!-- Compiler -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
                <!-- Suppress the normal "test" phase; there's no unit tests -->
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.5</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
                <!-- Cargo (starts and stops the web container) -->
                <plugin>
                    <groupId>org.codehaus.cargo</groupId>
                    <artifactId>cargo-maven2-plugin</artifactId>
                    <version>1.0.5</version>
                    <executions>
                        <execution>
                            <id>start-container</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>start</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>stop-container</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>stop</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <!-- Don't wait for CTRL-C after starting the container -->
                        <wait>false</wait>
    
                        <container>
                            <containerId>jetty7x</containerId>
                            <type>embedded</type>
                            <timeout>20000</timeout>
                        </container>
    
                        <configuration>
                            <properties>
                                <cargo.servlet.port>${http.port}</cargo.servlet.port>
                            </properties>
                            <deployables>
                                <deployable>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>${target.artifactId}</artifactId>
                                    <type>war</type>
                                    <properties>
                                        <context>${context.path}</context>
                                    </properties>
                                </deployable>
                            </deployables>
                        </configuration>
                    </configuration>
                </plugin>
                <!-- Failsafe (runs the acceptance tests) -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>integration-test</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>verify</id>
                            <goals>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <includes>
                            <include>**/*Test.java</include>
                        </includes>
                        <skipTests>false</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
                <!-- Add your tests' dependencies here, e.g. Selenium or Sahi,
                    with "test" scope -->
            <dependency>
                <!-- The artifact under test -->
                <groupId>${project.groupId}</groupId>
                <artifactId>${target.artifactId}</artifactId>
                <version>${target.version}</version>
                <type>war</type>
            </dependency>
        </dependencies>
        <properties>
            <!-- The artifact under test -->
            <target.artifactId>cake</target.artifactId>
            <target.version>0.1.0-SNAPSHOT</target.version>
            <context.path>${target.artifactId}</context.path>
            <http.port>8081</http.port>
            <java.version>1.6</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    </project>
    

    请注意,即使这个测试项目没有创建工件,它也必须使用一些一种包装(我在这里使用 jar),否则在验证阶段不运行任何测试。

    Note that even though this "tests" project doesn't create an artifact, it has to use some kind of packaging (I used "jar" here), otherwise no tests are run in the verify phase.

    推荐答案

    尝试两个专家项目。第一个包含构建和单元测试。您将工件安装在本地存储库中。第二个作业运行第二个maven项目,该项目将第一个项目的工件声明为依赖项并运行功能测试。

    Try two maven projects. The first one contains the build and unit tests. You install the artifacts in your local repository. The second job runs the second maven project which declares the artifacts of the first project as dependencies and runs the functional tests.

    不确定我刚才描述的场景是否可行,

    Not sure if the scenario I just described is possible, but I think it is.

    要快速改进,您可以使用 -Dmaven.test.skip = true 。如果将scm中代码的修订号传递给第二份工作,则应该可以签出相同的源代码。

    For a quick improvement you can bypass the unit test with -Dmaven.test.skip=true. If you pass the revision number of your code in your scm to the second job, you should be able to checkout the same source code.

    您还可以签入Clone Workspace SCM插件。这可能会为您提供一些其他选择。

    You can also check into the Clone Workspace SCM plugin. This might offer you some additional options.

    这篇关于如何停止Maven的验证阶段重建工件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆