Enterprise Java Development@TOPIC@
Many/most build commands can be automated through Maven or other scripts
Used in interactive development and automated/headless builds
Uses EJB, EAR and Test Module
Also shows WAR option to EAR
IT tests could be placed inside WAR module
Figure 92.1. Example Source Tree
. |-- ejb-basic-ear | `-- pom.xml |-- ejb-basic-ejb | |-- pom.xml | `-- src | |-- main | | `-- java | | `-- info | | `-- ejava | | `-- examples | | `-- ejb | | `-- basic | | |-- dto | | | |-- Greeting.java | | | `-- Name.java | | `-- ejb | | |-- BadRequestException.java | | |-- GreeterEJB.java | | |-- Greeter.java | | `-- GreeterRemote.java | `-- test | |-- java | | `-- info | | `-- ejava | | `-- examples | | `-- ejb | | `-- basic | | `-- pojo | | `-- GreeterTest.java | `-- resources | `-- log4j.xml |-- ejb-basic-test | |-- pom.xml | `-- src | `-- test | |-- java | | `-- info | | `-- ejava | | `-- examples | | `-- ejb | | `-- basicejb | | `-- ejbclient | | `-- GreeterIT.java | `-- resources | |-- jndi.properties | `-- log4j.xml |-- ejb-basic-war | |-- pom.xml | `-- src | `-- main | `-- webapp | `-- WEB-INF | `-- jboss-web.xml `-- pom.xml
Tests functionality of code within the bounds of a single JVM
All external dependencies stubbed, simulated, or in-memory versions used (e.g., database, JMS server)
Copies resource files from src tree to target tree
Can optionally "filter" variables and replace them with build time values (from environment's settings.xml)
Useful in customizing environment-specific properties
server URLs
server port#s
Figure 92.2. Example Source Resource File: src/test/resources/jndi.properties
$ cat src/test/resources/jndi.properties
java.naming.factory.initial=${java.naming.factory.initial}
java.naming.factory.url.pkgs=${java.naming.factory.url.pkgs}
java.naming.provider.url=${java.naming.provider.url}
#java.naming.security.principal=${jndi.user}
#java.naming.security.credentials=${jndi.password}
Source file uses variables to template source file
Figure 92.3. Example Filtered Resource File: target/test-classes/jndi.properties
$ cat target/test-classes/jndi.properties
java.naming.factory.initial=org.wildfly.naming.client.WildFlyInitialContextFactory
java.naming.factory.url.pkgs=
java.naming.provider.url=http-remoting://127.0.0.1:8080
#java.naming.security.principal=known
#java.naming.security.credentials=password1!
Concrete values are put in place for specific environment
Figure 92.4. Example Maven Configuration
<properties>
<jboss.host>localhost</jboss.host>
<jboss.http.port>8080</jboss.http.port>
<java.naming.factory.initial>org.wildfly.naming.client.WildFlyInitialContextFactory</java.naming.factory.initial>
<java.naming.provider.url>http-remoting://${jboss.host}:${jboss.http.port}</java.naming.provider.url>
<java.naming.factory.url.pkgs/>
<jndi.user>known</jndi.user>
<jndi.password>password1!</jndi.password>
</properties>
<build>
<!-- filter test/resource files for profile-specific valies -->
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
Properties defined either within pom.xml, settings.xml, or -Dsystem-properties
includes/exclused aids in filtering wrong files
avoid filtering binary files -- corrupts them (Ant has same issue)
Runs unit tests
Runs after tests compiled and before IT tests
Figure 92.5. Module Declaration
<properties>
<loop-count>3</loop-count>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<loop.count>${loop-count}</loop.count>
</systemPropertyVariables>
</configuration>
</plugin>
Plugin always enabled by default. Declaration used only to customize
Configuration defines environment for JVM
Easy way to supply system properties (-Dsystem-property=value)
values can be hard-coded or use properties to allow easier overrides
Figure 92.6. Optional Includes/Excludes
<configuration>
<includes>
<include>**/GreeterTest.java</include>
</includes>
</configuration>
Useful when turning off all unit tests to concentrate on IT tests
Can turn off problem test(s)
Figure 92.7. Parent Definition
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>${surefire.argLine}</argLine>
</configuration>
</plugin>
Just defining version here
Figure 92.8. Run Unit Tests
$ mvn clean test ... [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ ejb-basic-ejb --- ... [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ejb-basic-ejb --- ... [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ejb-basic-ejb --- ... [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ejb-basic-ejb --- ... [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ejb-basic-ejb --- ... [INFO] --- maven-surefire-plugin:2.17:test (default-test) @ ejb-basic-ejb --- ------------------------------------------------------- T E S T S ------------------------------------------------------- Running info.ejava.examples.ejb.basic.pojo.GreeterTest 02:11:38,695 INFO (GreeterTest.java:41) -*** dto *** 02:11:38,701 DEBUG (GreeterEJB.java:45) -sayHello(Name [firstName=thing, lastName=one]) 02:11:38,707 INFO (GreeterTest.java:27) -*** pojoGreeter *** 02:11:38,708 DEBUG (GreeterEJB.java:31) -sayHello(cat inhat) 02:11:38,711 INFO (GreeterTest.java:35) -*** badName *** 02:11:38,713 DEBUG (GreeterEJB.java:31) -sayHello() Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.238 sec - in info.ejava.examples.ejb.basic.pojo.GreeterTest Results : Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.871 s
Just defining version here
Runs after artifacts packaged and deployed
Required when tests require preparation/shutdown external to JVM
Four (4) phases
pre-integration-test
integration-test
post-integration-test
verify
Can be in same module as deployable artifact (i.e., within EJB or WAR modules)
Manages deployment of components to containers
Not specific to JBoss/Wildfly
Specializations for Maven and JBoss/Wildfly
Can run embedded within each IT module build or use remote instance
Typically configured within module with IT tests
Figure 92.9. IT Module Declaration
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<!-- artifacts to deploy to server -->
<deployables>
<deployable>
<groupId>${project.groupId}</groupId>
<artifactId>ejb-basic-ear</artifactId>
<type>ear</type>
</deployable>
</deployables>
</configuration>
</plugin>
Plugin declaration enacts parent-defined behavior
Module specifies artifacts to deploy
Module specification is not required if deployment is self (i.e., the primary EJB/WAR/EAR produced by this module)
Figure 92.10. WAR/EAR Module Cleanup Declarations
<profile>
<id>undeploy</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>undeploy-ear</id>
<phase>pre-clean</phase>
<goals>
<goal>undeploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Used to simply undeploy module from server
Requires artifact to be present
Profile keeps behavior from running under conditions that would fail build
Figure 92.11. Parent Definition
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo-maven2-plugin.version}</version>
<configuration>
<container>
<containerId>${cargo.containerId}</containerId>
<type>remote</type>
<log>target/server.log</log>
<output>target/output.log</output>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.hostname>${jboss.mgmt.host}</cargo.hostname>
<cargo.jboss.management.port>${jboss.mgmt.port}</cargo.jboss.management.port>
</properties>
</configuration>
</configuration>
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-controller-client</artifactId>
<version>${wildfly.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>cargo-prep</id>
<phase>pre-integration-test</phase>
<goals>
<goal>redeploy</goal>
</goals>
</execution>
<execution>
<id>cargo-post</id>
<phase>post-integration-test</phase>
<goals>
<goal>undeploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Parent defines boiler-plate portion
Child will provide module-specific information
Figure 92.12. Deploy Application
$ mvn pre-integration-test -DskipTests ... [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ejb-basic-test --- ... [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ejb-basic-test --- ... [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ejb-basic-test --- ... [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ejb-basic-test --- ... [INFO] --- maven-surefire-plugin:2.17:test (default-test) @ ejb-basic-test --- ... [INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ ejb-basic-test --- ... [INFO] --- cargo-maven2-plugin:1.4.3:redeploy (cargo-prep) @ ejb-basic-test --- Oct 01, 2014 2:20:21 AM org.xnio.Xnio <clinit> INFO: XNIO version 3.2.2.Final Oct 01, 2014 2:20:21 AM org.xnio.nio.NioXnio <clinit> INFO: XNIO NIO Implementation Version 3.2.2.Final Oct 01, 2014 2:20:21 AM org.jboss.remoting3.EndpointImpl <clinit> INFO: JBoss Remoting version 4.0.3.Final [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
Builds a new packaged, deployable artifact
Deploys and leaves on server
(optional)-DskipTests
bypasses any unit tests
Notice that build completes immediately after deploy with no IT tests or undeploy
Figure 92.13. Undeploy Application
$ mvn clean -Pundeploy ... [INFO] ------------------------------------------------------------------------ [INFO] Building EJB::Basic EJB::EAR 4.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- cargo-maven2-plugin:1.4.3:undeploy (undeploy-ear) @ ejb-basic-ear --- Oct 01, 2014 2:23:50 AM org.xnio.Xnio <clinit> INFO: XNIO version 3.2.2.Final Oct 01, 2014 2:23:50 AM org.xnio.nio.NioXnio <clinit> INFO: XNIO NIO Implementation Version 3.2.2.Final Oct 01, 2014 2:23:50 AM org.jboss.remoting3.EndpointImpl <clinit> INFO: JBoss Remoting version 4.0.3.Final [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ ejb-basic-ear --- [INFO] Deleting /home/jcstaff/workspaces/ejava-class/ejava-student/ejb/ejb-intro/ejb-basic-example/ejb-basic-ear/target
Undeploys atifact from server
Useful in automating cleanup
-Pundeploy
activates latched profile
Runs integration (IT) tests
Must be declared, not configured in by default
Runs after deployer completes and prior to undeployer
Figure 92.14. IT Module Declaration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<cargo.startstop>${cargo-startstop}</cargo.startstop>
</systemPropertyVariables>
</configuration>
</plugin>
Plugin definition causes IT tests to run
Configuration defines environment for JVM
Figure 92.15. Optional Includes/Excludes
<configuration>
<includes>
<include>**/GreeterIT.java</include>
</includes>
</configuration>
Can turn off problem or lengthy test(s)
Figure 92.16. Parent Definitition
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<configuration>
<argLine>${surefire.argLine}</argLine>
</configuration>
<executions>
<execution> <!-- run the tests here -->
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution> <!-- delay failures to after undeploy -->
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Defines version# and wires plugin into build phases when declared by child module
Figure 92.17. Run IT Tests
$ mvn clean verify
$ mvn clean verify
...
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ ejb-basic-test ---
...
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ejb-basic-test ---
...
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ejb-basic-test ---
...
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ejb-basic-test ---
...
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ejb-basic-test ---
...
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ ejb-basic-test ---
...
[INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ ejb-basic-test ---
...
[INFO] --- cargo-maven2-plugin:1.4.3:redeploy (cargo-prep) @ ejb-basic-test ---
Oct 01, 2014 2:28:02 AM org.xnio.Xnio <clinit>
INFO: XNIO version 3.2.2.Final
Oct 01, 2014 2:28:02 AM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.2.2.Final
Oct 01, 2014 2:28:02 AM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.3.Final
...
[INFO] --- maven-failsafe-plugin:2.17:integration-test (integration-tests) @ ejb-basic-test ---
-------------------------------------------------------
T E S T S
-------------------------------------------------------
...
Results :
Tests run: 12, Failures: 0, Errors: 0, Skipped: 0
...
[INFO] --- cargo-maven2-plugin:1.4.3:undeploy (cargo-post) @ ejb-basic-test ---
...
[INFO] --- maven-failsafe-plugin:2.17:verify (verify) @ ejb-basic-test ---
...
-------------------------------------------------------
[INFO] BUILD SUCCESS
Runs all phases and stops just prior to installing into local repository
Cannot bypass unit tests (unless includes/excludes used)