Index: src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java =================================================================== --- src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java (revision 581946) +++ src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java (working copy) @@ -1,16 +1,17 @@ package org.apache.maven.plugin.war.packaging; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.war.Overlay; - import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; +import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.war.Overlay; + /** * Handles the artifacts that needs to be packaged in the web application. * @@ -41,6 +42,13 @@ { final ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME ); + + final Set artifacts = context.getProject().getArtifacts(); + + final List dependencies = context.getProject().getDependencies(); + + contributeOptionalToArtifacts( artifacts, dependencies ); + final List duplicates = findDuplicates( context, artifacts ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) @@ -125,4 +133,59 @@ } return duplicates; } + + + /** + * Method contributes the optional attribute to the given set of project artifacts if the artifact's artifactId, + * groupId, and type matches optional dependencies in the given list of dependencies. + * + * @param artifacts the set of artifacts to potentially contribute the optional flag + * @param dependencies the list of dependencies which may or may not declare the artifacts as optional + */ + private void contributeOptionalToArtifacts( Set artifacts, List dependencies ) + { + // Build sub list of optional dependencies + List optionalDependencies = getOptionalDependencies( dependencies ); + + // Loop over the dependencies and match on artifactId, groupId, type + Artifact artifact = null; + Dependency dependency = null; + for ( Iterator artifactIter = artifacts.iterator(); artifactIter.hasNext(); ) + { + artifact = (Artifact) artifactIter.next(); + for ( Iterator dependencyIter = optionalDependencies.iterator(); dependencyIter.hasNext(); ) + { + dependency = (Dependency) dependencyIter.next(); + if ( artifact.getArtifactId().equalsIgnoreCase( dependency.getArtifactId() ) && + artifact.getGroupId().equalsIgnoreCase( dependency.getGroupId() ) && + artifact.getType().equalsIgnoreCase( dependency.getType() ) ) + { + artifact.setOptional( dependency.isOptional() ); + } + } + } + } + + /** + * Returns a sub list of the "optional" Dependency objects given a list of project + * dependencies. + * + * @param dependencies a List of project dependencies + * @return a sub list of the passed in dependencies whose optional attribute is true + */ + private List getOptionalDependencies( List dependencies ) + { + // Build sub list of optional dependencies + List optionalDependencies = new ArrayList(); + Dependency dependency = null; + for ( Iterator iter = dependencies.iterator(); iter.hasNext(); ) + { + dependency = (Dependency) iter.next(); + if ( dependency.isOptional() ) + { + optionalDependencies.add( dependency ); + } + } + return optionalDependencies; + } }