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.apache.commons.io.FileUtils;
22  import org.apache.maven.MavenConstants;
23  import org.apache.maven.project.Project;
24  import org.apache.maven.util.HttpUtils;
25  import org.dom4j.Node;
26  
27  import java.io.File;
28  import java.util.Iterator;
29  
30  /**
31   *
32   *
33   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
34   *
35   * @version $Id: SnapshotResolver.java 521198 2007-03-22 10:39:43Z ltheussl $
36   */
37  public class SnapshotResolver
38      extends AbstractPomTransformer
39  {
40      // -------------------------------------------------------------------------
41      // Accessors
42      // -------------------------------------------------------------------------
43  
44      public String selectNodesXPathExpression()
45      {
46          return "/project/dependencies/dependency[version='SNAPSHOT']";
47      }
48  
49      public String selectNodeXPath()
50      {
51          return "version";
52      }
53  
54      public String getNodeContent( Node node )
55          throws Exception
56      {
57          String timestampVersion = "SNAPSHOT";
58  
59          Node idNode = node.selectSingleNode( "id" );
60  
61          String groupId;
62          String artifactId;
63          String artifactType = "jar";
64  
65          if ( idNode != null )
66          {
67              groupId = idNode.getText();
68              artifactId = groupId;
69          }
70          else
71          {
72              Node artifactIdNode = node.selectSingleNode( "artifactId" );
73              Node groupIdNode = node.selectSingleNode( "groupId" );
74  
75              groupId = groupIdNode.getText();
76              artifactId = artifactIdNode.getText();
77          }
78  
79          Node typeIdNode = node.selectSingleNode( "type" );
80  
81          if ( typeIdNode != null )
82          {
83              artifactType = typeIdNode.getText();
84          }
85  
86          // Now we need to attempt to find the the actual timestamp
87          // version for this dependency.
88  
89          // Variables are being stored as ConstantExpressions ...
90  
91          Project project = (Project) getVariables().get( MavenConstants.MAVEN_POM );
92          File snapshotVersionFile = new File( getProject().getParentFile(), artifactId + "-snapshot-version" );
93  
94          for ( Iterator i = project.getContext().getMavenRepoRemote().iterator(); i.hasNext(); )
95          {
96              String remoteRepo = (String) i.next();
97              String url = remoteRepo + "/" + groupId + "/" + artifactType + "s/" + artifactId + "-snapshot-version";
98             
99              try
100             {
101                 HttpUtils.getFile( url,
102                                    snapshotVersionFile,
103                                    true, // ignore errors
104                                    false, // use timestamps
105                                    (String) getVariables().get( MavenConstants.PROXY_HOST ), // proxy host
106                                    (String) getVariables().get( MavenConstants.PROXY_PORT ), // proxy port
107                                    (String) getVariables().get( MavenConstants.PROXY_USERNAME ), // proxy user name
108                                    (String) getVariables().get( MavenConstants.PROXY_PASSWORD ) // proxy password
109                 );
110             }
111             catch ( Exception e )
112             {
113                 // Either doesn't exist, or we have a network problem. In
114                 // either event we can't update the version field.
115                 System.out.println( "Can't retrieve snapshot version file: " + e.getLocalizedMessage() );
116             }
117 
118             if ( snapshotVersionFile.exists() )
119             {
120                 timestampVersion = FileUtils.readFileToString( snapshotVersionFile, null );
121             }
122         }
123 
124         return timestampVersion;
125     }
126 
127     public void transformNode( Node node )
128         throws Exception
129     {
130         Node version = node.selectSingleNode( selectNodeXPath() );
131         version.setText( getNodeContent( node ) );
132     }
133 
134     public Node getTransformedNode( Node node )
135         throws Exception
136     {
137         Node transformedNode = (Node) node.clone();
138         Node version = transformedNode.selectSingleNode( selectNodeXPath() );
139         version.setText( getNodeContent( transformedNode ) );
140 
141         return transformedNode;
142     }
143 }