【问题标题】:Maven: Can't Launch Plugins on JAR project in pre-integration-test and post-integration-test phasesMaven:无法在集成测试前和集成测试后阶段在 JAR 项目上启动插件
【发布时间】:2012-04-28 06:00:48
【问题描述】:

我正在编写一个 JAR 文件,作为 Jenkins CI webapp 的客户端。所以我的项目有jar包,没有war包。但是,对于集成测试,我需要部署 Jenkins WAR 来测试我的 JAR 类。

我正在使用 cargo-maven2-plugin,并对其进行设置,以便我可以使用“mvn cargo:run”从命令行启动 Jenkins 但是,运行“mvn install”进入集成测试阶段,无需试图用 Cargo 启动 Jenkins。事实上,“mvn install -X”的输出甚至没有提到“cargo”这个词。

看起来我已经正确地将货物绑定到 pre-integration-test 和 post-integration-test ,但它不会触发。

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.phoenix.build</groupId>
<artifactId>HostOp</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jetty.port>53657</jetty.port><!-- mnemonic: 'JENKS' on a telephone -->
</properties>
<name>Host Operations</name>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7.2</version>
                <configuration>
                    <systemProperties>
                        <property>
                            <name>maven.output.dir</name>
                            <value>${project.build.directory}</value>
                        </property>
                    </systemProperties>
                    <excludes>
                        <exclude>**/*IntegrationTest.java</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-tests</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                            <excludes>
                                <exclude>none</exclude>
                            </excludes>
                            <includes>
                                <include>**/*IntegrationTest.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <executions>
                    <execution>
                        <id>start-container</id>
                        <phase>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>
                    <container>
                        <containerId>jetty6x</containerId>
                        <type>embedded</type>
                        <systemProperties>
                            <JENKINS_HOME>target/test-classes/jenkins_home</JENKINS_HOME>
                        </systemProperties>
                    </container>
                    <configuration>
                        <properties>
                            <cargo.servlet.port>53657</cargo.servlet.port>
                            <cargo.jvmargs>-DJENKINS_HOME=target/test-classes/jenkins_home</cargo.jvmargs>
                        </properties>
                        <deployables>
                            <deployable>
                                <type>war</type>
                                <location>target/test-classes/jenkins.war</location>
                                <pingURL>http://localhost:53657/jenkins/view/All/newJob</pingURL>
                            </deployable>
                        </deployables>
                    </configuration>
                </configuration>
            </plugin>
        </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
    </dependency>
</dependencies>
</project>

我怎样才能让它根据需要触发?

【问题讨论】:

  • 你确定你的 /project/build/plugins 元素中有插件并且没有意外嵌套在配置文件中吗?咬我的次数比我数的多,但我仍然这样做。
  • 抱歉,它在 中。不过想法不错。
  • 你能发布一个 pom 修剪成你看到的失败的货物插件吗?
  • 它足够小,我刚刚发布了整个 POM。

标签: maven maven-3 cargo


【解决方案1】:

您发布的 pom 对我来说按预期工作。我只是把它放到一个空项目中,然后:

$ mvn install
[...]
[INFO] --- maven-jar-plugin:2.3.1:jar (default-jar) @ cargo-plugin-test ---
[...]
[INFO] --- maven-surefire-plugin:2.7.2:test (integration-tests) @ cargo-plugin-test ---
[...]
[INFO] --- cargo-maven2-plugin:1.2.1:start (start-container) @ cargo-plugin-test ---
[INFO] [2.ContainerStartMojo] Resolved container artifact org.codehaus.cargo:cargo-core-container-jetty:jar:1.2.1 for container jetty6x
[INFO] [beddedLocalContainer] Jetty 6.x Embedded starting...
[...]
2012-04-27 15:01:02.457:WARN::Web application not found target/test-classes/jenkins.war
2012-04-27 15:01:02.457:WARN::Failed startup of context 
[...]
2012-04-27 15:01:02.741:INFO::Started SelectChannelConnector@0.0.0.0:53657
[INFO] [beddedLocalContainer] Jetty 6.x Embedded started on port [53657]
[...]

Maven 3.0.3 就是这样:

$ mvn --version
Apache Maven 3.0.3 (r1075438; 2011-02-28 11:31:09-0600)
Maven home: /home/ryan/dev/tools/maven
Java version: 1.7.0_03, vendor: Oracle Corporation
Java home: /home/ryan/dev/tools/jdk1.7.0_03/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.0.0-17-generic", arch: "amd64", family: "unix"

我不确定它可能是什么。

【讨论】:

  • 我使用的是相同的 Maven 版本。如果我摆脱了我的集成测试(rm /src/test/java/com/phoenix/build/jenkins/*IntegrationTest.java),那么 Cargo 就会启动并关闭。所以它显然只在我实际上不需要它时才有效。
  • 它变得更好了。我发现如果我编写了一个实际上并没有做任何事情的测试,并剔除其余的测试,我可以让 Cargo 启动。因此,我进行了其他一项测试并注释掉了所有内容,然后一一取消注释。有一个地方测试运行一个半无限循环(一个while(true),里面有一个break)。只要它保持半无限状态,Cargo 就不会启动。如果我用计数器使它成为一个有限的 for 循环,它将启动 Cargo。预集成测试阶段如何取决于代码本身?
  • 饥饿?如果 Maven 将 Cargo 启动分叉到另一个线程或进程中,它可能会在 Cargo 启动之前命中您的循环,这会占用 CPU 并阻止其他任何事情发生。您可以在循环中的某处尝试 Thread.sleep(50) 以减少对资源的消耗吗? (这只是在黑暗中拍摄,真的。)
  • 终于搞定了。菜鸟失误。如果你看上面的 pom,你会看到我将 cargo:start 绑定到了集成测试阶段,而不是预集成测试阶段。很抱歉浪费了带宽。
猜你喜欢
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
相关资源
最近更新 更多