View Javadoc

1   package org.apache.maven.release;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import org.apache.commons.io.FileUtils;
21  import org.apache.maven.MavenConstants;
22  import org.apache.maven.project.Project;
23  import org.apache.maven.util.HttpUtils;
24  import org.dom4j.Node;
25  
26  import java.io.File;
27  import java.util.Iterator;
28  
29  /**
30   *
31   *
32   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
33   *
34   * @version $Id: SnapshotResolver.java 170851 2005-05-19 01:18:10Z brett $
35   */
36  public class SnapshotResolver
37      extends AbstractPomTransformer
38  {
39      // -------------------------------------------------------------------------
40      // Accessors
41      // -------------------------------------------------------------------------
42  
43      public String selectNodesXPathExpression()
44      {
45          return "/project/dependencies/dependency[version='SNAPSHOT']";
46      }
47  
48      public String selectNodeXPath()
49      {
50          return "version";
51      }
52  
53      public String getNodeContent( Node node )
54          throws Exception
55      {
56          String timestampVersion = "SNAPSHOT";
57  
58          Node idNode = node.selectSingleNode( "id" );
59  
60          String groupId;
61          String artifactId;
62          String artifactType = "jar";
63  
64          if ( idNode != null )
65          {
66              groupId = idNode.getText();
67              artifactId = groupId;
68          }
69          else
70          {
71              Node artifactIdNode = node.selectSingleNode( "artifactId" );
72              Node groupIdNode = node.selectSingleNode( "groupId" );
73  
74              groupId = groupIdNode.getText();
75              artifactId = artifactIdNode.getText();
76          }
77  
78          Node typeIdNode = node.selectSingleNode( "type" );
79  
80          if ( typeIdNode != null )
81          {
82              artifactType = typeIdNode.getText();
83          }
84  
85          // Now we need to attempt to find the the actual timestamp
86          // version for this dependency.
87  
88          // Variables are being stored as ConstantExpressions ...
89  
90          Project project = (Project) getVariables().get( MavenConstants.MAVEN_POM );
91          File snapshotVersionFile = new File( getProject().getParentFile(), artifactId + "-snapshot-version" );
92  
93          for ( Iterator i = project.getContext().getMavenRepoRemote().iterator(); i.hasNext(); )
94          {
95              String remoteRepo = (String) i.next();
96              String url = remoteRepo + "/" + groupId + "/" + artifactType + "s/" + artifactId + "-snapshot-version";
97             
98              try
99              {
100                 HttpUtils.getFile( url,
101                                    snapshotVersionFile,
102                                    true, // ignore errors
103                                    false, // use timestamps
104                                    (String) getVariables().get( MavenConstants.PROXY_HOST ), // proxy host
105                                    (String) getVariables().get( MavenConstants.PROXY_PORT ), // proxy port
106                                    (String) getVariables().get( MavenConstants.PROXY_USERNAME ), // proxy user name
107                                    (String) getVariables().get( MavenConstants.PROXY_PASSWORD ) // proxy password
108                 );
109             }
110             catch ( Exception e )
111             {
112                 // Either doesn't exist, or we have a network problem. In
113                 // either event we can't update the version field.
114                 System.out.println( "Can't retrieve snapshot version file: " + e.getLocalizedMessage() );
115             }
116 
117             if ( snapshotVersionFile.exists() )
118             {
119                 timestampVersion = FileUtils.readFileToString( snapshotVersionFile, null );
120             }
121         }
122 
123         return timestampVersion;
124     }
125 
126     public void transformNode( Node node )
127         throws Exception
128     {
129         Node version = node.selectSingleNode( selectNodeXPath() );
130         version.setText( getNodeContent( node ) );
131     }
132 
133     public Node getTransformedNode( Node node )
134         throws Exception
135     {
136         Node transformedNode = (Node) node.clone();
137         Node version = transformedNode.selectSingleNode( selectNodeXPath() );
138         version.setText( getNodeContent( transformedNode ) );
139 
140         return transformedNode;
141     }
142 }