其中jboss-as-depchain插件可以自动从Jboss的Maven仓库下载对应版本的AS.zip包。<?xml version="1.0" encoding="UTF-8"?> <!-- vi:ts=2:sw=2:expandtab: --> <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"> <!-- Parent --> <parent> <groupId>org.jboss</groupId> <artifactId>jboss-parent</artifactId> <version>5</version> </parent> <!-- Model Version --> <modelVersion>4.0.0</modelVersion> <!-- Artifact Configuration --> <groupId>org.jboss.jbossas.embedded.examples</groupId> <artifactId>jbossas-embedded-examples-slsb</artifactId> <version>6.0.0.Final</version> <!-- To match target version of AS --> <name>JBoss Embedded AS Examples - SLSB</name> <description>Example for EmbeddedAS for testing EJB 3.x Stateless Session Beans</description> <!-- Properties --> <properties> <version.org.jboss.jbossas>6.0.0.Final</version.org.jboss.jbossas> <JBOSS_HOME>${project.build.directory}/jboss-${version.org.jboss.jbossas}</JBOSS_HOME> <version.junit>4.7</version.junit> </properties> <build> <plugins> <!-- Compiler --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <inherited>true</inherited> <configuration> <source>1.6</source> <target>1.6</target> <showDeprecation>false</showDeprecation> <showWarnings>true</showWarnings> <optimize>true</optimize> <compilerVersion>1.6</compilerVersion> <fork>true</fork> <argLine>-Xmx512M</argLine> <executable>${JAVA_HOME}/bin/javac</executable> </configuration> </plugin> <!-- Surefire --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <redirectTestOutputToFile>true</redirectTestOutputToFile> <trimStackTrace>false</trimStackTrace> <printSummary>true</printSummary> <includes> <include>**/*UnitTest.java</include> </includes> <forkMode>always</forkMode> </configuration> </plugin> </plugins> </build> <!-- Dependencies --> <dependencies> <!-- org.jboss.jbossas --> <dependency> <groupId>org.jboss.jbossas</groupId> <artifactId>jboss-as-depchain</artifactId> <version>${version.org.jboss.jbossas}</version> <type>pom</type> </dependency> <!-- JUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${version.junit}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- org.jboss.jbossas --> <dependency> <groupId>org.jboss.jbossas</groupId> <artifactId>jboss-as-depchain</artifactId> <version>${version.org.jboss.jbossas}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <profiles> <profile> <id>embedded</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <executions> <execution> <id>integration-test</id> <phase>integration-test</phase> <goals> <goal>test</goal> </goals> <configuration> <additionalClasspathElements> <additionalClasspathElement>${JBOSS_HOME}/client/jbossws-native-client.jar</additionalClasspathElement> <!-- Because jbossweb.sar contains shared web.xml, which must be visible from same CL as TomcatDeployer.class.getClassLoader --> <additionalClasspathElement>${JBOSS_HOME}/server/default/deploy/jbossweb.sar</additionalClasspathElement> </additionalClasspathElements> <redirectTestOutputToFile>true</redirectTestOutputToFile> <trimStackTrace>false</trimStackTrace> <printSummary>true</printSummary> <includes> <include>**/*IntegrationTest.java</include> </includes> <forkMode>always</forkMode> <!-- MaxPermSize Required to bump the space for relective data like classes, methods, etc. EMB-41. Endorsed required for things like WS support (EMB-61) --> <argLine>-Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Djava.util.logging.manager=org.jboss.logmanager.LogManager -Djava.endorsed.dirs=${JBOSS_HOME}/lib/endorsed -Djboss.home=${JBOSS_HOME} -Djboss.boot.server.log.dir=${JBOSS_HOME}</argLine> </configuration> </execution> </executions> </plugin> <!-- Get AS and put into "target" --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>pre-integration-test</phase> <!-- So run before testing --> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>org.jboss.jbossas</groupId> <artifactId>jboss-as-distribution</artifactId> <version>${version.org.jboss.jbossas}</version> <type>zip</type> <overWrite>false</overWrite> <outputDirectory>${project.build.directory}</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
2. 在surefire plugin中把这个包过滤掉:<dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-6.0</artifactId> <version>1.0.0.Final</version> <type>pom</type> <scope>provided</scope> </dependency>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<classpathDependencyExcludes>
<!-- exclude code absent api -->
<classpathDependencyExclude>javax:javaee-api</classpathDependencyExclude>
<classpathDependencyExclude>javax:javaee-web-api</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
JBoss在推Arquillian的同时,也支持ejb3.1新加的embeddable container.个人感觉ejb3.1的方式做起unit test 比 arquillian更简单。
原因是在isEjbJar这个方法中,它定义了一个Closeable 对象叫handle,null初始化,如果不是jar或者目录就会返回,但是在最后有一个finally,里面没检查,直接调了handle.close() 。 我了个擦,悲剧就这样发生了。。。怎么都起不来。。。Caused by: java.lang.NullPointerException
at org.jboss.ejb3.embedded.impl.base.scanner.ClassPathEjbJarScanner.isEjbJar(ClassPathEjbJarScanner.java:302)
at org.jboss.ejb3.embedded.impl.base.scanner.ClassPathEjbJarScanner.getEjbJars(ClassPathEjbJarScanner.java:174)
at org.jboss.ejb3.embedded.sub.JBossSubmersibleEJBContainer.createEJBContainer(JBossSubmersibleEJBContainer.java:117)
并且定义-Djboss.home=${JBOSS_HOME} -Xmx512m -XX:MaxPermSize=256m -Djava.endorsed.dirs=${JBOSS_HOME}/lib/endorsed
<JBOSS_HOME>C:\WorkShop\EclipseWS\IndigoWS\server\design\bcc-embedded-container\jboss-6.1.0.Final</JBOSS_HOME> </properties>
这里面的plugin是绕开eclipse m2e的插件在unpack的地方报错。<?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>com.test.sample</groupId> <artifactId>test-sample</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.8</version> <configuration> <useProjectReferences>false</useProjectReferences> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <!-- This plugin's configuration is used in m2e to prevent errors on project import in Eclipse --> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <versionRange>[2.1,)</versionRange> <goals> <goal>unpack</goal> <goal>copy-dependencies</goal> </goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> </build> <modules> <module>embedded-container</module> <module>wolfc-jboss-beach-ejb3-embedded-ee9d31a</module> </modules> </project>
这个脚本会自动从JBoss的仓库把定义版本(这里是6.1.0 Final)的JBoss拉下来,然后解压放到这个项目的jboss- ${version.org.jboss.jbossas}文件夹。注掉的部分是把当前文件夹下的run.bat拷到jboss的bin目录下替换掉原来的。<?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> <packaging>pom</packaging> <groupId>ericsson.edison</groupId> <artifactId>edison-container-embedded</artifactId> <name>container-embedded</name> <version>0.0.1-SNAPSHOT</version> <description/> <repositories> <repository> <id>jboss-public-repository-group</id> <name>JBoss Public Maven Repository Group</name> <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <properties> <version.org.jboss.jbossas>6.1.0.Final</version.org.jboss.jbossas> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <!-- Get AS and put into "target" --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>pre-integration-test</phase> <!-- So run before testing --> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>org.jboss.jbossas</groupId> <artifactId>jboss-as-distribution</artifactId> <version>${version.org.jboss.jbossas}</version> <type>zip</type> <overWrite>false</overWrite> <!--<outputDirectory>${project.build.directory}</outputDirectory>--> <outputDirectory>${basedir}</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <!-- <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>copyRelease</id> <phase>pre-integration-test</phase> <configuration> <tasks> <copy overwrite="true" todir="${basedir}/jboss-${version.org.jboss.jbossas}/bin"> <fileset dir="${basedir}/" includes="run.bat"/> </copy> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> --> </plugins> </build> <dependencies> <dependency> <groupId>org.jboss.jbossas</groupId> <artifactId>jboss-as-depchain</artifactId> <version>${version.org.jboss.jbossas}</version> <type>pom</type> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- org.jboss.jbossas --> <dependency> <groupId>org.jboss.jbossas</groupId> <artifactId>jboss-as-depchain</artifactId> <version>${version.org.jboss.jbossas}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
这里面比较重要的有几点:<?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>org.jboss.beach.ejb3</groupId> <artifactId>test-embedded</artifactId> <name>test-embedded</name> <version>0.0.1-SNAPSHOT</version> <description/> <repositories> <repository> <id>jboss-public-repository-group</id> <name>JBoss Public Maven Repository Group</name> <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <properties> <version.org.jboss.jbossas>6.1.0.Final</version.org.jboss.jbossas> <JBOSS_HOME>../embedded-container/jboss-${version.org.jboss.jbossas}</JBOSS_HOME> </properties> <build> <plugins> <!-- <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <executable>env</executable> <environmentVariables> <JBOSS_HOME> C:/Sample/embedded-container/jboss-${version.org.jboss.jbossas} </JBOSS_HOME> </environmentVariables> </configuration> <executions> <execution> <id>resetJbossHome</id> <phase>integration-test</phase> <goals> <goal>exec</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>copyRelease</id> <phase>pre-integration-test</phase> <configuration> <tasks> <exec executable="env"> <env key="JBOSS_HOME" value="C:/Edison/Docs/Learning/EmbeddableEJB/EmbeddedJBoss/Sample/embedded-container/jboss-${version.org.jboss.jbossas}"/> </exec> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>2.6</version> <configuration> <argLine>-Djboss.home=${JBOSS_HOME} -Xmx512m -XX:MaxPermSize=256m -Djava.endorsed.dirs=${JBOSS_HOME}/lib/endorsed</argLine> <classpathDependencyExcludes> <!-- exclude code absent api --> <classpathDependencyExclude>javax:javaee-api</classpathDependencyExclude> <classpathDependencyExclude>javax:javaee-web-api</classpathDependencyExclude> </classpathDependencyExcludes> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.test.sample</groupId> <artifactId>test-sar</artifactId> <version>0.1</version> <type>sar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.jboss.ejb3.embedded</groupId> <artifactId>jboss-ejb3-embedded-standalone</artifactId> <version>1.0.0-alpha-4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>[4,)</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
到这里,新的测试ejb的方案已经测试的差不多了。我自己可以写小例子通过了。