Index: src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java (revision 792490) +++ src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java (working copy) @@ -19,22 +19,30 @@ * under the License. */ +import java.io.File; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.ear.util.ArtifactTypeMappingService; import org.apache.maven.project.MavenProject; +import org.apache.maven.project.artifact.ActiveProjectArtifact; +import org.apache.maven.project.artifact.InvalidDependencyVersionException; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; -import java.io.File; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - /** * A base class for EAR-processing related tasks. * @@ -152,7 +160,48 @@ private List allModules; private JbossConfiguration jbossConfiguration; + + /** + * If the executed project is a reactor project, this will contains the full list of projects in the reactor. + * + * @parameter expression="${reactorProjects}" + * @required + * @readonly + */ + protected List reactorProjects; + /** + * Indicator whether to use project references. Default true. + * + * @parameter default-value="false" + */ + protected boolean useProjectReferences; + + /** + * Artifact resolver. Used to resolve artifacts in local repository. + * + * @component role="org.apache.maven.artifact.resolver.ArtifactResolver" + * @required + * @readonly + */ + protected ArtifactResolver artifactResolver; + + /** + * Artifact factory. Used to determine dependencies. + * + * @component role="org.apache.maven.artifact.factory.ArtifactFactory" + * @required + * @readonly + */ + protected ArtifactFactory artifactFactory; + + /** + * Maven Session. Needed to determine local repository location. + * + * @parameter expression="${session}" + */ + protected MavenSession session; + public void execute() throws MojoExecutionException, MojoFailureException { @@ -179,6 +228,20 @@ { throw new MojoExecutionException( "Failed to initialize JBoss configuration", e ); } + + // resolve dependencies if not already resolved. + try { + if( null == project.getArtifacts() || project.getArtifacts().isEmpty() ) { + getLog().info("Resolving project artifacts (" + + (useProjectReferences ? "using" : "without") + + " project references) ..."); + resolveDependencies(); + } else { + getLog().info("Project artifacts already resolved."); + } + } catch(Exception e) { // catch all exceptions + throw new MojoExecutionException("error resolving dependencies", e); + } getLog().debug( "Initializing ear execution context" ); EarExecutionContext.getInstance().initialize( project, mainArtifactId, defaultLibBundleDir, jbossConfiguration, @@ -352,4 +415,95 @@ } } } + + /** + * Resolv project dependencies.
+ * If using project references, then try to resolv those first. First try to + * resolv them in the local repository, otherwise via reactor. + * Then try to resolv all unresolved dependencies via the local repository. + * Since it makes no sense for the ear project to include libraries which are + * not used by included projects, we assume all 3rd party libraries are already + * downloaded by the other projects, so no need to download them here. + *

+ * @throws InvalidDependencyVersionException + * @throws ArtifactResolutionException + * @throws ArtifactNotFoundException + */ + private void resolveDependencies() throws InvalidDependencyVersionException, ArtifactResolutionException, ArtifactNotFoundException { + + Set unresolvedArtifacts = project.createArtifacts( artifactFactory, Artifact.SCOPE_TEST, null ); + + List remoteRepositories = new ArrayList(); // empty list of remote repo's + Set resolvedArtifacts = new HashSet(); + Iterator it = unresolvedArtifacts.iterator(); + while( it.hasNext() ) { + Artifact artifact = (Artifact) it.next(); + + // resolv artifact + try { + artifactResolver.resolve(artifact, remoteRepositories, session.getLocalRepository()); + resolvedArtifacts.add(artifact); + } catch(ArtifactNotFoundException artifactException) { + Artifact projectArtifact = null; + if( useProjectReferences ) { + // try to resolv artifact via reactor + projectArtifact = getProjectArtifact( artifact ); + } + if( null != projectArtifact ) { + resolvedArtifacts.add( projectArtifact ); + } else { + // rethrow exception + throw artifactException; + } + } + } + + project.setArtifacts( resolvedArtifacts ); + } + + /** + * Get artifact from reactor. + * + * @param groupId + * @param artifactId + * @param version + * @param type + * @return Project artifact or null if not found + */ + private Artifact getProjectArtifact(Artifact artifact) { + + String groupId = artifact.getGroupId(); + String artifactId = artifact.getArtifactId(); + String version = artifact.getVersion(); + String type = artifact.getType(); + + ArtifactTypeMappingService artifactTypeMappingService = ArtifactTypeMappingService.getInstance(); + Iterator reactorIt = reactorProjects.iterator(); + while( reactorIt.hasNext() ) { + MavenProject project = (MavenProject) reactorIt.next(); + + if(project.getGroupId().equals( groupId) && project.getArtifactId().equals( artifactId ) + && project.getVersion().equals( version ) + && artifactTypeMappingService.isMappedToType( type, project.getPackaging() )) { + + Artifact projectArtifact = new ActiveProjectArtifact(project, artifact); + + // determine file extention. Default same as packaging. + String extention = project.getPackaging(); + if("ejb".equals(project.getPackaging())) { + // exception: for EJB projects is 'jar' + extention = "jar"; + } + String filename = project.getModel().getBuild().getDirectory() + + "/" + project.getModel().getBuild().getFinalName() + + "." + extention; + File artifactFile = new File(filename); + projectArtifact.setFile( artifactFile ); + projectArtifact.setResolved( true ); + + return projectArtifact; + } + } + return null; + } } Index: src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java (revision 792490) +++ src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java (working copy) @@ -37,7 +37,6 @@ * @version $Id$ * @goal generate-application-xml * @phase generate-resources - * @requiresDependencyResolution test */ public class GenerateApplicationXmlMojo extends AbstractEarMojo