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