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.war.util;
20  
21  import java.util.Objects;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.model.Dependency;
25  import org.apache.maven.project.MavenProject;
26  
27  /**
28   * @author Stephane Nicoll
29   */
30  public class WarUtils {
31  
32      /**
33       * @param project {@link MavenProject}
34       * @param dependency {@link Dependency}
35       * @return {@link Artifact}
36       */
37      public static Artifact getArtifact(MavenProject project, Dependency dependency) {
38          for (Artifact artifact : project.getArtifacts()) {
39              if (artifact.getGroupId().equals(dependency.getGroupId())
40                      && artifact.getArtifactId().equals(dependency.getArtifactId())
41                      && artifact.getType().equals(dependency.getType())) {
42                  if (artifact.getClassifier() == null && dependency.getClassifier() == null) {
43                      return artifact;
44                  } else if (dependency.getClassifier() != null
45                          && dependency.getClassifier().equals(artifact.getClassifier())) {
46                      return artifact;
47                  }
48              }
49          }
50          return null;
51      }
52  
53      /**
54       * @param artifact {@link Artifact}
55       * @param dependency {@link Dependency}
56       * @return is related or not.
57       */
58      public static boolean isRelated(Artifact artifact, Dependency dependency) {
59          if (artifact == null || dependency == null) {
60              return false;
61          }
62  
63          if (!Objects.equals(artifact.getGroupId(), dependency.getGroupId())) {
64              return false;
65          }
66          if (!Objects.equals(artifact.getArtifactId(), dependency.getArtifactId())) {
67              return false;
68          }
69          if (Objects.equals(artifact.getVersion(), dependency.getVersion())) {
70              return false;
71          }
72          if (Objects.equals(artifact.getType(), dependency.getType())) {
73              return false;
74          }
75          if (Objects.equals(artifact.getClassifier(), dependency.getClassifier())) {
76              return false;
77          }
78          if (Objects.equals(artifact.getScope(), dependency.getScope())) {
79              return false;
80          }
81          if (artifact.isOptional() != dependency.isOptional()) {
82              return false;
83          }
84  
85          return true;
86      }
87  }