View Javadoc

1   package org.apache.maven.plugins.scm.release;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import org.dom4j.Document;
22  import org.dom4j.io.SAXReader;
23  import org.jaxen.XPath;
24  import org.jaxen.dom4j.Dom4jXPath;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * This is a simple POM manipulator that doesn't take into account any sort of
32   * POM inheritance. When the interpolation and inheritance is cleaned up in the
33   * Maven we will be able to serialize a POM directly with greater ease. But we
34   * may stick with this Jaxen solution anyway because it's so easy. Just turn the
35   * POM in memory into a Dom4j document and perform the same operation as we are
36   * here.
37   */
38  public class PomNodeSelector
39  {
40      /**
41       * POM document
42       */
43      private File pom;
44  
45      /**
46       * Xpath expression.
47       */
48      private String xpathExpression;
49  
50      /**
51       * Snapshot dependencies found.
52       */
53      private List nodes;
54  
55      /**
56       * Constructor.
57       */
58      public PomNodeSelector()
59      {
60          nodes = new ArrayList();
61      }
62  
63      // -------------------------------------------------------------------------
64      // Accessors
65      // -------------------------------------------------------------------------
66  
67      public void setXpathExpression( String xpathExpression )
68      {
69          this.xpathExpression = xpathExpression;
70      }
71  
72      public String getXpathExpression()
73      {
74          return xpathExpression;
75      }
76  
77      /**
78       * Sets the nodes attribute of the PomManipulator object
79       */
80      public void setNodes( List nodes )
81      {
82          this.nodes = nodes;
83      }
84  
85      /**
86       * Gets the nodes attribute of the PomManipulator object
87       */
88      public List getNodes()
89      {
90          return nodes;
91      }
92  
93      public void setPom( File pom )
94      {
95          this.pom = pom;
96      }
97  
98      public File getPom()
99      {
100         return pom;
101     }
102 
103     /**
104      * Update the snapshot version identifiers with actual timestamp versions
105      * and write out the POM in its updated form.
106      *
107      * @throws Exception
108      */
109     public void execute()
110         throws Exception
111     {
112         SAXReader reader = new SAXReader();
113         Document doc = reader.read( getPom() );
114 
115         // The selecting nodes with the xpath expression will give us a list
116         // of dependencies elements where the version element is equal to 'SNAPSHOT'.
117         // So we can get any information we need, and alter anything we need to before writing
118         // the dom4j document back out.
119         XPath xpath = new Dom4jXPath( getXpathExpression() );
120         setNodes( xpath.selectNodes( doc ) );
121     }
122 }