4141import java .io .File ;
4242import java .io .FileOutputStream ;
4343import java .io .IOException ;
44+ import java .io .OutputStream ;
4445import java .io .OutputStreamWriter ;
4546import java .io .Writer ;
4647import java .net .URISyntaxException ;
@@ -89,7 +90,7 @@ public class DashboardMain {
8990 */
9091 public static void main (String [] arguments )
9192 throws IOException , TemplateException , RepositoryException , URISyntaxException ,
92- ParseException , MavenRepositoryException {
93+ ParseException , MavenRepositoryException {
9394 DashboardArguments dashboardArguments = DashboardArguments .readCommandLine (arguments );
9495
9596 // If looking to edit the dashboard structure, see DashboardMain#generateDashboard.
@@ -110,17 +111,28 @@ public static void main(String[] arguments)
110111 }
111112
112113 if (dashboardArguments .getReport ()) {
113- if (!report (bom )) {
114+ if (!report (bom , System . out )) {
114115 throw new RuntimeException ("Failed to converge dependencies" );
115116 }
116117 } else {
117118 generate (bom );
118119 }
120+
121+ if (dashboardArguments .getOutputFile () != null ) {
122+ Path relativePath = dashboardArguments .getOutputFile ();
123+ Files .createDirectories (relativePath .getParent ());
124+ File file = new File (String .valueOf (relativePath ));
125+ OutputStream outputStream = new FileOutputStream (file );
126+ if (!report (bom ,outputStream )) {
127+ throw new RuntimeException ("Failed to converge dependencies" );
128+ }
129+ outputStream .close ();
130+ }
119131 }
120132
121133 private static void generateAllVersions (String versionlessCoordinates )
122134 throws IOException , TemplateException , RepositoryException , URISyntaxException ,
123- MavenRepositoryException {
135+ MavenRepositoryException {
124136 List <String > elements = Splitter .on (':' ).splitToList (versionlessCoordinates );
125137 checkArgument (
126138 elements .size () == 2 ,
@@ -178,7 +190,7 @@ private static ArtifactCache buildCache(Bom bom) {
178190 return loadArtifactInfo (managedDependencies );
179191 }
180192
181- private static boolean report (Bom bom ) {
193+ private static boolean report (Bom bom , OutputStream outputStream ) throws IOException {
182194 ArtifactCache cache = buildCache (bom );
183195 Map <Artifact , ArtifactInfo > infoMap = cache .getInfoMap ();
184196 String cloudBomVersion =
@@ -194,6 +206,7 @@ private static boolean report(Bom bom) {
194206 Multimap <String , String > sharedDepsToLibraries = ArrayListMultimap .create ();
195207 ImmutableSortedSet .Builder <ComparableVersion > sharedDepsVersionsBuilder =
196208 ImmutableSortedSet .reverseOrder ();
209+
197210 for (Map .Entry <String , String > entry : sharedDependencyVersions .entrySet ()) {
198211 if (!entry .getValue ().isEmpty ()) {
199212 sharedDepsToLibraries .put (entry .getValue (), entry .getKey ());
@@ -203,32 +216,30 @@ private static boolean report(Bom bom) {
203216
204217 SortedSet <ComparableVersion > sharedDepsVersions = sharedDepsVersionsBuilder .build ();
205218 if (sharedDepsVersions .size () == 1 ) {
206- System . out . println ("Shared dependencies converge \\ o/" );
219+ outputStream . write ("Shared dependencies converge \\ o/\n " . getBytes () );
207220 return true ;
208221 }
209222
210223 // Find the largest shared dependency version
211224 ComparableVersion largest = null ;
225+ StringBuilder outputString = new StringBuilder ();
212226 for (ComparableVersion version : sharedDepsVersions ) {
213227 if (largest == null ) {
214228 largest = version ;
215- System . out . println ("Greatest shared-dependencies version: " + version .toString ());
229+ outputString . append ("Greatest shared-dependencies version: " ). append ( version .toString ());
216230 } else {
217231 Collection <String > artifacts = sharedDepsToLibraries .get (version .toString ());
218- System .out .println ("-----------------------" );
219- System .out .println (
220- String .format (
221- "Found %d artifacts with shared-dependencies version: %s" ,
222- artifacts .size (), version .toString ()));
223- ;
232+ outputString .append ("\n -----------------------" );
233+ outputString .append ("\n Found " + artifacts .size () + " artifacts with shared dependencies version: " + version .toString ());
234+
224235 for (String artifactKey : artifacts ) {
225236 String artifactVersion = currentVersions .get (artifactKey );
226237 String artifact = artifactKey .split (":" )[0 ];
227- System . out . println ( String . format ( "- %s:%s" , artifact , artifactVersion ) );
238+ outputString . append ( " \n - " + artifact + ":" + artifactVersion );
228239 }
229240 }
241+ outputStream .write (outputString .toString ().getBytes ());
230242 }
231-
232243 return false ;
233244 }
234245
0 commit comments