java - 有没有maven sleep 功能?

标签 java maven jetty

我有 maven 配置文件集用于测试,在预集成测试中,maven 启动两个 jetty 服务器,然后开始测试。我偶然发现的问题是在服务器中,测试开始时它们没有完全加载。似乎通过在测试中添加 5 秒 sleep 时间来解决问题,但我希望将其添加到 Maven 中并从测试中删除。无论如何我可以这样做吗?请查看下面的代码示例以获取更多信息

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.work.projectname</groupId>
                                <artifactId>projectname-web</artifactId>
                                <version>${project.version}</version>
                                <type>war</type>
                                <destFileName>projectname-${project.version}.war</destFileName>
                            </artifactItem>
                            <artifactItem>
                                <groupId>com.work.projectname</groupId>
                                <artifactId>projectname-stubs-web</artifactId>
                                <version>${project.version}</version>
                                <type>war</type>
                                <destFileName>projectname-stubs-${project.version}.war</destFileName>
                            </artifactItem>
                        </artifactItems>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>true</overWriteSnapshots>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty-server.version}</version>
            <executions>
                <execution>
                    <id>start-projectname</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>deploy-war</goal>
                    </goals>
                    <configuration>
                        <daemon>true</daemon>
                        <reload>manual</reload>
                        <war>${project.build.directory}/projectname-${project.version}.war</war>
                        <webAppXml>${basedir}/src/jetty/jetty-loginService.xml</webAppXml>
                        <webAppConfig>
                            <contextPath>/projectname</contextPath>
                            <parentLoaderPriority>true</parentLoaderPriority>
                            <tempDirectory>${project.build.directory}/tmp-projectname</tempDirectory>
                        </webAppConfig>
                        <stopPort>8006</stopPort>
                        <stopKey>STOP</stopKey>
                    </configuration>
                </execution>
                <execution>
                    <id>start-projectname-stubs</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>deploy-war</goal>
                    </goals>
                    <configuration>
                        <daemon>true</daemon>
                        <reload>manual</reload>
                        <war>${project.build.directory}/projectname-stubs-${project.version}.war</war>
                        <connectors>
                            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                                <port>${stub-jetty-server.port}</port>
                                <maxIdleTime>300000</maxIdleTime>
                            </connector>
                        </connectors>
                        <stopPort>8008</stopPort>
                        <stopKey>STOP</stopKey>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-projectname</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                    <configuration>
                        <stopPort>8006</stopPort>
                        <stopKey>STOP</stopKey>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-projectname-stubs</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                    <configuration>
                        <stopPort>8008</stopPort>
                        <stopKey>STOP</stopKey>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <parallel>classes</parallel>
                <threadCountClasses>33</threadCountClasses>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

最佳答案

您可以借助 maven antrun 插件来完成。像这样:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <configuration>
        <tasks>
            <sleep seconds="5" />
        </tasks>
    </configuration>
    <executions>
        <execution>
            <id>sleep-for-a-while</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

编辑:以防有人发现这一点。根据 jl 的评论,任务确实已被弃用,这里是基于使用目标的同一件事。稍微重组但具有相同的功能。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>sleep-for-a-while</id>
            <phase>pre-integration-test</phase>
            <configuration>
                <target>
                    <sleep seconds="5" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

关于java - 有没有maven sleep 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21701229/

相关文章:

java - 打印机不写入文件

java - Flyway 仅迁移我的两个配置模式之一

java - 具有多个系统属性的 Maven exec

java - 如何加快部署到 Jetty?

java - NSS/JSS : load user imported cert along with PKCS#11 smartcard in Java

带有临时 Web 报告的 Java 商业智能框架?

java - 在 Maven 模块之间共享依赖关系

java - 无法在 jetty + nginx 上加载资源:net::ERR_CONNECTION_RESET

java - 使用 jar 命令创建 war 文件无法正常工作

c# - Dropbox 及其类似 "Folder"的设计