Is there any maven sleep functionality?

2019-04-20 09:30发布

问题:

I have maven profile set for testing where in pre-integration-tests maven starts two jetty servers and afterwards tests are started. The problem I've stumbled into is in the servers, they aren't fully loaded when tests start. It seems like the problem is fixed by adding 5 second sleep time into the tests, but I wish to add it in maven and remove from tests. Is there anyway I could do that? Please look into code sample below for additional information

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

回答1:

You can do it with the help of the maven antrun plugin. Something like this:

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

EDIT: In case someone will catch this. Based on the comment from jl, tasks have indeed been deprecated and here is the same thing based on using targets instead. Sligthly reorganized but has the same function.

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


回答2:

I have created a maven-sleep-plugin once. Not sure how much it works now, you can try and let us know.

Usage (after checking out the source and building):

<plugin>
    <groupId>org.jboss.maven.plugins</groupId>
    <artifactId>maven-sleep-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <id>sleep-5-seconds</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>sleep</goal>
            </goals>
            <configuration>
                 <delay>5</delay>
            </configuration>
        </execution>
   </executions>
</plugin>


标签: java maven Jetty