View Javadoc
1   /*
2    * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     Julien Carsique
16   *
17   */
18  
19  package org.nuxeo.build.maven.graph;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.UnsupportedEncodingException;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import org.apache.tools.ant.Project;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.graph.Dependency;
31  import org.eclipse.aether.graph.DependencyNode;
32  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
33  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
34  import org.nuxeo.build.ant.AntClient;
35  import org.nuxeo.build.ant.artifact.PrintGraphTask;
36  import org.nuxeo.build.maven.AntBuildMojo;
37  
38  /**
39   * Prints all dependencies in a flat sorted output. Duplicates and conflicting
40   * versions are removed.
41   *
42   * @since 2.0
43   */
44  public class FlatPrinterDependencyVisitor extends AbstractDependencyVisitor {
45  
46      protected int format;
47  
48      protected OutputStream output;
49  
50      protected Set<String> lines = new TreeSet<>();
51  
52      /**
53       * @param format 0 = standard GAV ; 1 = File + GAV
54       */
55      public FlatPrinterDependencyVisitor(OutputStream output, int format,
56              List<String> scopes) {
57          super(scopes);
58          this.output = output;
59          this.format = format;
60      }
61  
62      @Override
63      protected void doVisit(DependencyNode node, boolean newNode) {
64          if (newNode) {
65              print(node);
66          }
67      }
68  
69      /**
70       * output must be sorted before write; see {@link #print()}
71       */
72      protected void print(DependencyNode node) {
73          if ("pom".equals(node.getArtifact().getExtension())) {
74              return;
75          }
76          Artifact artifact = node.getArtifact();
77          DependencyNode winner = (DependencyNode) node.getData().get(
78                  ConflictResolver.NODE_DATA_WINNER);
79          if (winner != null
80                  && !ArtifactIdUtils.equalsId(artifact, winner.getArtifact())) {
81              Artifact w = winner.getArtifact();
82              if (ArtifactIdUtils.toVersionlessId(artifact).equals(
83                      ArtifactIdUtils.toVersionlessId(w))) {
84                  AntClient.getInstance().log(
85                          String.format("Ignored conflicting node %s with %s",
86                                  node, w.getVersion()), Project.MSG_DEBUG);
87              } else {
88                  AntClient.getInstance().log(
89                          String.format("Ignored conflicting node %s with %s",
90                                  node, w), Project.MSG_DEBUG);
91              }
92          } else {
93              String toString = toString(node)
94                      + System.getProperty("line.separator");
95              lines.add(toString);
96          }
97      }
98  
99      public void print() throws UnsupportedEncodingException, IOException {
100         for (String line : lines) {
101             output.write(line.getBytes(AntBuildMojo.getInstance().getEncoding()));
102         }
103     }
104 
105     /**
106      * @return String representation depending on {@link #format}
107      * @see PrintGraphTask#FORMAT_GAV
108      * @see PrintGraphTask#FORMAT_KV_F_GAV
109      */
110     public String toString(DependencyNode node) {
111         Artifact artifact = node.getArtifact();
112         Dependency dependency = node.getDependency();
113         StringBuilder sb = new StringBuilder();
114         switch (format) {
115         case 1:
116             sb.append(artifact.getFile().getName());
117             sb.append('=');
118             // fall through
119         case 0:
120             sb.append(artifact.getGroupId());
121             sb.append(':').append(artifact.getArtifactId());
122             sb.append(':').append(artifact.getVersion());
123             sb.append(':').append(artifact.getExtension());
124             sb.append(':').append(artifact.getClassifier());
125             sb.append(':').append(dependency.getScope());
126             break;
127         default:
128             return "Unknown format: " + format + "!";
129         }
130         return sb.toString();
131     }
132 
133 }