View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  
32  
33  
34  /**
35   * WarPluginInfo
36   *
37   * Information about the maven-war-plugin contained in the pom
38   */
39  public class WarPluginInfo
40  {
41      private MavenProject _project;
42      private Plugin _plugin;
43      private List<String> _dependentMavenWarIncludes;
44      private List<String> _dependentMavenWarExcludes;
45      private List<OverlayConfig> _overlayConfigs;
46      
47      
48      
49      /**
50       * @param project
51       */
52      public WarPluginInfo (MavenProject project)
53      {
54          _project = project;
55      }
56  
57      
58      
59      
60      /**
61       * Find the maven-war-plugin, if one is configured
62       * @return
63       */
64      public Plugin getPlugin()
65      {
66          if (_plugin == null)
67          {
68              List plugins = _project.getBuildPlugins();
69              if (plugins == null)
70                  return null;
71  
72  
73              Iterator itor = plugins.iterator();
74              while (itor.hasNext() && _plugin==null)
75              {
76                  Plugin plugin = (Plugin)itor.next();
77                  if ("maven-war-plugin".equals(plugin.getArtifactId()))
78                      _plugin = plugin;
79              }
80          }
81          return _plugin;
82      }
83  
84      
85      
86  
87      /**
88       * Get value of dependentWarIncludes for maven-war-plugin
89       * @return
90       */
91      public List<String> getDependentMavenWarIncludes()
92      {
93          if (_dependentMavenWarIncludes == null)
94          {
95              getPlugin();
96  
97              if (_plugin == null)
98                  return null;
99  
100             Xpp3Dom node = (Xpp3Dom)_plugin.getConfiguration();
101             if (node == null)
102                 return null;
103 
104             node = node.getChild("dependentWarIncludes");
105             if (node == null)
106                 return null;
107             String val = node.getValue();
108             _dependentMavenWarIncludes = Arrays.asList(val.split(",")); 
109         }
110         return _dependentMavenWarIncludes;
111     }
112 
113 
114     
115     
116     /**
117      * Get value of dependentWarExcludes for maven-war-plugin
118      * @return
119      */
120     public List<String> getDependentMavenWarExcludes()
121     {
122         if (_dependentMavenWarExcludes == null)
123         {
124             getPlugin();
125 
126             if (_plugin == null)
127                 return null;
128 
129             Xpp3Dom node = (Xpp3Dom)_plugin.getConfiguration();
130             if (node == null)
131                 return null;
132 
133             node = node.getChild("dependentWarExcludes");
134             if (node == null)
135                 return null;
136             String val = node.getValue();
137             _dependentMavenWarExcludes = Arrays.asList(val.split(","));
138         }
139         return _dependentMavenWarExcludes;
140     }
141 
142     
143     
144     
145     /**
146      * Get config for any overlays that have been declared for the maven-war-plugin.
147      * 
148      * @return
149      */
150     public List<OverlayConfig> getMavenWarOverlayConfigs ()
151     {
152         if (_overlayConfigs == null)
153         {
154             getPlugin();
155 
156             if (_plugin == null)
157                 return Collections.emptyList();
158 
159             getDependentMavenWarIncludes();
160             getDependentMavenWarExcludes();
161 
162             Xpp3Dom node = (Xpp3Dom)_plugin.getConfiguration();
163             if (node == null)
164                 return Collections.emptyList();
165 
166             node = node.getChild("overlays");
167             if (node == null)
168                 return Collections.emptyList();
169 
170             Xpp3Dom[] nodes = node.getChildren("overlay");
171             if (nodes == null)
172                 return Collections.emptyList();
173 
174             _overlayConfigs = new ArrayList<OverlayConfig>();
175             for (int i=0;i<nodes.length;i++)
176             {
177                 OverlayConfig overlayConfig = new OverlayConfig(nodes[i], _dependentMavenWarIncludes, _dependentMavenWarExcludes);
178                 _overlayConfigs.add(overlayConfig);
179             }
180         }
181 
182         return _overlayConfigs;
183     }
184     
185     
186     
187     
188     /**
189      * @return the xml as a string
190      */
191     public String getMavenWarOverlayConfigAsString ()
192     {
193         getPlugin();
194 
195         if (_plugin == null)
196             return "";
197         
198         Xpp3Dom node = (Xpp3Dom)_plugin.getConfiguration();
199         if (node == null)
200             return "";
201         return node.toString();
202     }
203 }