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