Jenkins:构建过程启动 Jetty 并开始对其进行测试

Jenkins: After build process boot Jetty and start a test against it

我如何以最佳方式设计 Jenkins 作业,其中 Java 项目由 Maven 构建,然后 Jetty 将启动,因此最后 JUnit 测试 class 可以针对它进行测试?

步骤:

  1. 通过 Maven 构建项目
  2. 启动(嵌入式)Jetty
  3. 在 Jetty
  4. 中针对 RESTful 网络服务执行测试

我目前的问题是,当 Jetty 启动时,无法执行 shell 脚本中的下一个命令,因为启动的 Jetty 挡住了路。 :-)

执行此类操作的最佳方法是什么?

我已经通过以下 Maven 构建设置实现了它:

<profiles>
        <profile>
            <id>sendAPI_CI</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>jetty</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <async>true</async>
                                    <executable>java</executable>
                                    <arguments>
                                        <argument>-Djson.schemas.location=${project.parent.parent.basedir}/common/target/test/resources/json_schemas/</argument>
                                        <argument>-jar</argument>
                                        <argument>${project.parent.parent.basedir}/facade/target/ams-jar-with-dependencies.jar</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>wait-maven-plugin</artifactId>
                        <version>1.0</version>
                        <executions>
                            <execution>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>wait</goal>
                                </goals>
                                <configuration>
                                    <protocol>http</protocol>
                                    <host>localhost</host>
                                    <port>8181</port>
                                    <file>/swagger</file>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.21.0</version>
                        <executions>
                            <execution>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
                            <testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
                            <includes>
                                <include>com.messaging.async.facade.sendApi.SendApiResourceTest.java</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

pre-integration-test 阶段我使用 exec-maven-plugin to start the (embedded) Jetty. Then by the help of the wait-maven-plugin, in the same Maven phase, we wait until the Jetty is up and this is achieved under the hood by checking if the URL is callable. In the end the maven-failsafe-plugin in the integration-test phase has its mission and starts the integration tests finally against the Jetty. As you can see this is configured within a Maven profile 所以在 Jenkins 工作中我可以专门在那里使用它。完毕!