Index: src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java (revision 575517) +++ src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java (working copy) @@ -23,6 +23,7 @@ import org.apache.maven.archiver.MavenArchiver; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; +import org.apache.maven.model.Dependency; import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -560,7 +561,11 @@ Set artifacts = project.getArtifacts(); List duplicates = findDuplicates( artifacts ); - + + List dependencies = project.getDependencies(); + + contributeOptionalToArtifacts( artifacts, dependencies ); + List dependentWarDirectories = new ArrayList(); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) @@ -656,8 +661,62 @@ } 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; + } + + /** * Unpacks war artifacts into a temporary directory inside workDirectory * named with the name of the war. *