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 935522 2015-01-08 20:15:45Z khmarbaise $
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 ( Object o : project.getArtifacts() )
42          {
43              Artifact artifact = (Artifact) o;
44              if ( artifact.getGroupId().equals( dependency.getGroupId() )
45                  && artifact.getArtifactId().equals( dependency.getArtifactId() )
46                  && artifact.getType().equals( dependency.getType() ) )
47              {
48                  if ( artifact.getClassifier() == null && dependency.getClassifier() == null )
49                  {
50                      return artifact;
51                  }
52                  else if ( dependency.getClassifier() != null
53                      && dependency.getClassifier().equals( artifact.getClassifier() ) )
54                  {
55                      return artifact;
56                  }
57              }
58          }
59          return null;
60      }
61  
62      /**
63       * @param artifact {@link Artifact}
64       * @param dependency {@link Dependency}
65       * @return is related or not.
66       */
67      public static boolean isRelated( Artifact artifact, Dependency dependency )
68      {
69          if ( artifact == null || dependency == null )
70          {
71              return false;
72          }
73  
74          if ( !StringUtils.equals( artifact.getGroupId(), dependency.getGroupId() ) )
75          {
76              return false;
77          }
78          if ( !StringUtils.equals( artifact.getArtifactId(), dependency.getArtifactId() ) )
79          {
80              return false;
81          }
82          if ( artifact.getVersion() != null ? !artifact.getVersion().equals( dependency.getVersion() )
83                          : dependency.getVersion() != null )
84          {
85              return false;
86          }
87          if ( artifact.getType() != null ? !artifact.getType().equals( dependency.getType() )
88                          : dependency.getType() != null )
89          {
90              return false;
91          }
92          if ( artifact.getClassifier() != null ? !artifact.getClassifier().equals( dependency.getClassifier() )
93                          : dependency.getClassifier() != null )
94          {
95              return false;
96          }
97          if ( artifact.getScope() != null ? !artifact.getScope().equals( dependency.getScope() )
98                          : dependency.getScope() != null )
99          {
100             return false;
101         }
102         if ( artifact.isOptional() != dependency.isOptional() )
103         {
104             return false;
105         }
106 
107         return true;
108     }
109 
110     /**
111      * @param first {@link Dependency}
112      * @param second {@link Dependency}
113      * @return are the dependencies equal.
114      */
115     public static boolean dependencyEquals( Dependency first, Dependency second )
116     {
117         if ( first == second )
118         {
119             return true;
120         }
121 
122         if ( first.isOptional() != second.isOptional() )
123         {
124             return false;
125         }
126         if ( !StringUtils.equals( first.getArtifactId(), second.getArtifactId() ) )
127         {
128             return false;
129         }
130         if ( first.getClassifier() != null ? !first.getClassifier().equals( second.getClassifier() )
131                         : second.getClassifier() != null )
132         {
133             return false;
134         }
135         if ( first.getExclusions() != null ? !first.getExclusions().equals( second.getExclusions() )
136                         : second.getExclusions() != null )
137         {
138             return false;
139         }
140         if ( !StringUtils.equals( first.getGroupId(), second.getGroupId() ) )
141         {
142             return false;
143         }
144         if ( first.getScope() != null ? !first.getScope().equals( second.getScope() ) : second.getScope() != null )
145         {
146             return false;
147         }
148         if ( first.getSystemPath() != null ? !first.getSystemPath().equals( second.getSystemPath() )
149                         : second.getSystemPath() != null )
150         {
151             return false;
152         }
153         if ( first.getType() != null ? !first.getType().equals( second.getType() ) : second.getType() != null )
154         {
155             return false;
156         }
157         if ( first.getVersion() != null ? !first.getVersion().equals( second.getVersion() )
158                         : second.getVersion() != null )
159         {
160             return false;
161         }
162         return true;
163     }
164 
165 }