Author: snicoll Date: Wed May 31 10:15:15 2006 New Revision: 410604 URL: http://svn.apache.org/viewvc?rev=410604&view=rev Log: MEJB-6: Added support for ejbVersion parameter, providing a support for EJB3 archives Submitted By: Tim Kettler, Dan Greening Reviewed By: Stephane Nicoll
Modified: maven/plugins/trunk/maven-ejb-plugin/pom.xml maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugin/ejb/EjbMojo.java maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugin/ejb/EjbMojoTest.java Modified: maven/plugins/trunk/maven-ejb-plugin/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/pom.xml?rev=410604&r1=410603&r2=410604&view=diff ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/pom.xml (original) +++ maven/plugins/trunk/maven-ejb-plugin/pom.xml Wed May 31 10:15:15 2006 @@ -1,4 +1,6 @@ -<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"> <parent> <artifactId>maven-plugins</artifactId> <groupId>org.apache.maven.plugins</groupId> Modified: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugin/ejb/EjbMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugin/ejb/EjbMojo.java?rev=410604&r1=410603&r2=410604&view=diff ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugin/ejb/EjbMojo.java (original) +++ maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugin/ejb/EjbMojo.java Wed May 31 10:15:15 2006 @@ -46,6 +46,8 @@ private static final String[] EMPTY_STRING_ARRAY = new String[0]; + private static final String ejbJarXmlFile = "META-INF/ejb-jar.xml"; + /** * The directory for the generated EJB. * @@ -130,6 +132,20 @@ private JarArchiver jarArchiver; /** + * What EJB version should the ejb-plugin generate? ejbVersion can be "2.x" or "3.x" + * (where x is a digit), defaulting to "2.1". When ejbVersion is "3.x", the + * ejb-jar.xml file is optional. + * + * <br/>Usage: + * <pre> + * <ejbVersion>3.0</ejbVersion> + * </pre> + * @parameter expression="2.1" + * @required + */ + private String ejbVersion; + + /** * The client Jar archiver. * * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}" @@ -161,7 +177,7 @@ public void execute() throws MojoExecutionException { - getLog().info( "Building ejb " + jarName ); + getLog().info( "Building ejb " + jarName + " with ejbVersion " + ejbVersion ); File jarFile = new File( basedir, jarName + ".jar" ); @@ -171,14 +187,24 @@ archiver.setOutputFile( jarFile ); - String ejbJarXmlFile = "META-INF/ejb-jar.xml"; + File deploymentDescriptor = new File( outputDirectory, ejbJarXmlFile ); + + // test EJB version compliance + if ( !( ejbVersion.matches( "\\A3\\.[0-9]\\z" ) || deploymentDescriptor.exists() ) ) + { + throw new MojoExecutionException( "Error assembling EJB: META-INF/ejb-jar.xml is required for ejbVersion < 3.0" ); + } try { archiver.getArchiver().addDirectory( new File( outputDirectory ), DEFAULT_INCLUDES, new String[]{ejbJarXmlFile, "**/package.html"} ); - archiver.getArchiver().addFile( new File( outputDirectory, ejbJarXmlFile ), ejbJarXmlFile ); + // possibly require ejb-jar.xml + if ( deploymentDescriptor.exists() ) + { + archiver.getArchiver().addFile( deploymentDescriptor, ejbJarXmlFile ); + } // create archive archiver.createArchive( project, archive ); Modified: maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugin/ejb/EjbMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugin/ejb/EjbMojoTest.java?rev=410604&r1=410603&r2=410604&view=diff ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugin/ejb/EjbMojoTest.java (original) +++ maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugin/ejb/EjbMojoTest.java Wed May 31 10:15:15 2006 @@ -16,15 +16,16 @@ * limitations under the License. */ -import java.io.File; -import java.util.LinkedList; -import java.util.jar.JarFile; - +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.ejb.stub.MavenProjectResourcesStub; import org.apache.maven.plugin.ejb.utils.JarContentChecker; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.codehaus.plexus.util.FileUtils; +import java.io.File; +import java.util.LinkedList; +import java.util.jar.JarFile; + /** * EJB plugin Test Case @@ -97,6 +98,7 @@ setVariableValueToObject( mojo, "clientExcludes", new LinkedList() ); setVariableValueToObject( mojo, "clientIncludes", new LinkedList() ); setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "2.1" ); mojo.execute(); @@ -144,6 +146,7 @@ setVariableValueToObject( mojo, "clientExcludes", new LinkedList() ); setVariableValueToObject( mojo, "clientIncludes", new LinkedList() ); setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "2.1" ); mojo.execute(); @@ -194,6 +197,7 @@ setVariableValueToObject( mojo, "clientExcludes", new LinkedList() ); setVariableValueToObject( mojo, "clientIncludes", new LinkedList() ); setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "2.1" ); mojo.execute(); @@ -254,6 +258,7 @@ setVariableValueToObject( mojo, "clientExcludes", new LinkedList() ); setVariableValueToObject( mojo, "clientIncludes", new LinkedList() ); setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "2.1" ); mojo.execute(); @@ -320,6 +325,7 @@ setVariableValueToObject( mojo, "clientExcludes", new LinkedList() ); setVariableValueToObject( mojo, "clientIncludes", inclusions ); setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "2.1" ); mojo.execute(); @@ -386,6 +392,7 @@ setVariableValueToObject( mojo, "clientExcludes", exclusions ); setVariableValueToObject( mojo, "clientIncludes", new LinkedList() ); setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "2.1" ); mojo.execute(); @@ -409,5 +416,143 @@ assertTrue( FileUtils.fileExists( checkedJarFile ) ); assertTrue( inclusionChecker.isOK( new JarFile( checkedJarFile ) ) ); assertFalse( exclusionChecker.isOK( new JarFile( checkedJarFile ) ) ); + } + + /** + * tests if the mojo throws an exception when the EJB version is < 3.0 + * and no deployment descriptor is present. The case with deployment descriptor + * present is covered by the testJarCreation* tests. + * + * @throws Exception + */ + public void testEjbCompliance_2_1_WithoutDescriptor() + throws Exception + { + File pomFile = new File( getBasedir(), defaultPOMPath ); + EjbMojo mojo = (EjbMojo) lookupMojo( "ejb", pomFile ); + + assertNotNull( mojo ); + + // this will automatically create the isolated + // test environment + MavenProjectResourcesStub project = new MavenProjectResourcesStub( "testEjbCompliance_2_1_WithoutDescriptor" ); + + // create the necessary test files + + // put this on the root dir + project.addFile( "pom.xml", MavenProjectResourcesStub.ROOT_FILE ); + // start creating the environment + project.setupBuildEnvironment(); + + // configure mojo + String jarName = "testJar"; + + setVariableValueToObject( mojo, "basedir", project.getBuild().getDirectory() ); + setVariableValueToObject( mojo, "outputDirectory", project.getBuild().getOutputDirectory() ); + setVariableValueToObject( mojo, "jarName", jarName ); + setVariableValueToObject( mojo, "generateClient", "false" ); + setVariableValueToObject( mojo, "clientExcludes", new LinkedList() ); + setVariableValueToObject( mojo, "clientIncludes", new LinkedList() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "2.1" ); + + try + { + mojo.execute(); + fail( "Exception should be thrown: No deployment descriptor present." ); + } + catch ( MojoExecutionException e ) + { + } + } + + /** + * Tests if the jar is created under EJB version 3.0 with + * deployment descriptor present. + * + * @throws Exception + */ + public void testEjbCompliance_3_0_WithDescriptor() + throws Exception + { + File pomFile = new File( getBasedir(), defaultPOMPath ); + EjbMojo mojo = (EjbMojo) lookupMojo( "ejb", pomFile ); + + assertNotNull( mojo ); + + // this will automatically create the isolated + // test environment + MavenProjectResourcesStub project = new MavenProjectResourcesStub( "testEjbCompliance_3_0_WithDescriptor" ); + + // create the necessary test files + + // put this on the target dir + project.addFile( "META-INF/ejb-jar.xml", MavenProjectResourcesStub.OUTPUT_FILE ); + // put this on the root dir + project.addFile( "pom.xml", MavenProjectResourcesStub.ROOT_FILE ); + // start creating the environment + project.setupBuildEnvironment(); + + // configure mojo + String jarName = "testJar"; + + setVariableValueToObject( mojo, "basedir", project.getBuild().getDirectory() ); + setVariableValueToObject( mojo, "outputDirectory", project.getBuild().getOutputDirectory() ); + setVariableValueToObject( mojo, "jarName", jarName ); + setVariableValueToObject( mojo, "generateClient", "false" ); + setVariableValueToObject( mojo, "clientExcludes", new LinkedList() ); + setVariableValueToObject( mojo, "clientIncludes", new LinkedList() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "3.0" ); + + mojo.execute(); + + // validate jar creation + String checkedJarFile = project.getBuild().getDirectory() + "/" + jarName + ".jar"; + assertTrue( FileUtils.fileExists( checkedJarFile ) ); + } + + /** + * Tests if the jar is created under EJB version 3.0 without + * deployment descriptor present. + * + * @throws Exception + */ + public void testEjbCompliance_3_0_WithoutDescriptor() + throws Exception + { + File pomFile = new File( getBasedir(), defaultPOMPath ); + EjbMojo mojo = (EjbMojo) lookupMojo( "ejb", pomFile ); + + assertNotNull( mojo ); + + // this will automatically create the isolated + // test environment + MavenProjectResourcesStub project = new MavenProjectResourcesStub( "testEjbCompliance_3_0_WithoutDescriptor" ); + + // create the necessary test files + + // put this on the root dir + project.addFile( "pom.xml", MavenProjectResourcesStub.ROOT_FILE ); + // start creating the environment + project.setupBuildEnvironment(); + + // configure mojo + String jarName = "testJar"; + + setVariableValueToObject( mojo, "basedir", project.getBuild().getDirectory() ); + setVariableValueToObject( mojo, "outputDirectory", project.getBuild().getOutputDirectory() ); + setVariableValueToObject( mojo, "jarName", jarName ); + setVariableValueToObject( mojo, "generateClient", "false" ); + setVariableValueToObject( mojo, "clientExcludes", new LinkedList() ); + setVariableValueToObject( mojo, "clientIncludes", new LinkedList() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "ejbVersion", "3.0" ); + + mojo.execute(); + + // validate jar creation + String checkedJarFile = project.getBuild().getDirectory() + "/" + jarName + ".jar"; + assertTrue( FileUtils.fileExists( checkedJarFile ) ); } }