From d3ace78602405079d6416a63c13216568ba97995 Mon Sep 17 00:00:00 2001 From: Stefan Oehme Date: Sun, 14 Apr 2019 13:47:05 +0200 Subject: [PATCH] [MNG-6638] Prevent reparsing POMs in MavenMetadataSource If a dependency on another project is found, don't parse its POM again, but instead use the model we already have in memory. This saves a large amount of time and memory for builds that have lots of subprojects and dependencies between them. The approach was directly copied from DefaultArtifactDescriptorReader, which does the same optimization. This closes #244 --- .../project/artifact/MavenMetadataSource.java | 22 ++++++++++++++++--- .../DefaultArtifactDescriptorReader.java | 1 - 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index d002f1766a..6cf9ec5060 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; @@ -55,6 +56,7 @@ import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.DistributionManagement; +import org.apache.maven.model.Model; import org.apache.maven.model.Relocation; import org.apache.maven.model.building.ModelBuildingException; import org.apache.maven.model.building.ModelBuildingRequest; @@ -68,6 +70,7 @@ import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.properties.internal.EnvironmentUtils; import org.apache.maven.properties.internal.SystemProperties; +import org.apache.maven.repository.internal.MavenWorkspaceReader; import org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest; import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.codehaus.plexus.PlexusContainer; @@ -77,6 +80,7 @@ import org.codehaus.plexus.component.repository.exception.ComponentLookupExcepti import org.codehaus.plexus.logging.Logger; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.repository.RepositoryPolicy; +import org.eclipse.aether.repository.WorkspaceReader; import org.eclipse.aether.transfer.ArtifactNotFoundException; /** @@ -174,9 +178,21 @@ public class MavenMetadataSource Artifact relocatedArtifact = null; - //TODO Not even sure this is really required as the project will be cached in the builder, we'll see this - // is currently the biggest hotspot - if ( artifact instanceof ArtifactWithDependencies ) + final WorkspaceReader workspace = legacySupport.getRepositorySession().getWorkspaceReader(); + Model model = null; + if ( workspace instanceof MavenWorkspaceReader ) + { + model = ( (MavenWorkspaceReader) workspace ).findModel( RepositoryUtils.toArtifact( artifact ) ); + } + + if ( model != null ) + { + pomArtifact = artifact; + dependencies = model.getDependencies(); + DependencyManagement dependencyManagement = model.getDependencyManagement(); + managedDependencies = dependencyManagement == null ? null : dependencyManagement.getDependencies(); + } + else if ( artifact instanceof ArtifactWithDependencies ) { pomArtifact = artifact; diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java index 4f464ea8d9..586c83e271 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java @@ -257,7 +257,6 @@ public class DefaultArtifactDescriptorReader Model model; - // hack: don't rebuild model if it was already loaded during reactor resolution final WorkspaceReader workspace = session.getWorkspaceReader(); if ( workspace instanceof MavenWorkspaceReader ) {