View Javadoc
1   /*
2    * (C) Copyright 2006-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   *     bstefanescu, slacoin, jcarsique
16   */
17  package org.nuxeo.build.ant.artifact;
18  
19  import java.util.Collection;
20  
21  import org.apache.tools.ant.BuildException;
22  import org.apache.tools.ant.Task;
23  import org.eclipse.aether.graph.DependencyNode;
24  import org.nuxeo.build.maven.AntBuildMojo;
25  import org.nuxeo.build.maven.filter.AndFilter;
26  import org.nuxeo.build.maven.filter.CompositeFilter;
27  import org.nuxeo.build.maven.graph.Graph;
28  import org.nuxeo.build.maven.graph.Node;
29  
30  /**
31   * TODO NXBT-258
32   */
33  public class ExpandTask extends Task {
34  
35      public String key;
36  
37      public int depth = Integer.MAX_VALUE;
38  
39      public AndFilter filter = new AndFilter();
40  
41      public void setKey(String key) {
42          this.key = key;
43      }
44  
45      public void setDepth(String depth) {
46          this.depth = Expand.readExpand(depth);
47      }
48  
49      public void addExcludes(Excludes excludes) {
50          filter.addFilter(excludes.getFilter());
51      }
52  
53      public void addIncludes(Includes includes) {
54          filter.addFilter(includes.getFilter());
55      }
56  
57      protected boolean acceptNode(DependencyNode node) {
58          return true;
59      }
60  
61      @Override
62      public void execute() throws BuildException {
63          AntBuildMojo mojo = AntBuildMojo.getInstance();
64          Graph graph;
65          if (key != null) {
66              graph = new Graph();
67              // TODO NXBT-258 mojo graph can be empty!
68              Collection<Node> nodes = mojo.getGraph().find(key);
69              addRootNodes(graph, nodes);
70          } else {
71              graph = mojo.newGraph();
72          }
73          graph.resolveDependencies(CompositeFilter.compact(filter), depth);
74      }
75  
76      public void addRootNodes(Graph graph,
77              Collection<? extends DependencyNode> nodes) {
78          for (DependencyNode node : nodes) {
79              if ("pom".equals(node.getArtifact().getExtension())) {
80                  // Add the POM direct dependencies as root nodes instead
81                  // addRootNodes(graph, node.getChildren());
82                  for (DependencyNode child : node.getChildren()) {
83                      if (acceptNode(node)) {
84                          graph.addRootNode(new Node(graph, child));
85                      }
86                  }
87              } else if (acceptNode(node)) {
88                  graph.addRootNode(new Node(graph, node));
89              }
90          }
91      }
92  }