1 package org.apache.maven.plugins.scm.release;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
87
88
89
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,
104 false,
105 (String) getVariables().get( MavenConstants.PROXY_HOST ),
106 (String) getVariables().get( MavenConstants.PROXY_PORT ),
107 (String) getVariables().get( MavenConstants.PROXY_USERNAME ),
108 (String) getVariables().get( MavenConstants.PROXY_PASSWORD )
109 );
110 }
111 catch ( Exception e )
112 {
113
114
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 }