Index: maven-project/src/main/java/org/apache/maven/project/MavenProject.java =================================================================== --- maven-project/src/main/java/org/apache/maven/project/MavenProject.java (revision 749018) +++ maven-project/src/main/java/org/apache/maven/project/MavenProject.java (working copy) @@ -1771,17 +1771,17 @@ { String refId = getProjectReferenceId( pluginArtifact.getGroupId(), pluginArtifact.getArtifactId(), pluginArtifact.getVersion() ); MavenProject ref = (MavenProject) getProjectReferences().get( refId ); - if ( ref != null && ref.getArtifact() != null ) + if ( ref != null ) { - // TODO: if not matching, we should get the correct artifact from that project (attached) - if ( ref.getArtifact().getDependencyConflictId().equals( pluginArtifact.getDependencyConflictId() ) ) + if ( ref.getArtifact() != null + && ref.getArtifact().getDependencyConflictId().equals( pluginArtifact.getDependencyConflictId() ) ) { // if the project artifact doesn't exist, don't use it. We haven't built that far. if ( ref.getArtifact().getFile() != null && ref.getArtifact().getFile().exists() ) { // FIXME: Why aren't we using project.getArtifact() for the second parameter here?? - pluginArtifact = new ActiveProjectArtifact( ref, pluginArtifact ); - return pluginArtifact; + Artifact resultArtifact = new ActiveProjectArtifact( ref, pluginArtifact ); + return resultArtifact; } else { @@ -1789,31 +1789,90 @@ } } - Iterator itr = ref.getAttachedArtifacts().iterator(); - while ( itr.hasNext() ) + Artifact attached = findMatchingArtifact( ref.getAttachedArtifacts(), pluginArtifact ); + if ( attached != null ) { - Artifact attached = (Artifact) itr.next(); - if ( attached.getDependencyConflictId().equals( pluginArtifact.getDependencyConflictId() ) ) + if ( attached.getFile() != null && attached.getFile().exists() ) { - if ( attached.getFile() != null && attached.getFile().exists() ) - { - Artifact resultArtifact = ArtifactUtils.copyArtifact( attached ); - resultArtifact.setScope( pluginArtifact.getScope() ); - - return resultArtifact; - } - else - { - logMissingSiblingProjectArtifact( pluginArtifact ); - break; - } + Artifact resultArtifact = ArtifactUtils.copyArtifact( attached ); + resultArtifact.setScope( pluginArtifact.getScope() ); + return resultArtifact; } + else + { + logMissingSiblingProjectArtifact( pluginArtifact ); + } } } } return pluginArtifact; } - + + /** + * Tries to resolve the specified artifact from the given collection of attached project artifacts. + * + * @param artifacts The attached artifacts, may be null. + * @param requestedArtifact The artifact to resolve, must not be null. + * @return The matching artifact or null if not found. + */ + private Artifact findMatchingArtifact( List artifacts, Artifact requestedArtifact ) + { + if ( artifacts != null && !artifacts.isEmpty() ) + { + // first try matching by dependency conflict id + String requestedId = requestedArtifact.getDependencyConflictId(); + for ( Iterator it = artifacts.iterator(); it.hasNext(); ) + { + Artifact artifact = (Artifact) it.next(); + if ( requestedId.equals( artifact.getDependencyConflictId() ) ) + { + return artifact; + } + } + + // next try matching by repository conflict id + requestedId = getRepositoryConflictId( requestedArtifact ); + for ( Iterator it = artifacts.iterator(); it.hasNext(); ) + { + Artifact artifact = (Artifact) it.next(); + if ( requestedId.equals( getRepositoryConflictId( artifact ) ) ) + { + return artifact; + } + } + } + + return null; + } + + /** + * Gets the repository conflict id of the specified artifact. Unlike the dependency conflict id, the repository + * conflict id uses the artifact file extension instead of the artifact type. Hence, the repository conflict id more + * closely reflects the identity of artifacts as perceived by a repository. + * + * @param artifact The artifact, must not be null. + * @return The repository conflict id, never null. + */ + private String getRepositoryConflictId( Artifact artifact ) + { + StringBuffer buffer = new StringBuffer( 128 ); + buffer.append( artifact.getGroupId() ); + buffer.append( ':' ).append( artifact.getArtifactId() ); + if ( artifact.getArtifactHandler() != null ) + { + buffer.append( ':' ).append( artifact.getArtifactHandler().getExtension() ); + } + else + { + buffer.append( ':' ).append( artifact.getType() ); + } + if ( artifact.hasClassifier() ) + { + buffer.append( ':' ).append( artifact.getClassifier() ); + } + return buffer.toString(); + } + private void logMissingSiblingProjectArtifact( Artifact artifact ) { if ( logger == null )