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.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.StringTokenizer;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.Project;
28  import org.eclipse.aether.graph.DependencyNode;
29  import org.eclipse.aether.util.artifact.JavaScopes;
30  import org.nuxeo.build.maven.filter.Filter;
31  
32  /**
33   * TODO NXBT-258
34   */
35  public class NuxeoExpandTask extends ExpandTask {
36  
37      private Map<String, Boolean> includedScopes = new HashMap<>();
38  
39      private boolean includeCompileScope = true;
40  
41      private boolean includeProvidedScope = false;
42  
43      private boolean includeRuntimeScope = true;
44  
45      private boolean includeTestScope = false;
46  
47      private boolean includeSystemScope = true;
48  
49      private String[] groupPrefixes = new String[] { "org.nuxeo" };
50  
51      /**
52       * @param groupPrefixes Comma separated list of accepted group prefixes
53       * @since 1.11
54       */
55      public void setGroupPrefixes(String groupPrefixes) {
56          List<String> prefixes = new ArrayList<>();
57          StringTokenizer st = new StringTokenizer(groupPrefixes, ",");
58          while (st.hasMoreTokens()) {
59              prefixes.add(st.nextToken());
60          }
61          this.groupPrefixes = prefixes.toArray(new String[0]);
62      }
63  
64      public NuxeoExpandTask() {
65          super();
66          setDepth("all");
67      }
68  
69      @Override
70      public void execute() throws BuildException {
71          getIncludedScopes().put(JavaScopes.COMPILE, includeCompileScope);
72          getIncludedScopes().put(JavaScopes.PROVIDED, includeProvidedScope);
73          getIncludedScopes().put(JavaScopes.RUNTIME, includeRuntimeScope);
74          getIncludedScopes().put(JavaScopes.TEST, includeTestScope);
75          getIncludedScopes().put(JavaScopes.SYSTEM, includeSystemScope);
76  
77          filter.addFilter(new Filter() {
78  
79              @Override
80              public boolean accept(Artifact artifact) {
81                  return true;
82              }
83  
84              @Override
85              public boolean accept(DependencyNode node,
86                      List<DependencyNode> parents) {
87                  if (node.getDependency().isOptional()) {
88                      return false;
89                  }
90                  if ("".equals(node.getDependency().getScope())) {
91                      log("Node with no scope accepted (root node?): " + node,
92                              Project.MSG_DEBUG);
93                      return true;
94                  }
95                  return getIncludedScopes().get(node.getDependency().getScope());
96              }
97          });
98          super.execute();
99      }
100 
101     @Override
102     protected boolean acceptNode(DependencyNode node) {
103         for (String prefix : groupPrefixes) {
104             if (node.getArtifact().getGroupId().startsWith(prefix)) {
105                 return true;
106             }
107         }
108         return false;
109     }
110 
111     /**
112      * @since 1.10.2
113      */
114     protected Map<String, Boolean> getIncludedScopes() {
115         return includedScopes;
116     }
117 
118     /**
119      * @since 1.10.2
120      */
121     public void setIncludeCompileScope(boolean includeCompileScope) {
122         this.includeCompileScope = includeCompileScope;
123     }
124 
125     /**
126      * @since 1.10.2
127      */
128     public void setIncludeProvidedScope(boolean includeProvidedScope) {
129         this.includeProvidedScope = includeProvidedScope;
130     }
131 
132     /**
133      * @since 1.10.2
134      */
135     public void setIncludeRuntimeScope(boolean includeRuntimeScope) {
136         this.includeRuntimeScope = includeRuntimeScope;
137     }
138 
139     /**
140      * @since 1.10.2
141      */
142     public void setIncludeTestScope(boolean includeTestScope) {
143         this.includeTestScope = includeTestScope;
144     }
145 
146     /**
147      * @since 1.10.2
148      */
149     public void setIncludeSystemScope(boolean includeSystemScope) {
150         this.includeSystemScope = includeSystemScope;
151     }
152 }