View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  
20  package org.eclipse.jetty.maven.plugin;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.maven.model.Plugin;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.util.xml.Xpp3Dom;
31  import org.eclipse.jetty.util.StringUtil;
32  
33  /**
34   * WarPluginInfo
35   * <p>
36   * Information about the maven-war-plugin contained in the pom
37   */
38  public class WarPluginInfo
39  {
40      private MavenProject _project;
41      private Plugin _plugin;
42      private List<String> _dependentMavenWarIncludes;
43      private List<String> _dependentMavenWarExcludes;
44      private List<OverlayConfig> _overlayConfigs;
45      
46      
47      public WarPluginInfo (MavenProject project)
48      {
49          _project = project;
50      }
51  
52      
53      
54      
55      /**
56       * Find the maven-war-plugin, if one is configured
57       * @return the plugin
58       */
59      public Plugin getPlugin()
60      {
61          if (_plugin == null)
62          {
63              List plugins = _project.getBuildPlugins();
64              if (plugins == null)
65                  return null;
66  
67  
68              Iterator itor = plugins.iterator();
69              while (itor.hasNext() && _plugin==null)
70              {
71                  Plugin plugin = (Plugin)itor.next();
72                  if ("maven-war-plugin".equals(plugin.getArtifactId()))
73                      _plugin = plugin;
74              }
75          }
76          return _plugin;
77      }
78  
79      
80      
81  
82      /**
83       * Get value of dependentWarIncludes for maven-war-plugin
84       * @return the list of dependent war includes
85       */
86      public List<String> getDependentMavenWarIncludes()
87      {
88          if (_dependentMavenWarIncludes == null)
89          {
90              getPlugin();
91  
92              if (_plugin == null)
93                  return null;
94  
95              Xpp3Dom node = (Xpp3Dom)_plugin.getConfiguration();
96              if (node == null)
97                  return null;
98  
99              node = node.getChild("dependentWarIncludes");
100             if (node == null)
101                 return null;
102             String val = node.getValue();
103             _dependentMavenWarIncludes = StringUtil.csvSplit(null,val,0,val.length());
104         }
105         return _dependentMavenWarIncludes;
106     }
107 
108 
109     
110     
111     /**
112      * Get value of dependentWarExcludes for maven-war-plugin
113      * @return the list of dependent war excludes
114      */
115     public List<String> getDependentMavenWarExcludes()
116     {
117         if (_dependentMavenWarExcludes == null)
118         {
119             getPlugin();
120 
121             if (_plugin == null)
122                 return null;
123 
124             Xpp3Dom node = (Xpp3Dom)_plugin.getConfiguration();
125             if (node == null)
126                 return null;
127 
128             node = node.getChild("dependentWarExcludes");
129             if (node == null)
130                 return null;
131             String val = node.getValue();
132             _dependentMavenWarExcludes = StringUtil.csvSplit(null,val,0,val.length());
133         }
134         return _dependentMavenWarExcludes;
135     }
136 
137     
138     
139     
140     /**
141      * Get config for any overlays that have been declared for the maven-war-plugin.
142      * 
143      * @return the list of overlay configs
144      */
145     public List<OverlayConfig> getMavenWarOverlayConfigs ()
146     {
147         if (_overlayConfigs == null)
148         {
149             getPlugin();
150 
151             if (_plugin == null)
152                 return Collections.emptyList();
153 
154             getDependentMavenWarIncludes();
155             getDependentMavenWarExcludes();
156 
157             Xpp3Dom node = (Xpp3Dom)_plugin.getConfiguration();
158             if (node == null)
159                 return Collections.emptyList();
160 
161             node = node.getChild("overlays");
162             if (node == null)
163                 return Collections.emptyList();
164 
165             Xpp3Dom[] nodes = node.getChildren("overlay");
166             if (nodes == null)
167                 return Collections.emptyList();
168 
169             _overlayConfigs = new ArrayList<OverlayConfig>();
170             for (int i=0;i<nodes.length;i++)
171             {
172                 OverlayConfig overlayConfig = new OverlayConfig(nodes[i], _dependentMavenWarIncludes, _dependentMavenWarExcludes);
173                 _overlayConfigs.add(overlayConfig);
174             }
175         }
176 
177         return _overlayConfigs;
178     }
179     
180     
181     
182     
183     /**
184      * @return the xml as a string
185      */
186     public String getMavenWarOverlayConfigAsString ()
187     {
188         getPlugin();
189 
190         if (_plugin == null)
191             return "";
192         
193         Xpp3Dom node = (Xpp3Dom)_plugin.getConfiguration();
194         if (node == null)
195             return "";
196         return node.toString();
197     }
198 }