View Javadoc
1   package org.apache.maven.plugins.assembly.functions;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          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              @Override
97              public void accept( MavenProject project )
98              {
99                  final String projectId = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
100 
101                 logger.debug( "Excluding POM-packaging module: " + projectId );
102             }
103         };
104     }
105 
106     public static MavenProjectConsumer addTo( final Set<MavenProject> set )
107     {
108         return new MavenProjectConsumer()
109         {
110             @Override
111             public void accept( MavenProject project )
112             {
113                 set.add( project );
114             }
115         };
116     }
117 
118 }