1   package org.apache.maven.plugin.assembly.functions;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.ArtifactUtils;
24  import org.apache.maven.project.MavenProject;
25  import org.codehaus.plexus.logging.Logger;
26  
27  import javax.annotation.Nullable;
28  import java.util.List;
29  import java.util.Set;
30  
31  
32  
33  
34  public class MavenProjects
35  {
36      public static void without( Iterable<MavenProject> source, String packagingType, MavenProjectConsumer consumer )
37      {
38          for ( MavenProject project : source )
39          {
40              if ( !packagingType.equals( project.getPackaging() ) )
41              {
42                  consumer.accept( project );
43              }
44          }
45      }
46  
47      public static void select( Iterable<MavenProject> source, String packagingType, MavenProjectConsumer consumer )
48      {
49          for ( MavenProject project : source )
50          {
51              if ( packagingType.equals( project.getPackaging() ) )
52              {
53                  consumer.accept( project );
54              }
55          }
56      }
57  
58      public static void select( Iterable<MavenProject> source, String packagingType, MavenProjectConsumer include,
59                                 MavenProjectConsumer excluded )
60      {
61          for ( MavenProject project : source )
62          {
63              if ( packagingType.equals( project.getPackaging() ) )
64              {
65                  include.accept( project );
66              }
67              else
68              {
69                  excluded.accept( project );
70              }
71          }
72      }
73  
74      @Nullable
75      public static Artifact findArtifactByClassifier( MavenProject mavenProject, String classifier )
76      {
77          @SuppressWarnings( "unchecked" ) final List<Artifact> attachments = mavenProject.getAttachedArtifacts();
78          if ( ( attachments != null ) && !attachments.isEmpty() )
79          {
80              for ( final Artifact attachment : attachments )
81              {
82                  if ( classifier.equals( attachment.getClassifier() ) )
83                  {
84                      return attachment;
85                  }
86              }
87          }
88          return null;
89      }
90  
91  
92      public static MavenProjectConsumer log( final Logger logger )
93      {
94          return new MavenProjectConsumer()
95          {
96              public void accept( MavenProject project )
97              {
98                  final String projectId = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
99  
100                 logger.debug( "Excluding POM-packaging module: " + projectId );
101             }
102         };
103     }
104 
105     public static MavenProjectConsumer addTo( final Set<MavenProject> set )
106     {
107         return new MavenProjectConsumer()
108         {
109             public void accept( MavenProject project )
110             {
111                 set.add( project );
112             }
113         };
114     }
115 
116 }