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 937155 2015-01-21 21:53:50Z khmarbaise $
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,
162                                                 outputScope, sort ) );
163         }
164 
165         if ( this.skippedDependencies != null && !this.skippedDependencies.isEmpty() )
166         {
167             sb.append( "\n" );
168             sb.append( "The following files were skipped:\n" );
169             Set<Artifact> skippedDependencies = new LinkedHashSet<Artifact>();
170             skippedDependencies.addAll( this.skippedDependencies );
171             sb.append( buildArtifactListOutput( skippedDependencies, outputAbsoluteArtifactFilename,
172                                                 outputScope, sort ) );
173         }
174 
175         if ( this.unResolvedDependencies != null && !this.unResolvedDependencies.isEmpty() )
176         {
177             sb.append( "\n" );
178             sb.append( "The following files have NOT been resolved:\n" );
179             Set<Artifact> unResolvedDependencies = new LinkedHashSet<Artifact>();
180             unResolvedDependencies.addAll( this.unResolvedDependencies );
181             sb.append( buildArtifactListOutput( unResolvedDependencies, outputAbsoluteArtifactFilename,
182                                                 outputScope, sort ) );
183         }
184         sb.append( "\n" );
185 
186         return sb.toString();
187     }
188 
189     private StringBuilder buildArtifactListOutput( Set<Artifact> artifacts, boolean outputAbsoluteArtifactFilename,
190                                                    boolean outputScope, boolean sort )
191     {
192         StringBuilder sb = new StringBuilder();
193         List<String> artifactStringList = new ArrayList<String>();
194         for ( Artifact artifact : artifacts )
195         {
196             String artifactFilename = null;
197             if ( outputAbsoluteArtifactFilename )
198             {
199                 try
200                 {
201                     // we want to print the absolute file name here
202                     artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
203                 }
204                 catch ( NullPointerException e )
205                 {
206                     // ignore the null pointer, we'll output a null string
207                     artifactFilename = null;
208                 }
209             }
210 
211             String id = outputScope ? artifact.toString() : artifact.getId();
212 
213             artifactStringList.add( "   " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" )
214                 + "\n" );
215         }
216         if ( sort )
217         {
218             Collections.sort( artifactStringList );
219         }
220         for ( String artifactString : artifactStringList )
221         {
222             sb.append( artifactString );
223         }
224         return sb;
225     }
226 }