View Javadoc
1   package org.apache.maven.plugin.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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.model.Dependency;
24  import org.apache.maven.project.MavenProject;
25  import org.codehaus.plexus.util.StringUtils;
26  
27  /**
28   * @author Stephane Nicoll
29   * @version $Id: WarUtils.html 925069 2014-10-08 17:03:57Z khmarbaise $
30   */
31  public class WarUtils
32  {
33  
34      public static Artifact getArtifact( MavenProject project, Dependency dependency )
35      {
36          for ( Object o : project.getArtifacts() )
37          {
38              Artifact artifact = (Artifact) o;
39              if ( artifact.getGroupId().equals( dependency.getGroupId() )
40                  && artifact.getArtifactId().equals( dependency.getArtifactId() )
41                  && artifact.getType().equals( dependency.getType() ) )
42              {
43                  if ( artifact.getClassifier() == null && dependency.getClassifier() == null )
44                  {
45                      return artifact;
46                  }
47                  else if ( dependency.getClassifier() != null
48                      && dependency.getClassifier().equals( artifact.getClassifier() ) )
49                  {
50                      return artifact;
51                  }
52              }
53          }
54          return null;
55      }
56  
57      public static boolean isRelated( Artifact artifact, Dependency dependency )
58      {
59          if ( artifact == null || dependency == null )
60          {
61              return false;
62          }
63  
64          if ( !StringUtils.equals( artifact.getGroupId(), dependency.getGroupId() ) )
65          {
66              return false;
67          }
68          if ( !StringUtils.equals( artifact.getArtifactId(), dependency.getArtifactId() ) )
69          {
70              return false;
71          }
72          if ( artifact.getVersion() != null ? !artifact.getVersion().equals( dependency.getVersion() )
73                          : dependency.getVersion() != null )
74          {
75              return false;
76          }
77          if ( artifact.getType() != null ? !artifact.getType().equals( dependency.getType() )
78                          : dependency.getType() != null )
79          {
80              return false;
81          }
82          if ( artifact.getClassifier() != null ? !artifact.getClassifier().equals( dependency.getClassifier() )
83                          : dependency.getClassifier() != null )
84          {
85              return false;
86          }
87          if ( artifact.getScope() != null ? !artifact.getScope().equals( dependency.getScope() )
88                          : dependency.getScope() != null )
89          {
90              return false;
91          }
92          if ( artifact.isOptional() != dependency.isOptional() )
93          {
94              return false;
95          }
96  
97          return true;
98      }
99  
100     public static boolean dependencyEquals( Dependency first, Dependency second )
101     {
102         if ( first == second )
103         {
104             return true;
105         }
106 
107         if ( first.isOptional() != second.isOptional() )
108         {
109             return false;
110         }
111         if ( !StringUtils.equals( first.getArtifactId(), second.getArtifactId() ) )
112         {
113             return false;
114         }
115         if ( first.getClassifier() != null ? !first.getClassifier().equals( second.getClassifier() )
116                         : second.getClassifier() != null )
117         {
118             return false;
119         }
120         if ( first.getExclusions() != null ? !first.getExclusions().equals( second.getExclusions() )
121                         : second.getExclusions() != null )
122         {
123             return false;
124         }
125         if ( !StringUtils.equals( first.getGroupId(), second.getGroupId() ) )
126         {
127             return false;
128         }
129         if ( first.getScope() != null ? !first.getScope().equals( second.getScope() ) : second.getScope() != null )
130         {
131             return false;
132         }
133         if ( first.getSystemPath() != null ? !first.getSystemPath().equals( second.getSystemPath() )
134                         : second.getSystemPath() != null )
135         {
136             return false;
137         }
138         if ( first.getType() != null ? !first.getType().equals( second.getType() ) : second.getType() != null )
139         {
140             return false;
141         }
142         if ( first.getVersion() != null ? !first.getVersion().equals( second.getVersion() )
143                         : second.getVersion() != null )
144         {
145             return false;
146         }
147         return true;
148     }
149 
150 }