有没有maven睡眠功能?

时间:2014-02-11 11:59:24

标签: java maven jetty

我有maven配置文件集用于测试在预集成测试中maven启动两个jetty服务器,然后开始测试。我偶然发现的问题是在服务器中,测试开始时它们没有完全加载。似乎通过在测试中添加5秒的睡眠时间来解决问题,但我希望将其添加到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>

2 个答案:

答案 0 :(得分:16)

你可以在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的评论,任务确实已被弃用,并且基于使用目标而言,这是相同的事情。 Sligthly重组但具有相同的功能。

<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>

答案 1 :(得分:0)

我创建了maven-sleep-plugin一次。不知道它现在有多少工作,你可以尝试告诉我们。

用法(在检查源和建筑物后):

array.forEach {
    actions.append($0)
}