AddThis

Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Friday, August 26, 2011

Run Jetty Externally and Connect Remotely With Eclipse

There's another alternative to embedding Jetty inside Eclipse.  We can also use Eclipse to use Maven to start Jetty for us.  Then we can remotely connect and debug/manage it.  It's your choice as to which option you prefer.


Configure Maven to run Jetty

<plugin>
        <groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.24</version>
<configuration>
 <connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                  <!-- Use whatever you want the http port to be -->
 <port>8080</port>  
</connector>
 </connectors>
 <userRealms>
<userRealm implementation="org.mortbay.jetty.security.HashUserRealm">
 <name>default</name>
 <config>src/main/resources/jetty-realm.properties</config>
</userRealm>
 </userRealms>
          <!-- If you plan on changing this file during execution, turn the scan interval higher than 0 -->
 <scanIntervalSeconds>0</scanIntervalSeconds>
 <webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
</configuration>
</plugin>

You will need a jetty-realm.properties file and a webdefault.xml file.

jetty-realm.properties:
user1: pass1,role1
user2: pass2,role2

webdefault.xml:
Mine looks like the "JSP configuration" section



Setup Start Server Command
  1. Run -> External Tools -> External Tools Configuration 
  2. Right Click Program -> New
    1. Name: Whatever
    2. Location: Browse to your mvn executable
    3. Working Directory: click Browse Workspace button and select ${YOUR_WEB_PROJECT}
    4. Arguments: jetty:run
  3. Click Environment
    1. Click New
    2. Enter a variable names MAVEN_OPTS and set it to "-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"


Setup Remote Debugging
  1. Run -> Debug Configurations
  2. Right Click Remote Java Application -> New
    1. Name: Whatever
    2. Project: ${YOUR_WEB_PROJECT}
    3. Connection Type: Standard (Socket Attach)
    4. Host: localhost
    5. Port: 8787
  3. Also click "Allow termination of remote VM".  It's going to be the only way to elegantly kill your Jetty server without opening the task manager.


Usage
  1. Start the server using the external tools 
  2. Start the remote debugging once the server is up
  3. Open up the debug perspective -> Right click the VM and click terminate to kill the server

Thursday, August 18, 2011

Java Integration Tests

You have unit tests and integration tests, but how do you manage the two?  Can you easily partion them so that you only run unit tests at one time and integration tests at another?  Then have them both run before building your artifact?  Yea, maven makes it real easy.  You might already know about the surefire plugin, the plugin used to run unit tests.  It looks for classes ending or starting with Test (**/Test*.java, **/*Test.java, and **/*TestCase.java) and runs them in the "test" phase.  But there is another plugin, failsafe, which is used to run integration tests and is run during the "integration-test" phase.  The failsafe plugin looks for classes ending or starting with IT (**/IT*.java, **/*IT.java, and **/*ITCase.java) and runs those.

The failsafe plugin has four phases that it uses:
  1. pre-integration-test - the setup phase.  use it to start a webserver or configure data in a database.
  2. integration-test - runs the tests.
  3. post-integration-test - the teardown phase.  use it to stop your webserver or clean your database.
  4. verify - used to help intepret results of the tests.  if any tests failed, the build will exit.

Configuration:
Surefire
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.7.2</version>
        <configuration>
          <testFailureIgnore>true</testFailureIgnore>
        </configuration>
      </plugin>

Failsafe
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>failsafe-maven-plugin</artifactId>
        <version>2.4.3-alpha-1</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

Another approach that I have done on previous projects is to leave all my unit tests in their appropriate projects and package all of my integration tests into a separate project that I run every few hours everyday in an integration test environment.