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