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  import java.util.Iterator;
28  
29  /**
30   *
31   * @author Stephane Nicoll
32   * @version $Id: WarUtils.java 743348 2009-02-11 14:40:19Z dennisl $
33   */
34  public class WarUtils
35  {
36  
37  
38      public static Artifact getArtifact( MavenProject project, Dependency dependency )
39      {
40          final Iterator it = project.getArtifacts().iterator();
41          while ( it.hasNext() )
42          {
43              Artifact artifact = (Artifact) it.next();
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      public static boolean isRelated( Artifact artifact, Dependency dependency )
63      {
64          if ( artifact == null || dependency == null )
65          {
66              return false;
67          }
68  
69          if ( !StringUtils.equals( artifact.getGroupId(), dependency.getGroupId() ) )
70          {
71              return false;
72          }
73          if ( !StringUtils.equals( artifact.getArtifactId(), dependency.getArtifactId() ) )
74          {
75              return false;
76          }
77          if ( artifact.getVersion() != null ? !artifact.getVersion().equals( dependency.getVersion() )
78              : dependency.getVersion() != null )
79          {
80              return false;
81          }
82          if ( artifact.getType() != null ? !artifact.getType().equals( dependency.getType() )
83              : dependency.getType() != null )
84          {
85              return false;
86          }
87          if ( artifact.getClassifier() != null ? !artifact.getClassifier().equals( dependency.getClassifier() )
88              : dependency.getClassifier() != null )
89          {
90              return false;
91          }
92          if ( artifact.getScope() != null ? !artifact.getScope().equals( dependency.getScope() )
93              : dependency.getScope() != null )
94          {
95              return false;
96          }
97          if ( artifact.isOptional() != dependency.isOptional() )
98          {
99              return false;
100         }
101 
102         return true;
103     }
104 
105     public static boolean dependencyEquals( Dependency first, Dependency second )
106     {
107         if ( first == second )
108         {
109             return true;
110         }
111 
112         if ( first.isOptional() != second.isOptional() )
113         {
114             return false;
115         }
116         if ( !StringUtils.equals( first.getArtifactId(), second.getArtifactId() ) )
117         {
118             return false;
119         }
120         if ( first.getClassifier() != null ? !first.getClassifier().equals( second.getClassifier() )
121             : second.getClassifier() != null )
122         {
123             return false;
124         }
125         if ( first.getExclusions() != null ? !first.getExclusions().equals( second.getExclusions() )
126             : second.getExclusions() != null )
127         {
128             return false;
129         }
130         if ( !StringUtils.equals( first.getGroupId(), second.getGroupId() ) )
131         {
132             return false;
133         }
134         if ( first.getScope() != null ? !first.getScope().equals( second.getScope() ) : second.getScope() != null )
135         {
136             return false;
137         }
138         if ( first.getSystemPath() != null ? !first.getSystemPath().equals( second.getSystemPath() )
139             : second.getSystemPath() != null )
140         {
141             return false;
142         }
143         if ( first.getType() != null ? !first.getType().equals( second.getType() ) : second.getType() != null )
144         {
145             return false;
146         }
147         if ( first.getVersion() != null ? !first.getVersion().equals( second.getVersion() )
148             : second.getVersion() != null )
149         {
150             return false;
151         }
152         return true;
153     }
154 
155 
156 }