From 964fcc39e1f35a55768feca261260e5188baa3df Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Tue, 1 Jun 2021 17:34:57 +0200 Subject: [PATCH 1/3] [MNG-6843] Use streams to implement access to artifacts and dependencies in MavenProject This make the code shorter and easier to understand and also removes the risk of hitting the 2000 lines barrier for a single file --- .../apache/maven/project/MavenProject.java | 345 +++++------------- 1 file changed, 101 insertions(+), 244 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 57069a5e6a9a..95f8f5a3b268 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -32,6 +32,9 @@ import java.util.Properties; import java.util.Set; import java.util.Objects; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; @@ -350,81 +353,38 @@ public List getTestCompileSourceRoots() public List getCompileClasspathElements() throws DependencyResolutionRequiredException { - List list = new ArrayList<>( getArtifacts().size() + 1 ); - - String d = getBuild().getOutputDirectory(); - if ( d != null ) - { - list.add( d ); - } - - for ( Artifact a : getArtifacts() ) - { - if ( a.getArtifactHandler().isAddedToClasspath() ) - { - // TODO let the scope handler deal with this - if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) - || Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) - { - addArtifactPath( a, list ); - } - } - } - - return list; + return Stream.concat( + ofNullable( getBuild().getOutputDirectory() ), + getClasspathElements( MavenProject::compileScope ) + ).collect( Collectors.toList() ); } - // TODO this checking for file == null happens because the resolver has been confused about the root - // artifact or not. things like the stupid dummy artifact coming from surefire. public List getTestClasspathElements() throws DependencyResolutionRequiredException { - List list = new ArrayList<>( getArtifacts().size() + 2 ); - - String d = getBuild().getTestOutputDirectory(); - if ( d != null ) - { - list.add( d ); - } - - d = getBuild().getOutputDirectory(); - if ( d != null ) - { - list.add( d ); - } - - for ( Artifact a : getArtifacts() ) - { - if ( a.getArtifactHandler().isAddedToClasspath() ) - { - addArtifactPath( a, list ); - } - } - - return list; + return Stream.concat( Stream.concat( + ofNullable( getBuild().getTestOutputDirectory() ), + ofNullable( getBuild().getOutputDirectory() ) ), + getClasspathElements( a -> true ) + ).collect( Collectors.toList() ); } public List getRuntimeClasspathElements() throws DependencyResolutionRequiredException { - List list = new ArrayList<>( getArtifacts().size() + 1 ); - - String d = getBuild().getOutputDirectory(); - if ( d != null ) - { - list.add( d ); - } + return Stream.concat( + ofNullable( getBuild().getOutputDirectory() ), + getClasspathElements( MavenProject::runtimeScope ) + ).collect( Collectors.toList() ); + } - for ( Artifact a : getArtifacts() ) - { - if ( a.getArtifactHandler().isAddedToClasspath() - // TODO let the scope handler deal with this - && ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) ) ) - { - addArtifactPath( a, list ); - } - } - return list; + private Stream getClasspathElements( Predicate scopes ) + { + return getArtifacts().stream() + .filter( MavenProject::isAddedToClasspath ) + .filter( scopes ) + .filter( a -> a.getFile() != null ) + .map( a -> a.getFile().getPath() ); } // ---------------------------------------------------------------------- @@ -1583,227 +1543,81 @@ public List getScriptSourceRoots() @Deprecated public List getCompileArtifacts() { - List list = new ArrayList<>( getArtifacts().size() ); - - for ( Artifact a : getArtifacts() ) - { - // TODO classpath check doesn't belong here - that's the other method - if ( a.getArtifactHandler().isAddedToClasspath() ) - { - // TODO let the scope handler deal with this - if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) - || Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) - { - list.add( a ); - } - } - } - return list; + return getArtifacts().stream() + .filter( MavenProject::isAddedToClasspath ) // TODO classpath check doesn't belong here - that's the other method + .filter( MavenProject::compileScope ) // TODO let the scope handler deal with this + .collect( Collectors.toList() ); } @Deprecated public List getCompileDependencies() { - Set artifacts = getArtifacts(); - - if ( ( artifacts == null ) || artifacts.isEmpty() ) - { - return Collections.emptyList(); - } - - List list = new ArrayList<>( artifacts.size() ); - - for ( Artifact a : getArtifacts() ) - { - // TODO let the scope handler deal with this - if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) - || Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) - { - Dependency dependency = new Dependency(); - - dependency.setArtifactId( a.getArtifactId() ); - dependency.setGroupId( a.getGroupId() ); - dependency.setVersion( a.getVersion() ); - dependency.setScope( a.getScope() ); - dependency.setType( a.getType() ); - dependency.setClassifier( a.getClassifier() ); - - list.add( dependency ); - } - } - return Collections.unmodifiableList( list ); + return Collections.unmodifiableList( getArtifacts().stream() + .filter( MavenProject::compileScope ) // TODO let the scope handler deal with this + .map( MavenProject::toDependency ) + .collect( Collectors.toList() ) ); } @Deprecated public List getTestArtifacts() { - List list = new ArrayList<>( getArtifacts().size() ); - - for ( Artifact a : getArtifacts() ) - { - // TODO classpath check doesn't belong here - that's the other method - if ( a.getArtifactHandler().isAddedToClasspath() ) - { - list.add( a ); - } - } - return list; + return Collections.unmodifiableList( getArtifacts().stream() + .filter( MavenProject::isAddedToClasspath ) // TODO classpath check doesn't belong here - that's the other method + .collect( Collectors.toList() ) ); } @Deprecated public List getTestDependencies() { - Set artifacts = getArtifacts(); - - if ( ( artifacts == null ) || artifacts.isEmpty() ) - { - return Collections.emptyList(); - } - - List list = new ArrayList<>( artifacts.size() ); - - for ( Artifact a : getArtifacts() ) - { - Dependency dependency = new Dependency(); - - dependency.setArtifactId( a.getArtifactId() ); - dependency.setGroupId( a.getGroupId() ); - dependency.setVersion( a.getVersion() ); - dependency.setScope( a.getScope() ); - dependency.setType( a.getType() ); - dependency.setClassifier( a.getClassifier() ); - - list.add( dependency ); - } - return Collections.unmodifiableList( list ); + return Collections.unmodifiableList( getArtifacts().stream() + .map( MavenProject::toDependency ) + .collect( Collectors.toList() ) ); } @Deprecated // used by the Maven ITs public List getRuntimeDependencies() { - Set artifacts = getArtifacts(); - - if ( ( artifacts == null ) || artifacts.isEmpty() ) - { - return Collections.emptyList(); - } - - List list = new ArrayList<>( artifacts.size() ); - - for ( Artifact a : getArtifacts() ) - { - // TODO let the scope handler deal with this - if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) ) - { - Dependency dependency = new Dependency(); - - dependency.setArtifactId( a.getArtifactId() ); - dependency.setGroupId( a.getGroupId() ); - dependency.setVersion( a.getVersion() ); - dependency.setScope( a.getScope() ); - dependency.setType( a.getType() ); - dependency.setClassifier( a.getClassifier() ); - - list.add( dependency ); - } - } - return Collections.unmodifiableList( list ); + return Collections.unmodifiableList( getArtifacts().stream() + .filter( MavenProject::runtimeScope ) // TODO let the scope handler deal with this + .map( MavenProject::toDependency ) + .collect( Collectors.toList() ) ); } @Deprecated public List getRuntimeArtifacts() { - List list = new ArrayList<>( getArtifacts().size() ); - - for ( Artifact a : getArtifacts() ) - { - // TODO classpath check doesn't belong here - that's the other method - if ( a.getArtifactHandler().isAddedToClasspath() - // TODO let the scope handler deal with this - && ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) ) ) - { - list.add( a ); - } - } - return list; + return getArtifacts().stream() + .filter( MavenProject::isAddedToClasspath ) // TODO classpath check doesn't belong here - that's the other method + .filter( MavenProject::runtimeScope ) // TODO let the scope handler deal with this + .collect( Collectors.toList() ); } @Deprecated public List getSystemClasspathElements() throws DependencyResolutionRequiredException { - List list = new ArrayList<>( getArtifacts().size() ); - - String d = getBuild().getOutputDirectory(); - if ( d != null ) - { - list.add( d ); - } - - for ( Artifact a : getArtifacts() ) - { - if ( a.getArtifactHandler().isAddedToClasspath() ) - { - // TODO let the scope handler deal with this - if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) - { - addArtifactPath( a, list ); - } - } - } - return list; + return Stream.concat( + ofNullable( getBuild().getOutputDirectory() ), + getClasspathElements( MavenProject::systemScope ) + ).collect( Collectors.toList() ); } @Deprecated public List getSystemArtifacts() { - List list = new ArrayList<>( getArtifacts().size() ); - - for ( Artifact a : getArtifacts() ) - { - // TODO classpath check doesn't belong here - that's the other method - if ( a.getArtifactHandler().isAddedToClasspath() ) - { - // TODO let the scope handler deal with this - if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) - { - list.add( a ); - } - } - } - return list; + return getArtifacts().stream() + .filter( MavenProject::isAddedToClasspath ) // TODO classpath check doesn't belong here - that's the other method + .filter( MavenProject::systemScope ) // TODO let the scope handler deal with this + .collect( Collectors.toList() ); } @Deprecated public List getSystemDependencies() { - Set artifacts = getArtifacts(); - - if ( ( artifacts == null ) || artifacts.isEmpty() ) - { - return Collections.emptyList(); - } - - List list = new ArrayList<>( artifacts.size() ); - - for ( Artifact a : getArtifacts() ) - { - // TODO let the scope handler deal with this - if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) - { - Dependency dependency = new Dependency(); - - dependency.setArtifactId( a.getArtifactId() ); - dependency.setGroupId( a.getGroupId() ); - dependency.setVersion( a.getVersion() ); - dependency.setScope( a.getScope() ); - dependency.setType( a.getType() ); - dependency.setClassifier( a.getClassifier() ); - - list.add( dependency ); - } - } - return Collections.unmodifiableList( list ); + return Collections.unmodifiableList( getArtifacts().stream() + .filter( MavenProject::systemScope ) // TODO let the scope handler deal with this + .map( MavenProject::toDependency ) + .collect( Collectors.toList() ) ); } @Deprecated @@ -1986,4 +1800,47 @@ public void setProjectBuildingRequest( ProjectBuildingRequest projectBuildingReq { this.projectBuilderConfiguration = projectBuildingRequest; } + + private static Dependency toDependency( Artifact a ) + { + Dependency dependency = new Dependency(); + + dependency.setArtifactId( a.getArtifactId() ); + dependency.setGroupId( a.getGroupId() ); + dependency.setVersion( a.getVersion() ); + dependency.setScope( a.getScope() ); + dependency.setType( a.getType() ); + dependency.setClassifier( a.getClassifier() ); + + return dependency; + } + + private static Stream ofNullable( T t ) + { + return t != null ? Stream.of( t ) : Stream.empty(); + } + + private static boolean compileScope( Artifact a ) + { + return Artifact.SCOPE_COMPILE.equals( a.getScope() ) + || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) + || Artifact.SCOPE_SYSTEM.equals( a.getScope() ); + } + + private static boolean runtimeScope( Artifact a ) + { + return Artifact.SCOPE_COMPILE.equals( a.getScope() ) + || Artifact.SCOPE_RUNTIME.equals( a.getScope() ); + } + + private static boolean systemScope( Artifact a ) + { + return Artifact.SCOPE_SYSTEM.equals( a.getScope() ); + } + + private static boolean isAddedToClasspath( Artifact a ) + { + return a.getArtifactHandler().isAddedToClasspath(); + } + } From c46eaefbaae0b17c43f9bdaefa9ed1b7a9808d28 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 2 Jun 2021 09:30:14 +0200 Subject: [PATCH 2/3] [MNG-6843] Parallel build fails due to missing JAR artifacts in compilePath --- .../maven/project/DefaultProjectBuilder.java | 1 - .../apache/maven/project/MavenProject.java | 64 +++++++------------ .../ProjectDependenciesResolverTest.java | 2 +- 3 files changed, 25 insertions(+), 42 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 12e6e2383ccd..060644bb1a40 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -248,7 +248,6 @@ private DependencyResolutionResult resolveDependencies( MavenProject project, Re } } project.setResolvedArtifacts( artifacts ); - project.setArtifacts( artifacts ); return resolutionResult; } diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 95f8f5a3b268..f58543eb0cbd 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -32,6 +32,7 @@ import java.util.Properties; import java.util.Set; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -114,9 +115,10 @@ public class MavenProject private Set resolvedArtifacts; - private ArtifactFilter artifactFilter; + private final ThreadLocal artifactFilter + = ThreadLocal.withInitial( () -> a -> true ); - private Set artifacts; + private final Map> artifacts = new ConcurrentHashMap<>(); private Artifact parentArtifact; @@ -154,8 +156,7 @@ public class MavenProject private Artifact artifact; - // calculated. - private Map artifactMap; + private final Map> artifactMap = new ConcurrentHashMap<>(); private Model originalModel; @@ -653,14 +654,6 @@ public void addLicense( License license ) getModel().addLicense( license ); } - public void setArtifacts( Set artifacts ) - { - this.artifacts = artifacts; - - // flush the calculated artifactMap - artifactMap = null; - } - /** * All dependencies that this project has, including transitive ones. Contents are lazily populated, so depending on * what phases have run dependencies in some scopes won't be included. eg. if only compile phase has run, @@ -671,34 +664,32 @@ public void setArtifacts( Set artifacts ) */ public Set getArtifacts() { - if ( artifacts == null ) + if ( resolvedArtifacts == null ) { - if ( artifactFilter == null || resolvedArtifacts == null ) - { - artifacts = new LinkedHashSet<>(); - } - else + return Collections.emptySet(); + } + return artifacts.computeIfAbsent( Objects.requireNonNull( artifactFilter.get() ), f -> + { + Set artifacts = new LinkedHashSet<>(); + for ( Artifact a : resolvedArtifacts ) { - artifacts = new LinkedHashSet<>( resolvedArtifacts.size() * 2 ); - for ( Artifact artifact : resolvedArtifacts ) + if ( f.include( a ) ) { - if ( artifactFilter.include( artifact ) ) - { - artifacts.add( artifact ); - } + artifacts.add( a ); } } - } - return artifacts; + return artifacts; + } ); } public Map getArtifactMap() { - if ( artifactMap == null ) + if ( resolvedArtifacts == null ) { - artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() ); + return Collections.emptyMap(); } - return artifactMap; + return artifactMap.computeIfAbsent( Objects.requireNonNull( artifactFilter.get() ), + f -> ArtifactUtils.artifactMapByVersionlessId( getArtifacts() ) ); } public void setPluginArtifacts( Set pluginArtifacts ) @@ -1189,11 +1180,6 @@ private void deepCopy( MavenProject project ) setDependencyArtifacts( Collections.unmodifiableSet( project.getDependencyArtifacts() ) ); } - if ( project.getArtifacts() != null ) - { - setArtifacts( Collections.unmodifiableSet( project.getArtifacts() ) ); - } - if ( project.getParentFile() != null ) { parentFile = new File( project.getParentFile().getAbsolutePath() ); @@ -1387,9 +1373,9 @@ public DependencyFilter getExtensionDependencyFilter() */ public void setResolvedArtifacts( Set artifacts ) { - this.resolvedArtifacts = ( artifacts != null ) ? artifacts : Collections.emptySet(); - this.artifacts = null; - this.artifactMap = null; + this.resolvedArtifacts = ( artifacts != null ) ? artifacts : Collections.emptySet(); + this.artifacts.clear(); + this.artifactMap.clear(); } /** @@ -1402,9 +1388,7 @@ public void setResolvedArtifacts( Set artifacts ) */ public void setArtifactFilter( ArtifactFilter artifactFilter ) { - this.artifactFilter = artifactFilter; - this.artifacts = null; - this.artifactMap = null; + this.artifactFilter.set( artifactFilter ); } /** diff --git a/maven-core/src/test/java/org/apache/maven/ProjectDependenciesResolverTest.java b/maven-core/src/test/java/org/apache/maven/ProjectDependenciesResolverTest.java index b648f2aec4e4..da33b32d0347 100644 --- a/maven-core/src/test/java/org/apache/maven/ProjectDependenciesResolverTest.java +++ b/maven-core/src/test/java/org/apache/maven/ProjectDependenciesResolverTest.java @@ -96,7 +96,7 @@ public void testSystemScopeDependencyIsPresentInTheCompileClasspathElements() MavenSession session = createMavenSession( pom, eps ); MavenProject project = session.getCurrentProject(); - project.setArtifacts( resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), session ) ); + project.setResolvedArtifacts( resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), session ) ); List elements = project.getCompileClasspathElements(); assertEquals( 2, elements.size() ); From 6868a439fa24c01e5f26811404f0125bddd19c03 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 2 Jun 2021 09:46:11 +0200 Subject: [PATCH 3/3] [MNG-7157] Provide a better design for obtaining artifacts from a MavenProject inside a mojo # Conflicts: # maven-core/src/main/java/org/apache/maven/project/MavenProject.java --- .../lifecycle/internal/MojoExecutor.java | 2 +- .../PluginParameterExpressionEvaluator.java | 31 ++++++++++ .../apache/maven/project/MavenProject.java | 56 +++++++++++++++---- ...luginParameterExpressionEvaluatorTest.java | 42 ++++++++++++++ 4 files changed, 119 insertions(+), 12 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java index 89fadfdb32ff..f6c7153cfee3 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java @@ -283,7 +283,7 @@ public void ensureDependenciesAreResolved( MojoDescriptor mojoDescriptor, MavenS } } - private ArtifactFilter getArtifactFilter( MojoDescriptor mojoDescriptor ) + public static ArtifactFilter getArtifactFilter( MojoDescriptor mojoDescriptor ) { String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired(); String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired(); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java index 4f5503382313..084b78512fa0 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java @@ -22,7 +22,9 @@ import java.io.File; import java.util.Properties; +import org.apache.maven.artifact.resolver.filter.ArtifactFilter; import org.apache.maven.execution.MavenSession; +import org.apache.maven.lifecycle.internal.MojoExecutor; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.project.MavenProject; @@ -244,6 +246,10 @@ else if ( "mojoExecution".equals( expression ) ) { value = mojoExecution; } + else if ( "artifactFilter".equals( expression ) ) + { + value = MojoExecutor.getArtifactFilter( mojoDescriptor ); + } else if ( "project".equals( expression ) ) { value = project; @@ -264,6 +270,31 @@ else if ( expression.startsWith( "project" ) || expression.startsWith( "pom" ) ) value = ReflectionValueExtractor.evaluate( pathExpression, project ); value = value + expression.substring( pathSeparator ); } + else if ( "project.artifacts".equals( expression ) ) + { + ArtifactFilter filter = MojoExecutor.getArtifactFilter( mojoDescriptor ); + value = project.getArtifacts( filter ); + } + else if ( "project.artifactMap".equals( expression ) ) + { + ArtifactFilter filter = MojoExecutor.getArtifactFilter( mojoDescriptor ); + value = project.getArtifactMap( filter ); + } + else if ( "project.compileClasspathElements".equals( expression ) ) + { + ArtifactFilter filter = MojoExecutor.getArtifactFilter( mojoDescriptor ); + value = project.getCompileClasspathElements( filter ); + } + else if ( "project.runtimeClasspathElements".equals( expression ) ) + { + ArtifactFilter filter = MojoExecutor.getArtifactFilter( mojoDescriptor ); + value = project.getRuntimeClasspathElements( filter ); + } + else if ( "project.testClasspathElements".equals( expression ) ) + { + ArtifactFilter filter = MojoExecutor.getArtifactFilter( mojoDescriptor ); + value = project.getTestClasspathElements( filter ); + } else { value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), project ); diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index f58543eb0cbd..bc3256c95303 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -351,37 +351,58 @@ public List getTestCompileSourceRoots() return testCompileSourceRoots; } + @Deprecated public List getCompileClasspathElements() throws DependencyResolutionRequiredException + { + return getCompileClasspathElements( artifactFilter.get() ); + } + + public List getCompileClasspathElements( ArtifactFilter artifactFilter ) + throws DependencyResolutionRequiredException { return Stream.concat( ofNullable( getBuild().getOutputDirectory() ), - getClasspathElements( MavenProject::compileScope ) + getClasspathElements( artifactFilter, MavenProject::compileScope ) ).collect( Collectors.toList() ); } + @Deprecated public List getTestClasspathElements() throws DependencyResolutionRequiredException + { + return getTestClasspathElements( artifactFilter.get() ); + } + + public List getTestClasspathElements( ArtifactFilter artifactFilter ) + throws DependencyResolutionRequiredException { return Stream.concat( Stream.concat( ofNullable( getBuild().getTestOutputDirectory() ), ofNullable( getBuild().getOutputDirectory() ) ), - getClasspathElements( a -> true ) + getClasspathElements( artifactFilter, a -> true ) ).collect( Collectors.toList() ); } + @Deprecated public List getRuntimeClasspathElements() throws DependencyResolutionRequiredException + { + return getRuntimeClasspathElements( artifactFilter.get() ); + } + + public List getRuntimeClasspathElements( ArtifactFilter artifactFilter ) + throws DependencyResolutionRequiredException { return Stream.concat( ofNullable( getBuild().getOutputDirectory() ), - getClasspathElements( MavenProject::runtimeScope ) + getClasspathElements( artifactFilter, MavenProject::runtimeScope ) ).collect( Collectors.toList() ); } - private Stream getClasspathElements( Predicate scopes ) + private Stream getClasspathElements( ArtifactFilter artifactFilter, Predicate scopes ) { - return getArtifacts().stream() + return getArtifacts( artifactFilter ).stream() .filter( MavenProject::isAddedToClasspath ) .filter( scopes ) .filter( a -> a.getFile() != null ) @@ -662,13 +683,19 @@ public void addLicense( License license ) * @return {@link Set} < {@link Artifact} > * @see #getDependencyArtifacts() to get only direct dependencies */ + @Deprecated public Set getArtifacts() { - if ( resolvedArtifacts == null ) + return getArtifacts( artifactFilter.get() ); + } + + public Set getArtifacts( ArtifactFilter filter ) + { + if ( filter == null || resolvedArtifacts == null ) { return Collections.emptySet(); } - return artifacts.computeIfAbsent( Objects.requireNonNull( artifactFilter.get() ), f -> + return artifacts.computeIfAbsent( filter, f -> { Set artifacts = new LinkedHashSet<>(); for ( Artifact a : resolvedArtifacts ) @@ -682,14 +709,20 @@ public Set getArtifacts() } ); } + @Deprecated public Map getArtifactMap() { - if ( resolvedArtifacts == null ) + return getArtifactMap( artifactFilter.get() ); + } + + public Map getArtifactMap( ArtifactFilter filter ) + { + if ( filter == null || resolvedArtifacts == null ) { return Collections.emptyMap(); } - return artifactMap.computeIfAbsent( Objects.requireNonNull( artifactFilter.get() ), - f -> ArtifactUtils.artifactMapByVersionlessId( getArtifacts() ) ); + return artifactMap.computeIfAbsent( filter, + f -> ArtifactUtils.artifactMapByVersionlessId( getArtifacts( f ) ) ); } public void setPluginArtifacts( Set pluginArtifacts ) @@ -1386,6 +1419,7 @@ public void setResolvedArtifacts( Set artifacts ) * * @param artifactFilter The artifact filter, may be {@code null} to exclude all artifacts. */ + @Deprecated public void setArtifactFilter( ArtifactFilter artifactFilter ) { this.artifactFilter.set( artifactFilter ); @@ -1582,7 +1616,7 @@ public List getSystemClasspathElements() { return Stream.concat( ofNullable( getBuild().getOutputDirectory() ), - getClasspathElements( MavenProject::systemScope ) + getClasspathElements( artifactFilter.get(), MavenProject::systemScope ) ).collect( Collectors.toList() ); } diff --git a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorTest.java b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorTest.java index 16deedd33794..a75382b419d1 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorTest.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorTest.java @@ -21,10 +21,13 @@ import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import org.apache.maven.AbstractCoreMavenComponentTestCase; import org.apache.maven.artifact.Artifact; @@ -391,6 +394,45 @@ public void testShouldExtractPluginArtifacts() assertEquals( "testGroup", result.getGroupId() ); } + @Test + public void testProjectArtifacts() + throws Exception + { + MavenProject project = createDefaultProject(); + + Artifact artifact1 = createArtifact( "testGroup", "testArtifact1", "1.0" ); + Artifact artifact2 = createArtifact( "testGroup", "testArtifact2", "1.0" ); + artifact2.setScope( "test" ); + project.setResolvedArtifacts( new HashSet<>(Arrays.asList( artifact1, artifact2 ) ) ); + + ArtifactRepository repo = factory.createDefaultLocalRepository(); + + MutablePlexusContainer container = (MutablePlexusContainer) getContainer(); + MavenSession session = createSession( container, repo, new Properties() ); + session.setCurrentProject( project ); + + MojoDescriptor mojo = new MojoDescriptor(); + PluginDescriptor pd = new PluginDescriptor(); + mojo.setPluginDescriptor( pd ); + mojo.setGoal( "goal" ); + mojo.setDependencyResolutionRequired( "compile" ); + MojoExecution mojoExecution = new MojoExecution( mojo ); + ExpressionEvaluator ee = new PluginParameterExpressionEvaluator( session, mojoExecution ); + + Object value = ee.evaluate( "${project.artifacts}" ); + + assertTrue( value instanceof Set ); + + @SuppressWarnings( "unchecked" ) + Set artifacts = (Set) value; + + assertEquals( 1, artifacts.size() ); + + Artifact result = artifacts.iterator().next(); + + assertEquals( "testArtifact1", result.getArtifactId() ); + } + private MavenProject createDefaultProject() { return new MavenProject( new Model() );