View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.plugin.jboss.modules.domain;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.sonatype.aether.collection.DependencyTraverser;
22  
23  import de.smartics.maven.plugin.jboss.modules.aether.DependencyTraverserGenerator;
24  import de.smartics.maven.plugin.jboss.modules.aether.PruningDependencyTraverser;
25  import de.smartics.maven.plugin.jboss.modules.descriptor.ArtifactClusion;
26  import de.smartics.maven.plugin.jboss.modules.descriptor.ModuleDescriptor;
27  
28  /**
29   * Implements pruning on dependency excludes an modules tagged as 'skip'.
30   */
31  public class PrunerGenerator implements DependencyTraverserGenerator
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    // --- members --------------------------------------------------------------
38  
39    /**
40     * A list of dependencies to be excluded from the transitive dependency
41     * collection process.
42     */
43    private final List<ArtifactClusion> dependencyExcludes;
44  
45    /**
46     * The module descriptors that skip dependency resolution.
47     */
48    private final List<ModuleDescriptor> skipModules;
49  
50    /**
51     * The flag that allows to globally ignore exclusions declared in Maven
52     * dependencies.
53     */
54    private final boolean ignoreDependencyExclusions;
55  
56    // ****************************** Initializer *******************************
57  
58    // ****************************** Constructors ******************************
59  
60    @Override
61    public boolean isIgnoreDependencyExclusions()
62    {
63      return ignoreDependencyExclusions;
64    }
65  
66    /**
67     * Default constructor.
68     *
69     * @param dependencyExcludes a list of dependencies to be excluded from the
70     *          transitive dependency collection process.
71     * @param modules lost of modules to calculate the skip modules.
72     * @param ignoreDependencyExclusions the flag that allows to globally ignore
73     *          exclusions declared in Maven dependencies.
74     */
75    public PrunerGenerator(final List<ArtifactClusion> dependencyExcludes,
76        final List<ModuleDescriptor> modules,
77        final boolean ignoreDependencyExclusions)
78    {
79      this.dependencyExcludes =
80          dependencyExcludes != null ? dependencyExcludes
81              : new ArrayList<ArtifactClusion>();
82      this.skipModules = calcSkipModules(modules);
83      this.ignoreDependencyExclusions = ignoreDependencyExclusions;
84    }
85  
86    // ****************************** Inner Classes *****************************
87  
88    // ********************************* Methods ********************************
89  
90    // --- init -----------------------------------------------------------------
91  
92    // --- get&set --------------------------------------------------------------
93  
94    private List<ModuleDescriptor> calcSkipModules(
95        final List<ModuleDescriptor> modules)
96    {
97      if (modules == null)
98      {
99        return new ArrayList<ModuleDescriptor>();
100     }
101     final List<ModuleDescriptor> skipModules =
102         new ArrayList<ModuleDescriptor>(modules.size());
103     for (final ModuleDescriptor module : modules)
104     {
105       if (module.getDirectives().getSkip())
106       {
107         skipModules.add(module);
108       }
109     }
110     return skipModules;
111   }
112 
113   // --- business -------------------------------------------------------------
114 
115   /**
116    * Creates a dependency traverser to prune dependency branches that are
117    * excluded as dependencies or skipped as modules.
118    *
119    * @param delegate the traverse to delegate to after own check is not
120    *          rejecting.
121    * @return the traverser.
122    * @throws NullPointerException if {@code delegate} is <code>null</code>.
123    */
124   public DependencyTraverser createDependencyTraverser(
125       final DependencyTraverser delegate) throws NullPointerException
126   {
127     return new PruningDependencyTraverser(delegate, dependencyExcludes,
128         skipModules);
129   }
130 
131   // --- object basics --------------------------------------------------------
132 
133 }