Schlagwörter: jetty Kommentarverlauf ein-/ausschalten | Tastaturkürzel

  • Andreas Höhmann 12:05 am Tuesday, 1. June 2010 Permalink |
    Tags: integration-test, jetty, maven2, port already bind, ,   

    Howto define jetty port for integration tests 

    Today I found a simple hack to define the jetty-port for a web-application integration test via command line.

    The problem was the defined „default“ jetty port (here 8080) … on the build system this port was already bind … so the integration test always failed :-(.

    My first idea was to call maven with external system property „jetty.port“ … (this worked already to start the web application on different ports):

    mvn clean integration-test -Pintegration-test -Djetty.port=1234

    The maven profile is here:

    ...
      <profile>
          <id>integration-test</id>
          <build>
            <defaultGoal>integration-test</defaultGoal>
            <plugins>
              <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <executions>
                  <execution>
                    <id>integration-tests</id>
                    <phase>integration-test</phase>
                    <goals>
                      <goal>test</goal>
                    </goals>
                    <configuration>
                      <forkMode>once</forkMode>
                      <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/Integration-Suite.xml</suiteXmlFile>
                      </suiteXmlFiles>
                    </configuration>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>${jetty.version}</version>
                <executions>
                  <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                      <goal>run</goal>
                    </goals>
                    <configuration>
                      <daemon>true</daemon>
                      <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                          <port>${jetty.port}</port>
                        </connector>
                      </connectors>
                    </configuration>
                  </execution>
                  <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                      <goal>stop</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
            </plugins>
          </build>
        </profile>
    

    I think with this TestNG test this should work … (the base class ImportantUseCaseTest contains some @Test annotated test methods):

    public class IntegrationTest extends ImportantUseCaseTest {
      @Override
      protected String getServerURL() {
        final String jettyPort = System.getProperty("jetty.port", "8080);
        return String.format("http://localhost:%s/webapp", jettyPort);
      }
    }
    

    But it doesn’t work 🙂

    If I call maven with „-Djetty.port=1234“ the jetty starts on 1234 but the integration test runs on 8080.  Hmmm …

    The solution was to change the surefire plugin configuration a little bit:

    ...
      <plugin>
         <artifactId>maven-surefire-plugin</artifactId>
         ...
         <configuration>
             ...
             <argLine>-Xmx512m -Djetty.port.integration=${jetty.port}</argLine>
         </configuration>
    

    and the test class:

    public class IntegrationTest extends ImportantUseCaseTest {
      @Override
      protected String getServerURL() {
        final String jettyPort = System.getProperty("jetty.port.integration", "8080);
        return String.format("http://localhost:%s/webapp", jettyPort);
      }
    }
    

    I guess that surefire with forkMode=once doesn’t pass the given system properties to the test jvm. Maybe with forkMode=never this problem not exists … but you can try this by yourself 😉

     
  • Andreas Höhmann 11:31 am Tuesday, 25. May 2010 Permalink |
    Tags: jetty, ,   

    Export maven war artifact as runnable jetty package 

    Today I will show you a way to export a <packaging>war</packaging> artifact as a runnable windows application.

    The ingredients are:

    • maven war artifact (i.e. our web application)
    • embedded Jetty

    A minimal web application’s pom looks like this:

    <?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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>de.ahoehma</groupId>
      <artifactId>dummy-webapp</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <packaging>war</packaging>
    </project>
    

    Now we add some components to the artifact …

    1. add the following profile to the pom.xml:

    <profiles>
       <profile>
          <id>jetty-offline</id>
          <build>
            <plugins>
              <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                  <finalName>offline-dummy-webapp-${project.version}</finalName>
                  <attach>false</attach>
                  <descriptors>
                    <descriptor>src/assembly/jetty-offline.xml</descriptor>
                  </descriptors>
                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                      <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
            </plugins>
          </build>
          <dependencies>
            <!-- logging ... -->
            <dependency>
              <groupId>commons-logging</groupId>
              <artifactId>commons-logging</artifactId>
              <version>1.1.1</version>
            </dependency>
            <dependency>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
              <version>1.2.15</version>
              <scope>runtime</scope>
            </dependency>
            <!-- embedded jetty -->
            <dependency>
              <groupId>org.mortbay.jetty</groupId>
              <artifactId>jetty</artifactId>
              <version>${jetty.version}</version>
            </dependency>
            <dependency>
              <groupId>org.mortbay.jetty</groupId>
              <artifactId>start</artifactId>
              <version>${jetty.version}</version>
            </dependency>
            <dependency>
              <groupId>org.mortbay.jetty</groupId>
              <artifactId>jetty-util</artifactId>
              <version>${jetty.version}</version>
            </dependency>
            <dependency>
              <groupId>org.mortbay.jetty</groupId>
              <artifactId>jsp-2.1-jetty</artifactId>
              <version>${jetty.version}</version>
            </dependency>
          </dependencies>
          <properties>
              <jetty.version>6.1.23</jetty.version>
          </properties>
        </profile>
    </profiles>
    

    2. the assembly descriptor for src/assembly/jetty-offline is here:

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1 http://maven.apache.org/xsd/assembly-1.1.1.xsd">
    
      <formats>
        <format>dir</format>
      </formats>
      
      <dependencySets>
        <dependencySet>
          <!-- unpack the webapp as root application -->
          <scope>runtime</scope>
          <unpack>true</unpack>
          <outputDirectory>webapps/root/</outputDirectory>
          <includes>
            <include>de.ahoehma:dummy-webapp:war</include>
          </includes>
        </dependencySet>
        <dependencySet>
          <unpack>false</unpack>
          <outputDirectory>lib</outputDirectory>
          <useTransitiveFiltering>true</useTransitiveFiltering>
          <useStrictFiltering>true</useStrictFiltering>
          <includes>
            <include>org.mortbay.jetty:jetty</include>
            <include>org.mortbay.jetty:jetty-util</include>
            <include>org.mortbay.jetty:start</include>
            <include>org.mortbay.jetty:jsp-2.1-jetty</include>
            <include>commons-logging:commons-logging</include>
            <include>log4j:log4j</include>
          </includes>
        </dependencySet>
      </dependencySets>
    
      <fileSets>
        <fileSet>
          <directory>src/jetty/bin</directory>
          <outputDirectory>bin/</outputDirectory>
          <filtered>false</filtered>
        </fileSet>
        <fileSet>
          <directory>src/jetty/resources</directory>
          <outputDirectory>etc/</outputDirectory>
          <filtered>false</filtered>
        </fileSet>
      </fileSets>
      
    </assembly>
    

    See maven-assembly-plugin for more options …

    3. src/jetty/bin contains a windows batch file:

     @echo off
     @set JVM_OPTS=-XX:MaxPermSize=256m -XX:PermSize=128m -Xms128m -Xmx512m
     (cd .. && java %JVM_OPTS% -jar lib/start-6.1.23.jar)
    

    4. and at least the src/jetty/resources contains the file jetty.xml:

    <?xml version="1.0"?>
    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
    <Configure id="Server" class="org.mortbay.jetty.Server">
        <Set name="ThreadPool">
          <New class="org.mortbay.thread.BoundedThreadPool">
            <Set name="minThreads">10</Set>
            <Set name="maxThreads">250</Set>
            <Set name="lowThreads">25</Set>
          </New>
        </Set>
        <Call name="addConnector">
          <Arg>
              <New class="org.mortbay.jetty.nio.SelectChannelConnector">
                <Set name="host"><SystemProperty name="jetty.host" default="localhost"/></Set>
                <Set name="port"><SystemProperty name="jetty.port" default="1976"/></Set>
                <Set name="maxIdleTime">30000</Set>
                <Set name="Acceptors">2</Set>
                <Set name="statsOn">false</Set>
                <Set name="confidentialPort">8442</Set>
          	    <Set name="lowResourcesConnections">5000</Set>
          	    <Set name="lowResourcesMaxIdleTime">5000</Set>
              </New>
          </Arg>
        </Call>
        <Set name="handler">
          <New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
            <Set name="handlers">
             <Array type="org.mortbay.jetty.Handler">
               <Item>
                 <New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>
               </Item>
               <Item>
                 <New id="DefaultHandler" class="org.mortbay.jetty.handler.DefaultHandler"/>
               </Item>
             </Array>
            </Set>
          </New>
        </Set>
        <Call name="addLifeCycle">
          <Arg>
            <New class="org.mortbay.jetty.deployer.ContextDeployer">
              <Set name="contexts"><Ref id="Contexts"/></Set>
              <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>
              <Set name="scanInterval">1</Set>
            </New>
          </Arg>
        </Call>
        <Call name="addLifeCycle">
          <Arg>
            <New class="org.mortbay.jetty.deployer.WebAppDeployer">
              <Set name="contexts"><Ref id="Contexts"/></Set>
              <Set name="webAppDir"><SystemProperty name="jetty.home" default="."/>/webapps</Set>
          	  <Set name="parentLoaderPriority">false</Set>
          	  <Set name="extract">true</Set>
          	  <Set name="allowDuplicates">false</Set>
            </New>
          </Arg>
        </Call>
        <Set name="stopAtShutdown">true</Set>
        <Set name="sendServerVersion">true</Set>
        <Set name="sendDateHeader">true</Set>
        <Set name="gracefulShutdown">1000</Set>
    </Configure>
    

    Now we are able to build a „offline version“ of our web application:

     mvn package -Pjetty-offline
    

    The result will be a directory target\distribution\offline-dummy-webapp-1.0.0-SNAPSHOT with the structure:

     bin/
        jetty_run.bar
     etc/
        jetty.xml
     lib/
        log4j-1.2.15.jar
        commons-logging-1.1.1.jar    
        servlet-api-2.5-20081211.jar
        start-6.1.23.jar
        jetty-6.1.23.jar
        jetty-util-6.1.23.jar
        ant-1.6.5.jar
        core-3.1.1.jar
        ecj-3.5.1.jar
        jsp-2.1-jetty-6.1.23.jar
        jsp-2.1-glassfish-2.1.v20091210.jar
        jsp-api-2.1-glassfish-2.1.v20091210.jar
     webapps/
        root/
           index.html
           css/
           images/
           META-INF/
           pages/
           WEB-INF/
           ...
    

    To run the web application start the jetty_run.bat (i.e. open the dir with the explorer and double click the bat-file).

    That’s it 😀

    This article was the first part if the tutorial … in the second part I will show you how to build a windows exe package with a runnable web application inside …

     
    • tplee923 19:54 am Donnerstag, 16. Juni 2011 Permalink | Zum Antworten anmelden

      When I use your code, and execute the bat file, why I get the error:
      java.lang.ClassNotFoundException: org.mortbay.xml.XmlConfiguration
      at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
      at org.mortbay.start.Main.invokeMain(Main.java:179)
      at org.mortbay.start.Main.start(Main.java:534)
      at org.mortbay.start.Main.start(Main.java:441)
      at org.mortbay.start.Main.main(Main.java:119)

c
Neuen Beitrag erstellen
j
nächster Beitrag/nächster Kommentar
k
vorheriger Beitrag/vorheriger Kommentar
r
Antworten
e
Bearbeiten
o
zeige/verstecke Kommentare
t
Zum Anfang gehen
l
zum Login
h
Zeige/Verberge Hilfe
Shift + ESC
Abbrechen