View Javadoc

1   package org.apache.maven.plugin.dependency.utils;
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  /**
23   *
24   */
25  
26  import java.util.LinkedHashSet;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.Artifact;
30  
31  /**
32   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
33   * @version $Id: DependencyStatusSets.java 1384337 2012-09-13 13:53:19Z olamy $
34   */
35  public class DependencyStatusSets
36  {
37  
38      Set<Artifact> resolvedDependencies = null;
39  
40      Set<Artifact> unResolvedDependencies = null;
41  
42      Set<Artifact> skippedDependencies = null;
43  
44      public DependencyStatusSets()
45      {
46  
47      }
48  
49      public DependencyStatusSets( Set<Artifact> resolved, Set<Artifact> unResolved, Set<Artifact> skipped )
50      {
51          if ( resolved != null )
52          {
53              this.resolvedDependencies = new LinkedHashSet<Artifact>( resolved );
54          }
55          if ( unResolved != null )
56          {
57              this.unResolvedDependencies = new LinkedHashSet<Artifact>( unResolved );
58          }
59          if ( skipped != null )
60          {
61              this.skippedDependencies = new LinkedHashSet<Artifact>( skipped );
62          }
63      }
64  
65      /**
66       * @return Returns the resolvedDependencies.
67       */
68      public Set<Artifact> getResolvedDependencies()
69      {
70          return this.resolvedDependencies;
71      }
72  
73      /**
74       * @param resolvedDependencies
75       *            The resolvedDependencies to set.
76       */
77      public void setResolvedDependencies( Set<Artifact> resolvedDependencies )
78      {
79          if ( resolvedDependencies != null )
80          {
81              this.resolvedDependencies = new LinkedHashSet<Artifact>( resolvedDependencies );
82          }
83          else
84          {
85              this.resolvedDependencies = null;
86          }
87      }
88  
89      /**
90       * @return Returns the skippedDependencies.
91       */
92      public Set<Artifact> getSkippedDependencies()
93      {
94          return this.skippedDependencies;
95      }
96  
97      /**
98       * @param skippedDependencies
99       *            The skippedDependencies to set.
100      */
101     public void setSkippedDependencies( Set<Artifact> skippedDependencies )
102     {
103         if ( skippedDependencies != null )
104         {
105             this.skippedDependencies = new LinkedHashSet<Artifact>( skippedDependencies );
106         }
107         else
108         {
109             this.skippedDependencies = null;
110         }
111     }
112 
113     /**
114      * @return Returns the unResolvedDependencies.
115      */
116     public Set<Artifact> getUnResolvedDependencies()
117     {
118         return this.unResolvedDependencies;
119     }
120 
121     /**
122      * @param unResolvedDependencies
123      *            The unResolvedDependencies to set.
124      */
125     public void setUnResolvedDependencies( Set<Artifact> unResolvedDependencies )
126     {
127         if ( unResolvedDependencies != null )
128         {
129             this.unResolvedDependencies = new LinkedHashSet<Artifact>( unResolvedDependencies );
130         }
131         else
132         {
133             this.unResolvedDependencies = null;
134         }
135     }
136 
137     public String getOutput( boolean outputAbsoluteArtifactFilename )
138     {
139         return getOutput( outputAbsoluteArtifactFilename, true );
140     }
141 
142     public String getOutput( boolean outputAbsoluteArtifactFilename, boolean outputScope )
143     {
144         StringBuilder sb = new StringBuilder();
145         sb.append( "\n" );
146         sb.append( "The following files have been resolved:\n" );
147         if ( this.resolvedDependencies == null || this.resolvedDependencies.isEmpty() )
148         {
149             sb.append( "   none\n" );
150         }
151         else
152         {
153             for ( Artifact artifact : resolvedDependencies )
154             {
155                 String artifactFilename = null;
156                 if ( outputAbsoluteArtifactFilename )
157                 {
158                     try
159                     {
160                         // we want to print the absolute file name here
161                         artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
162                     }
163                     catch ( NullPointerException e )
164                     {
165                         // ignore the null pointer, we'll output a null string
166                         artifactFilename = null;
167                     }
168                 }
169 
170                 String id = outputScope ? artifact.toString() : artifact.getId();
171 
172                 sb.append( "   " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" ) + "\n" );
173             }
174         }
175 
176         if ( this.skippedDependencies != null && !this.skippedDependencies.isEmpty() )
177         {
178             sb.append( "\n" );
179             sb.append( "The following files were skipped:\n" );
180             Set<Artifact> skippedDependencies = new LinkedHashSet<Artifact>();
181             skippedDependencies.addAll( this.skippedDependencies );
182             for ( Artifact artifact : skippedDependencies )
183             {
184                 sb.append( "   " + artifact.getId() + "\n" );
185             }
186         }
187 
188         if ( this.unResolvedDependencies != null && !this.unResolvedDependencies.isEmpty() )
189         {
190             sb.append( "\n" );
191             sb.append( "The following files have NOT been resolved:\n" );
192             Set<Artifact> unResolvedDependencies = new LinkedHashSet<Artifact>();
193             unResolvedDependencies.addAll( this.unResolvedDependencies );
194             for ( Artifact artifact : unResolvedDependencies )
195             {
196                 sb.append( "   " + artifact.getId() + "\n" );
197             }
198         }
199         sb.append( "\n" );
200 
201         return sb.toString();
202     }
203 }