Author: khmarbaise Date: Sat Dec 26 23:13:59 2015 New Revision: 1721772 URL: http://svn.apache.org/viewvc?rev=1721772&view=rev Log: Continued refactoring Mojo
Added: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java Modified: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbMojoTest.java Added: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java?rev=1721772&view=auto ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java (added) +++ maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java Sat Dec 26 23:13:59 2015 @@ -0,0 +1,91 @@ +package org.apache.maven.plugins.ejb; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; + +/** + * This class contains some helper methods which do not belong to {@link EjbMojo}. + * + * @author Karl Heinz Marbaise <khmarba...@apache.org> + */ +public class EjbHelper +{ + /** + * Check if a <code>classifier</code> is valid or not. + * + * @param classifier The classifier which should be checked. + * @return true in case of a valid classifier false otherwise. + */ + public static boolean isClassifierValid( String classifier ) + { + // @FIXME: Check classifier and clientClassifier for leading "-" ?? + // What are the rules for a valid classifier? Somewhere documented? which can be used as a reference? + boolean result = false; + + // The following check is only based on an educated guess ;-) + if ( classifier.matches( "^[a-zA-Z]+[0-9a-zA-Z\\-]*" ) ) + { + result = true; + } + + return result; + } + + /** + * Check if the given classifier exists in the meaning of not being {@code null} and contain something else than + * only white spaces. + * + * @param classifier The classifier to be used. + * @return true in case when the given classifier is not {@code null} and contains something else than white spaces. + */ + public static boolean hasClassifier( String classifier ) + { + boolean result = false; + if ( classifier != null && classifier.trim().length() > 0 ) + { + result = true; + } + return result; + } + + /** + * Returns the Jar file to generate, based on an optional classifier. + * + * @param basedir the output directory + * @param finalName the name of the ear file + * @param classifier an optional classifier + * @return the file to generate + */ + public static File getJarFileName( File basedir, String finalName, String classifier ) + { + StringBuilder fileName = new StringBuilder( finalName ); + + if ( hasClassifier( classifier ) ) + { + fileName.append( "-" ).append( classifier ); + } + + fileName.append( ".jar" ); + + return new File( basedir, fileName.toString() ); + } + +} Modified: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java?rev=1721772&r1=1721771&r2=1721772&view=diff ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java (original) +++ maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java Sat Dec 26 23:13:59 2015 @@ -67,6 +67,16 @@ public class EjbMojo new String[] { "**/*Bean.class", "**/*CMP.class", "**/*Session.class", "**/package.html" }; /** + * Default value for {@link #clientClassifier} + */ + public static final String DEFAULT_CLIENT_CLASSIFIER = "client"; + + /** + * Default value for {@link #ejbJar}. + */ + public static final String DEFAULT_EJBJAR = "META-INF/ejb-jar.xml"; + + /** * The directory location for the generated EJB. */ @Parameter( defaultValue = "${project.build.directory}", required = true, readonly = true ) @@ -91,19 +101,14 @@ public class EjbMojo private String classifier; /** - * Classifier to add to the client artifact generated. + * Classifier which is used for the client artifact. * * @since 3.0.0 */ - @Parameter( property = "maven.ejb.clientClassifier" ) + @Parameter( property = "maven.ejb.clientClassifier", defaultValue = DEFAULT_CLIENT_CLASSIFIER ) private String clientClassifier; /** - * Default value for {@link #ejbJar}. - */ - public static final String DEFAULT_EJBJAR = "META-INF/ejb-jar.xml"; - - /** * You can define the location of <code>ejb-jar.xml</code> file. */ @Parameter( property = "maven.ejb.ejbJar", defaultValue = DEFAULT_EJBJAR ) @@ -271,15 +276,18 @@ public class EjbMojo sourceDirectory.mkdirs(); } - getLog().info( "Building EJB " + jarName + " with EJB version " + ejbVersion ); - File jarFile = generateEjb(); - // Handle the classifier if necessary - // TODO: For 3.0 this should be changed having a separate classifier for main artifact and ejb-client. - if ( classifier != null ) + if ( hasClassifier() ) { - projectHelper.attachArtifact( project, "ejb", classifier, jarFile ); + if ( !isClassifierValid() ) + { + String message = "The given classifier '" + getClassifier() + "' is not valid."; + getLog().error( message ); + throw new MojoExecutionException( message ); + } + + projectHelper.attachArtifact( project, "ejb", getClassifier(), jarFile ); } else { @@ -289,15 +297,22 @@ public class EjbMojo if ( generateClient ) { File clientJarFile = generateEjbClient(); - // TODO: shouldn't need classifer - // TODO: For 3.0 this should be changed having a separate classifier for main artifact and ejb-client. - if ( classifier != null ) + if ( hasClientClassifier() ) { - projectHelper.attachArtifact( project, "ejb-client", classifier + "-client", clientJarFile ); + if ( !isClientClassifierValid() ) + { + String message = "The given client classifier '" + getClientClassifier() + "' is not valid."; + getLog().error( message ); + throw new MojoExecutionException( message ); + } + + projectHelper.attachArtifact( project, "ejb-client", getClientClassifier(), clientJarFile ); } else { - projectHelper.attachArtifact( project, "ejb-client", "client", clientJarFile ); + // FIXME: This does not make sense, cause a classifier for the client should always exist otherwise + // Failure! + projectHelper.attachArtifact( project, "ejb-client", getClientClassifier(), clientJarFile ); } } @@ -306,7 +321,9 @@ public class EjbMojo private File generateEjb() throws MojoExecutionException { - File jarFile = getEJBJarFile( outputDirectory, jarName, classifier ); + File jarFile = EjbHelper.getJarFileName( outputDirectory, jarName, getClassifier() ); + + getLog().info( "Building EJB " + jarName + " with EJB version " + ejbVersion ); MavenArchiver archiver = new MavenArchiver(); @@ -316,7 +333,6 @@ public class EjbMojo File deploymentDescriptor = new File( sourceDirectory, ejbJar ); - /* test EJB version compliance */ checkEJBVersionCompliance( deploymentDescriptor ); try @@ -332,6 +348,7 @@ public class EjbMojo archiver.getArchiver().addDirectory( sourceDirectory, DEFAULT_INCLUDES, mainJarExcludes ); + // FIXME: We should be able to filter more than just the deployment descriptor? if ( deploymentDescriptor.exists() ) { // EJB-34 Filter ejb-jar.xml @@ -374,14 +391,9 @@ public class EjbMojo private File generateEjbClient() throws MojoExecutionException { - String clientJarName = jarName; - if ( classifier != null ) - { - clientJarName += "-" + classifier; - } + File clientJarFile = EjbHelper.getJarFileName( outputDirectory, jarName, getClientClassifier() ); - String resultingClientJarNameWithClassifier = clientJarName + "-client"; - getLog().info( "Building EJB client " + resultingClientJarNameWithClassifier ); + getLog().info( "Building EJB client " + clientJarFile.getPath() ); String[] excludes = DEFAULT_CLIENT_EXCLUDES; String[] includes = DEFAULT_INCLUDES; @@ -396,8 +408,6 @@ public class EjbMojo excludes = (String[]) clientExcludes.toArray( new String[clientExcludes.size()] ); } - File clientJarFile = new File( outputDirectory, resultingClientJarNameWithClassifier + ".jar" ); - MavenArchiver clientArchiver = new MavenArchiver(); clientArchiver.setArchiver( clientJarArchiver ); @@ -471,33 +481,11 @@ public class EjbMojo } /** - * Returns the EJB Jar file to generate, based on an optional classifier. - * - * @param basedir the output directory - * @param finalName the name of the ear file - * @param classifier an optional classifier - * @return the EJB file to generate - */ - private static File getEJBJarFile( File basedir, String finalName, String classifier ) - { - if ( classifier == null ) - { - classifier = ""; - } - else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) ) - { - classifier = "-" + classifier; - } - - return new File( basedir, finalName + classifier + ".jar" ); - } - - /** * @return true in case where the classifier is not {@code null} and contains something else than white spaces. */ private boolean hasClassifier() { - return hasClassifier( getClassifier() ); + return EjbHelper.hasClassifier( getClassifier() ); } /** @@ -506,17 +494,17 @@ public class EjbMojo */ private boolean hasClientClassifier() { - return hasClassifier( getClientClassifier() ); + return EjbHelper.hasClassifier( getClientClassifier() ); } - private boolean hasClassifier( String classfier ) + private boolean isClassifierValid() { - boolean result = false; - if ( classfier != null && classfier.trim().length() > 0 ) - { - result = true; - } - return result; + return EjbHelper.isClassifierValid( getClassifier() ); + } + + private boolean isClientClassifierValid() + { + return EjbHelper.isClassifierValid( getClientClassifier() ); } /** Added: maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java?rev=1721772&view=auto ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java (added) +++ maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java Sat Dec 26 23:13:59 2015 @@ -0,0 +1,86 @@ +package org.apache.maven.plugins.ejb; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.File; + +import org.junit.Test; + +public class EjbHelperTest +{ + + @Test + public void validClassifier() + { + assertThat( EjbHelper.isClassifierValid( "anton" ), is( Boolean.TRUE ) ); + } + + @Test + public void anOtherValidClassifier() + { + assertThat( EjbHelper.isClassifierValid( "jdk15" ), is( Boolean.TRUE ) ); + } + + @Test + public void moreValidClassifier() + { + assertThat( EjbHelper.isClassifierValid( "client-classifier" ), is( Boolean.TRUE ) ); + } + + @Test + public void invalidClassifier() + { + assertThat( EjbHelper.isClassifierValid( "-anton" ), is( Boolean.FALSE ) ); + } + + @Test + public void hasClassifierShouldReturnFalseForNull() + { + assertThat( EjbHelper.hasClassifier( null ), is( Boolean.FALSE ) ); + } + + @Test + public void hasClassifierShouldReturnFalseForEmptyString() + { + assertThat( EjbHelper.hasClassifier( "" ), is( Boolean.FALSE ) ); + } + + @Test + public void hasClassifierShouldReturnTrueForNonEmptyString() + { + assertThat( EjbHelper.hasClassifier( "x" ), is( Boolean.TRUE ) ); + } + + @Test + public void getJarFileNameShouldReturnFileNameWithoutClassifier() + { + assertThat( EjbHelper.getJarFileName( new File( "base" ), "test", null ).getPath(), is( "base/test.jar" ) ); + } + + @Test + public void getJarFileNameShouldReturnFileNameWithClassifier() + { + assertThat( EjbHelper.getJarFileName( new File( "base" ), "test", "alpha" ).getPath(), + is( "base/test-alpha.jar" ) ); + } +} Modified: maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbMojoTest.java?rev=1721772&r1=1721771&r2=1721772&view=diff ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbMojoTest.java (original) +++ maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbMojoTest.java Sat Dec 26 23:13:59 2015 @@ -148,6 +148,7 @@ public class EjbMojoTest setVariableValueToObject( mojo, "generateClient", Boolean.TRUE ); setVariableValueToObject( mojo, "ejbVersion", "2.1" ); setVariableValueToObject( mojo, "classifier", "classified" ); + setVariableValueToObject( mojo, "clientClassifier", "classified-client" ); mojo.execute(); @@ -564,6 +565,7 @@ public class EjbMojoTest setVariableValueToObject( mojo, "clientExcludes", clientExcludes ); setVariableValueToObject( mojo, "clientIncludes", clientIncludes ); setVariableValueToObject( mojo, "excludes", excludes ); + setVariableValueToObject( mojo, "clientClassifier", EjbMojo.DEFAULT_CLIENT_CLASSIFIER); return mojo; }