diff --git a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarModule.java b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarModule.java new file mode 100644 index 0000000000..b06503e16f --- /dev/null +++ b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarModule.java @@ -0,0 +1,226 @@ +package org.apache.maven.plugin.ear; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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 org.apache.maven.artifact.Artifact; + +import java.util.Iterator; +import java.util.Set; + +/** + * A base implementation of an {@link EarModule}. + * + * @author Stephane Nicoll + * @version $Id$ + */ +public abstract class AbstractEarModule + implements EarModule +{ + + protected static final String MODULE_ELEMENT = "module"; + + private String uri; + + private Artifact artifact; + + // Those are set by the configuration + + private String groupId; + + private String artifactId; + + private String bundleDir; + + private String bundleFileName; + + /** + * Empty constructor to be used when the module + * is built based on the configuration. + */ + public AbstractEarModule() + { + } + + /** + * Creates an ear module from the artifact. + * + * @param a the artifact + */ + public AbstractEarModule( Artifact a ) + { + this.artifact = a; + this.groupId = a.getGroupId(); + this.artifactId = a.getArtifactId(); + this.bundleDir = null; + } + + public void resolveArtifact( Set artifacts ) + throws EarPluginException + { + if ( artifact != null ) + { + return; + } + else + { + if ( groupId == null || artifactId == null ) + { + throw new EarPluginException( + "Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]" ); + } + + Iterator i = artifacts.iterator(); + while ( i.hasNext() ) + { + Artifact a = (Artifact) i.next(); + if ( a.getGroupId().equals( groupId ) && a.getArtifactId().equals( artifactId ) && + a.getType().equals( getType() ) ) + { + artifact = a; + return; + } + } + + // Artifact has not been found + throw new EarPluginException( "Artifact[" + groupId + ":" + artifactId + ":" + getType() + "] " + + "is not a dependency of the project." ); + } + } + + /** + * Returns the type associated to the module. + * + * @return the artifact's type of the module + */ + protected abstract String getType(); + + public Artifact getArtifact() + { + return artifact; + } + + public String getUri() + { + if ( uri == null ) + { + if ( getBundleDir() == null ) + { + uri = getBundleFileName(); + } + else + { + uri = getBundleDir() + getBundleFileName(); + } + } + return uri; + } + + /** + * Returns the artifact's groupId. + * + * @return the group Id + */ + public String getGroupId() + { + return groupId; + } + + /** + * Returns the artifact's Id. + * + * @return the artifact Id + */ + public String getArtifactId() + { + return artifactId; + } + + /** + * Returns the bundle directory. If null, the module + * is bundled in the root of the EAR. + * + * @return the custom bundle directory + */ + public String getBundleDir() + { + if ( bundleDir != null ) + { + bundleDir = cleanBundleDir( bundleDir ); + } + return bundleDir; + } + + /** + * Returns the bundle file name. If null, the artifact's + * file name is returned. + * + * @return the bundle file name + */ + public String getBundleFileName() + { + if ( bundleFileName == null ) + { + bundleFileName = artifact.getFile().getName(); + } + return bundleFileName; + } + + public String toString() + { + StringBuffer sb = new StringBuffer(); + sb.append( getType() ).append( ":" ).append( groupId ).append( ":" ).append( artifactId ); + if ( artifact != null ) + { + sb.append( ":" ).append( artifact.getVersion() ); + } + return sb.toString(); + } + + /** + * Cleans the bundle directory so that it might be used + * properly. + * + * @param bundleDir the bundle directory to clean + * @return the cleaned bundle directory + */ + private static String cleanBundleDir( String bundleDir ) + { + if ( bundleDir == null ) + { + return bundleDir; + } + + // Using slashes + bundleDir = bundleDir.replace( '\\', '/' ); + + // Remove '/' prefix if any so that directory is a relative path + if ( bundleDir.startsWith( " / " ) ) + { + bundleDir = bundleDir.substring( 1, bundleDir.length() ); + } + + // Adding '/' suffix to specify a directory structure + if ( !bundleDir.endsWith( "/" ) ) + { + bundleDir = bundleDir + "/"; + } + + System.out.println( "Bundle dir[" + bundleDir + "]" ); + + return bundleDir; + } +} diff --git a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java index 12450e5169..ff3ccd046e 100644 --- a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java +++ b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java @@ -18,12 +18,12 @@ package org.apache.maven.plugin.ear; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.ear.module.EarModule; -import org.apache.maven.plugin.ear.module.EarModuleFactory; +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Set; @@ -46,48 +46,91 @@ public abstract class AbstractEarMojo * @parameter expression="${project}" * @required * @readonly - * @description "the maven project to use" */ private MavenProject project; + /** + * The ear modules configuration. + * + * @parameter + */ + private EarModule[] modules; + /** * Directory that resources are copied to during the build. * * @parameter expression="${project.build.directory}/${project.build.finalName}" * @required */ - private String earDirectory; + private String workDirectory; - private List modules; + private List earModules; private File buildDir; - protected List getModules() + public void execute() + throws MojoExecutionException { - if ( modules == null ) + getLog().debug( "Resolving ear modules ..." ); + + if ( modules != null && modules.length > 0 ) { - // Gather modules and copy them - modules = new ArrayList(); - Set artifacts = project.getArtifacts(); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + + // Let's validate user-defined modules + EarModule module = null; + try { - Artifact artifact = (Artifact) iter.next(); - if ( !Artifact.SCOPE_TEST.equals( artifact.getScope()) || - !Artifact.SCOPE_PROVIDED.equals( artifact.getScope()) ) + for ( int i = 0; i < modules.length; i++ ) { - EarModule module = EarModuleFactory.newEarModule( artifact ); - modules.add( module ); + module = (EarModule) modules[i]; + getLog().debug( "Resolving ear module[" + module + "]" ); + module.resolveArtifact( project.getArtifacts() ); } } + catch ( EarPluginException e ) + { + throw new MojoExecutionException( "Failed to initialize ear modules", e ); + } + earModules = new ArrayList( Arrays.asList( modules ) ); } - return modules; + else + { + earModules = new ArrayList(); + } + + // Let's add other modules + Set artifacts = project.getArtifacts(); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + + // Artifact is not yet registered and it has neither test, nor a + // provided scope + if ( !isArtifactRegistered( artifact, earModules ) && ( + !Artifact.SCOPE_TEST.equals( artifact.getScope() ) || + !Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) ) ) + { + EarModule module = EarModuleFactory.newEarModule( artifact ); + earModules.add( module ); + } + } + + } + + protected List getModules() + { + if ( earModules == null ) + { + throw new IllegalStateException( "Ear modules have not been initialized" ); + } + return earModules; } protected File getBuildDir() { if ( buildDir == null ) { - buildDir = new File( earDirectory ); + buildDir = new File( workDirectory ); } return buildDir; } @@ -97,8 +140,22 @@ public abstract class AbstractEarMojo return project; } - protected String getEarDirectory() + protected String getWorkDirectory() { - return earDirectory; + return workDirectory; + } + + private static boolean isArtifactRegistered( Artifact a, List currentList ) + { + Iterator i = currentList.iterator(); + while ( i.hasNext() ) + { + EarModule em = (EarModule) i.next(); + if ( em.getArtifact().equals( a ) ) + { + return true; + } + } + return false; } } diff --git a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java index 050c583f7b..0241396d71 100644 --- a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java +++ b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java @@ -1,6 +1,5 @@ package org.apache.maven.plugin.ear; -import org.apache.maven.plugin.ear.module.EarModule; import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; import org.codehaus.plexus.util.xml.XMLWriter; diff --git a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModule.java b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModule.java new file mode 100644 index 0000000000..fe388edbe4 --- /dev/null +++ b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModule.java @@ -0,0 +1,68 @@ +package org.apache.maven.plugin.ear; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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 org.apache.maven.artifact.Artifact; +import org.codehaus.plexus.util.xml.XMLWriter; + +import java.util.Set; + +/** + * The ear module interface. + * + * @author Stephane Nicoll + * @version $Id$ + */ +public interface EarModule +{ + + /** + * Returns the {@link Artifact} representing this module. + *
+ * Note that this might return null till the + * module has been resolved. + * + * @return the artifact + * @see #resolveArtifact(java.util.Set) + */ + public Artifact getArtifact(); + + /** + * Returns the URI for this module. + * + * @return the URI + */ + public String getUri(); + + /** + * Appends the XML representation of this module. + * + * @param writer the writer to use + * @param version the version of the application.xml file + */ + public void appendModule( XMLWriter writer, String version ); + + /** + * Resolves the {@link Artifact} represented by the module. + * + * @param artifacts the project's artifacts + * @throws EarPluginException if the artifact could not be resolved + */ + public void resolveArtifact( Set artifacts ) + throws EarPluginException; + +} diff --git a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModuleFactory.java b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModuleFactory.java new file mode 100644 index 0000000000..f7ff0aca49 --- /dev/null +++ b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModuleFactory.java @@ -0,0 +1,65 @@ +package org.apache.maven.plugin.ear; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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 org.apache.maven.artifact.Artifact; + +/** + * Builds an {@link EarModule} based on an Artifact. + * + * @author Stephane Nicoll + * @version $Id$ + */ +public final class EarModuleFactory +{ + + /** + * Creates a new {@link EarModule} based on the + * specified {@link Artifact}. + * + * @param artifact the artifact + * @return an ear module for this artifact + */ + public static final EarModule newEarModule( Artifact artifact ) + { + if ( "jar".equals( artifact.getType() ) ) + { + return new JavaModule( artifact ); + } + else if ( "ejb".equals( artifact.getType() ) ) + { + return new EjbModule( artifact ); + } + else if ( "ejb-client".equals( artifact.getType() ) ) + { + return new EjbClientModule( artifact ); + } + else if ( "rar".equals( artifact.getType() ) ) + { + return new RarModule( artifact ); + } + else if ( "war".equals( artifact.getType() ) ) + { + return new WebModule( artifact ); + } + else + { + throw new IllegalStateException( "Could not handle artifact type[" + artifact.getType() + "]" ); + } + } + +} diff --git a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java index 8f4ca107d4..82f20cf41a 100644 --- a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java +++ b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java @@ -19,7 +19,6 @@ package org.apache.maven.plugin.ear; import org.apache.maven.archiver.MavenArchiveConfiguration; import org.apache.maven.archiver.MavenArchiver; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.ear.module.EarModule; import org.codehaus.plexus.util.FileUtils; import java.io.File; @@ -32,7 +31,7 @@ import java.util.List; * Builds J2EE Enteprise Archive (EAR) files. * * @author Stephane Nicoll - * @version $Id $ + * @version $Id$ * @goal ear * @phase package * @requiresDependencyResolution test @@ -77,7 +76,7 @@ public class EarMojo * * @parameter alias="earName" expression="${project.build.finalName}" * @required - * @readonly + * @readonly */ private String finalName; @@ -100,11 +99,14 @@ public class EarMojo public void execute() throws MojoExecutionException { + // Initializes ear modules + super.execute(); + getLog().debug( " ======= EarMojo settings =======" ); getLog().debug( "earSourceDirectory[" + earSourceDirectory + "]" ); getLog().debug( "manifestLocation[" + manifestLocation + "]" ); getLog().debug( "applicationXmlLocation[" + applicationXmlLocation + "]" ); - getLog().debug( "earDirectory[" + getEarDirectory() + "]" ); + getLog().debug( "workDirectory[" + getWorkDirectory() + "]" ); getLog().debug( "outputDirectory[" + outputDirectory + "]" ); getLog().debug( "finalName[" + finalName + "]" ); getLog().debug( "excludedDependencies[" + excludedDependencies + "]" ); @@ -115,9 +117,9 @@ public class EarMojo for ( Iterator iter = getModules().iterator(); iter.hasNext(); ) { EarModule module = (EarModule) iter.next(); - getLog().info( "Copying artifact[" + module.getArtifact().getGroupId() + ", " + - module.getArtifact().getId() + ", " + module.getArtifact().getType() + "]" ); - FileUtils.copyFileToDirectory( module.getArtifact().getFile(), getBuildDir() ); + getLog().info( "Copying artifact[" + module + "] to[" + module.getUri() + "]" ); + File destinationFile = buildDestinationFile( getBuildDir(), module.getUri() ); + FileUtils.copyFile( module.getArtifact().getFile(), destinationFile ); } } catch ( IOException e ) @@ -144,7 +146,8 @@ public class EarMojo File ddFile = new File( getBuildDir(), APPLICATION_XML_URI ); if ( !ddFile.exists() ) { - throw new MojoExecutionException( "Deployment descriptor: " + ddFile.getAbsolutePath() + " does not exist." ); + throw new MojoExecutionException( + "Deployment descriptor: " + ddFile.getAbsolutePath() + " does not exist." ); } try @@ -161,4 +164,9 @@ public class EarMojo throw new MojoExecutionException( "Error assembling EAR", e ); } } + + private static File buildDestinationFile( File buildDir, String uri ) + { + return new File( buildDir, uri ); + } } \ No newline at end of file diff --git a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarPluginException.java b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarPluginException.java index a8e935fc9c..929782a0d9 100644 --- a/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarPluginException.java +++ b/maven-plugins/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarPluginException.java @@ -19,9 +19,8 @@ package org.apache.maven.plugin.ear; /** * The base exception of the EAR plugin. * - * @author Stephane Nicoll