View Javadoc
1   /*
2    * (C) Copyright 2006-2015 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.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.tools.ant.BuildException;
24  import org.apache.tools.ant.types.DataType;
25  import org.apache.tools.ant.types.Resource;
26  import org.apache.tools.ant.types.ResourceCollection;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.resolution.ArtifactResult;
29  import org.eclipse.aether.resolution.DependencyResult;
30  import org.nuxeo.build.maven.AntBuildMojo;
31  import org.nuxeo.build.maven.ArtifactDescriptor;
32  import org.nuxeo.build.maven.filter.AndFilter;
33  import org.nuxeo.build.maven.filter.CompositeFilter;
34  import org.nuxeo.build.maven.filter.Filter;
35  import org.nuxeo.build.maven.graph.DependencyUtils;
36  import org.nuxeo.build.maven.graph.Graph;
37  import org.nuxeo.build.maven.graph.Node;
38  
39  /**
40   * TODO NXBT-258
41   */
42  public class ArtifactDependencies extends DataType implements ResourceCollection {
43  
44      protected Graph graph = AntBuildMojo.getInstance().getGraph();
45  
46      protected Node node;
47  
48      protected List<Artifact> artifacts;
49  
50      public String key;
51  
52      public int depth = 1;
53  
54      public ArtifactDescriptor ad = new ArtifactDescriptor();
55  
56      public Includes includes;
57  
58      public Excludes excludes;
59  
60      public void setDepth(String depth) {
61          this.depth = Expand.readExpand(depth);
62      }
63  
64      public void addExcludes(@SuppressWarnings("hiding") Excludes excludes) {
65          if (this.excludes != null) {
66              throw new BuildException("Found an Excludes that is defined more than once in an artifact dependencies");
67          }
68          this.excludes = excludes;
69      }
70  
71      public void addIncludes(@SuppressWarnings("hiding") Includes includes) {
72          if (this.includes != null) {
73              throw new BuildException("Found an Includes that is defined more than once in an artifact dependencies");
74          }
75          this.includes = includes;
76      }
77  
78      public void setKey(String pattern) {
79          key = pattern;
80          ad = ArtifactDescriptor.parseQuietly(pattern);
81      }
82  
83      public void setArtifactId(String artifactId) {
84          ad.setArtifactId(artifactId);
85      }
86  
87      public void setGroupId(String groupId) {
88          ad.setGroupId(groupId);
89      }
90  
91      public void setType(String type) {
92          ad.setType(type);
93      }
94  
95      public void setVersion(String version) {
96          ad.setVersion(version);
97      }
98  
99      public Node getNode() {
100         if (node == null) {
101             node = graph.findNode(key, ad);
102         }
103         return node;
104     }
105 
106     public List<Artifact> getArtifacts() {
107         if (artifacts == null) {
108             Filter filter = Filter.ANY;
109             if (includes != null || excludes != null) {
110                 AndFilter andf = new AndFilter();
111                 if (includes != null) {
112                     andf.addFilter(includes.getFilter());
113                 }
114                 if (excludes != null) {
115                     andf.addFilter(excludes.getFilter());
116                 }
117                 filter = CompositeFilter.compact(andf);
118             }
119             DependencyResult result = DependencyUtils.resolveDependencies(getNode(), filter, depth);
120             List<ArtifactResult> results = result.getArtifactResults();
121             artifacts = new ArrayList<>();
122             for (ArtifactResult artifactResult : results) {
123                 artifacts.add(artifactResult.getArtifact());
124             }
125             if (!filter.accept(result.getRoot(), null)) {
126                 Artifact root = result.getRoot().getArtifact();
127                 artifacts.remove(root);
128             }
129         }
130         return artifacts;
131     }
132 
133     @Override
134     public Iterator<Resource> iterator() {
135         return ArtifactSet.createIterator(getArtifacts());
136     }
137 
138     @Override
139     public int size() {
140         return getArtifacts().size();
141     }
142 
143     @Override
144     public boolean isFilesystemOnly() {
145         return true;
146     }
147 }