View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.functions;
20  
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.ArtifactUtils;
26  import org.apache.maven.project.MavenProject;
27  import org.slf4j.Logger;
28  
29  /**
30   *
31   */
32  public class MavenProjects {
33      public static void without(Iterable<MavenProject> source, String packagingType, MavenProjectConsumer consumer) {
34          for (MavenProject project : source) {
35              if (!packagingType.equals(project.getPackaging())) {
36                  consumer.accept(project);
37              }
38          }
39      }
40  
41      public static void select(Iterable<MavenProject> source, String packagingType, MavenProjectConsumer consumer) {
42          for (MavenProject project : source) {
43              if (packagingType.equals(project.getPackaging())) {
44                  consumer.accept(project);
45              }
46          }
47      }
48  
49      public static void select(
50              Iterable<MavenProject> source,
51              String packagingType,
52              MavenProjectConsumer include,
53              MavenProjectConsumer excluded) {
54          for (MavenProject project : source) {
55              if (packagingType.equals(project.getPackaging())) {
56                  include.accept(project);
57              } else {
58                  excluded.accept(project);
59              }
60          }
61      }
62  
63      public static Artifact findArtifactByClassifier(MavenProject mavenProject, String classifier) {
64          final List<Artifact> attachments = mavenProject.getAttachedArtifacts();
65          if ((attachments != null) && !attachments.isEmpty()) {
66              for (final Artifact attachment : attachments) {
67                  if (classifier.equals(attachment.getClassifier())) {
68                      return attachment;
69                  }
70              }
71          }
72          return null;
73      }
74  
75      public static MavenProjectConsumer log(final Logger logger) {
76          return new MavenProjectConsumer() {
77              @Override
78              public void accept(MavenProject project) {
79                  final String projectId = ArtifactUtils.versionlessKey(project.getGroupId(), project.getArtifactId());
80  
81                  logger.debug("Excluding POM-packaging module: " + projectId);
82              }
83          };
84      }
85  
86      public static MavenProjectConsumer addTo(final Set<MavenProject> set) {
87          return new MavenProjectConsumer() {
88              @Override
89              public void accept(MavenProject project) {
90                  set.add(project);
91              }
92          };
93      }
94  }