From 4c0c5f3edc45ffbf273ed7096340161def8515e4 Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Tue, 20 Aug 2013 05:54:28 -0700 Subject: [PATCH] MNG-5503: Fix for the issue where Maven 3.1.0 fails to resolve artifacts produced by reactor build The general strategy is to fall back to Aether artifact type and use its notion of identity as much as possible. I have a simple IT taken from the sample project that I will also push. --- .../main/java/org/apache/maven/ReactorReader.java | 244 +++++++-------------- .../java/org/apache/maven/RepositoryUtils.java | 10 + 2 files changed, 93 insertions(+), 161 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/ReactorReader.java b/maven-core/src/main/java/org/apache/maven/ReactorReader.java index 90d102f..9b19e27 100644 --- a/maven-core/src/main/java/org/apache/maven/ReactorReader.java +++ b/maven-core/src/main/java/org/apache/maven/ReactorReader.java @@ -19,12 +19,6 @@ * under the License. */ -import org.apache.maven.artifact.ArtifactUtils; -import org.apache.maven.project.MavenProject; -import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.repository.WorkspaceReader; -import org.eclipse.aether.repository.WorkspaceRepository; - import java.io.File; import java.util.ArrayList; import java.util.Arrays; @@ -35,6 +29,13 @@ import java.util.List; import java.util.Map; +import org.apache.maven.artifact.ArtifactUtils; +import org.apache.maven.project.MavenProject; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.repository.WorkspaceReader; +import org.eclipse.aether.repository.WorkspaceRepository; +import org.eclipse.aether.util.artifact.ArtifactIdUtils; + /** * An implementation of a workspace reader that knows how to search the Maven reactor for artifacts. * @@ -43,8 +44,6 @@ class ReactorReader implements WorkspaceReader { - private static final Collection JAR_LIKE_TYPES = Arrays.asList( "jar", "test-jar", "ejb-client" ); - private static final Collection COMPILE_PHASE_TYPES = Arrays.asList( "jar", "ejb-client" ); private Map projectsByGAV; @@ -52,7 +51,7 @@ private Map> projectsByGA; private WorkspaceRepository repository; - + public ReactorReader( Map reactorProjects ) { projectsByGAV = reactorProjects; @@ -73,9 +72,64 @@ public ReactorReader( Map reactorProjects ) projects.add( project ); } - repository = new WorkspaceRepository( "reactor", new HashSet( projectsByGAV.keySet() ) ); + repository = new WorkspaceRepository( "reactor", new HashSet( projectsByGAV.keySet() ) ); + } + + // + // Public API + // + + public WorkspaceRepository getRepository() + { + return repository; + } + + public File findArtifact( Artifact artifact ) + { + String projectKey = ArtifactUtils.key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); + + MavenProject project = projectsByGAV.get( projectKey ); + + if ( project != null ) + { + File file = find( project, artifact ); + if ( file == null && project != project.getExecutionProject() ) + { + file = find( project.getExecutionProject(), artifact ); + } + return file; + } + + return null; } + public List findVersions( Artifact artifact ) + { + String key = ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() ); + + List projects = projectsByGA.get( key ); + if ( projects == null || projects.isEmpty() ) + { + return Collections.emptyList(); + } + + List versions = new ArrayList(); + + for ( MavenProject project : projects ) + { + if ( find( project, artifact ) != null ) + { + versions.add( project.getVersion() ); + } + } + + return Collections.unmodifiableList( versions ); + } + + // + // Implementation + // + private File find( MavenProject project, Artifact artifact ) { if ( "pom".equals( artifact.getExtension() ) ) @@ -83,7 +137,7 @@ private File find( MavenProject project, Artifact artifact ) return project.getFile(); } - org.apache.maven.artifact.Artifact projectArtifact = findMatchingArtifact( project, artifact ); + Artifact projectArtifact = findMatchingArtifact( project, artifact ); if ( hasArtifactFileFromPackagePhase( projectArtifact ) ) { @@ -116,7 +170,7 @@ else if ( !hasBeenPackaged( project ) ) return null; } - private boolean hasArtifactFileFromPackagePhase( org.apache.maven.artifact.Artifact projectArtifact ) + private boolean hasArtifactFileFromPackagePhase( Artifact projectArtifact ) { return projectArtifact != null && projectArtifact.getFile() != null && projectArtifact.getFile().exists(); } @@ -136,122 +190,38 @@ private boolean hasBeenPackaged( MavenProject project ) * * Note that this */ - private org.apache.maven.artifact.Artifact findMatchingArtifact( MavenProject project, Artifact requestedArtifact ) + private Artifact findMatchingArtifact( MavenProject project, Artifact requestedArtifact ) { - String requestedRepositoryConflictId = getConflictId( requestedArtifact ); + String requestedRepositoryConflictId = ArtifactIdUtils.toVersionlessId( requestedArtifact ); - org.apache.maven.artifact.Artifact mainArtifact = project.getArtifact(); - if ( requestedRepositoryConflictId.equals( getConflictId( mainArtifact ) ) ) + Artifact mainArtifact = RepositoryUtils.toArtifact( project.getArtifact() ); + if ( requestedRepositoryConflictId.equals( ArtifactIdUtils.toVersionlessId( mainArtifact ) ) ) { return mainArtifact; } - Collection attachedArtifacts = project.getAttachedArtifacts(); - if ( attachedArtifacts != null && !attachedArtifacts.isEmpty() ) + for ( Artifact attachedArtifact : RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ) ) { - for ( org.apache.maven.artifact.Artifact attachedArtifact : attachedArtifacts ) + if ( attachedArtifactComparison ( requestedArtifact, attachedArtifact ) ) { - /* - * Don't use the conflict ids, use a customized comparison that takes various ideas into account. - */ - if ( attachedArtifactComparison ( requestedArtifact, attachedArtifact ) ) - { - return attachedArtifact; - } + return attachedArtifact; } } return null; } - - /** - * Try to satisfy both MNG-4065 and MNG-5214. Consider jar and test-jar equivalent. - * @param requestedType - * @param artifactType - * @return - */ - private boolean attachedArtifactComparison ( Artifact requestedArtifact, org.apache.maven.artifact.Artifact attachedArtifact ) - { - if ( ! requestedArtifact.getGroupId().equals ( attachedArtifact.getGroupId() ) ) - { - return false; - } - if ( ! requestedArtifact.getArtifactId().equals ( attachedArtifact.getArtifactId() ) ) - { - return false; - } - String requestedExtension = requestedArtifact.getExtension(); - String attachedExtension = null; - if ( attachedArtifact.getArtifactHandler() != null ) - { - attachedExtension = attachedArtifact.getArtifactHandler().getExtension(); - } - String requestedType = requestedArtifact.getProperty ( "type", "" ); - String attachedType = attachedArtifact.getType(); - boolean typeOk = false; - - if ( requestedExtension.equals ( attachedExtension ) ) - { - // the ideal case. - typeOk = true; - } - else if ( requestedType.equals( attachedType ) ) - { - typeOk = true; - } - else if ( JAR_LIKE_TYPES.contains( requestedType ) && JAR_LIKE_TYPES.contains( attachedType ) ) - { - typeOk = true; - } - if ( !typeOk ) - { - return false; - } - return requestedArtifact.getClassifier().equals ( attachedArtifact.getClassifier() ); - } - - /** - * 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 getConflictId( org.apache.maven.artifact.Artifact artifact ) + private boolean attachedArtifactComparison( Artifact requested, Artifact attached ) { - StringBuilder buffer = new StringBuilder( 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 String getConflictId( Artifact artifact ) - { - StringBuilder buffer = new StringBuilder( 128 ); - buffer.append( artifact.getGroupId() ); - buffer.append( ':' ).append( artifact.getArtifactId() ); - buffer.append( ':' ).append( artifact.getExtension() ); - if ( artifact.getClassifier().length() > 0 ) - { - buffer.append( ':' ).append( artifact.getClassifier() ); - } - return buffer.toString(); - } - + // + // We are taking as much as we can from the DefaultArtifact.equals(). The requested artifact has no file so + // we want to remove that from the comparision. + // + return requested.getArtifactId().equals( attached.getArtifactId() ) && requested.getGroupId().equals( attached.getGroupId() ) + && requested.getVersion().equals( attached.getVersion() ) && requested.getExtension().equals( attached.getExtension() ) + && requested.getClassifier().equals( attached.getClassifier() ); + } + /** * Determines whether the specified artifact refers to test classes. * @@ -263,52 +233,4 @@ private static boolean isTestArtifact( Artifact artifact ) return ( "test-jar".equals( artifact.getProperty( "type", "" ) ) ) || ( "jar".equals( artifact.getExtension() ) && "tests".equals( artifact.getClassifier() ) ); } - - public File findArtifact( Artifact artifact ) - { - String projectKey = ArtifactUtils.key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); - - MavenProject project = projectsByGAV.get( projectKey ); - - if ( project != null ) - { - File file = find( project, artifact ); - if ( file == null && project != project.getExecutionProject() ) - { - file = find( project.getExecutionProject(), artifact ); - } - return file; - } - - return null; - } - - public List findVersions( Artifact artifact ) - { - String key = ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() ); - - List projects = projectsByGA.get( key ); - if ( projects == null || projects.isEmpty() ) - { - return Collections.emptyList(); - } - - List versions = new ArrayList(); - - for ( MavenProject project : projects ) - { - if ( find( project, artifact ) != null ) - { - versions.add( project.getVersion() ); - } - } - - return Collections.unmodifiableList( versions ); - } - - public WorkspaceRepository getRepository() - { - return repository; - } - } diff --git a/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java b/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java index 9b68a2e..c966e9a 100644 --- a/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java +++ b/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java @@ -350,4 +350,14 @@ public ArtifactType get( String stereotypeId ) } + public static Collection toArtifacts(Collection artifactsToConvert ) + { + List artifacts = new ArrayList(); + for( org.apache.maven.artifact.Artifact a : artifactsToConvert ) + { + artifacts.add(toArtifact(a)); + } + return artifacts; + } + } -- 1.8.1.4