1 | |
package org.apache.maven.plugin.invoker; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.io.Reader; |
25 | |
import java.io.Writer; |
26 | |
import java.text.SimpleDateFormat; |
27 | |
import java.util.Collection; |
28 | |
import java.util.Date; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.LinkedHashSet; |
31 | |
import java.util.Set; |
32 | |
import java.util.TimeZone; |
33 | |
|
34 | |
import org.apache.maven.artifact.Artifact; |
35 | |
import org.codehaus.plexus.util.IOUtil; |
36 | |
import org.codehaus.plexus.util.ReaderFactory; |
37 | |
import org.codehaus.plexus.util.WriterFactory; |
38 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
39 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
40 | |
import org.codehaus.plexus.util.xml.Xpp3DomUtils; |
41 | |
import org.codehaus.plexus.util.xml.Xpp3DomWriter; |
42 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | class MetadataUtils |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public static void createMetadata( File file, Artifact artifact ) |
62 | |
throws IOException |
63 | |
{ |
64 | 0 | TimeZone tz = java.util.TimeZone.getTimeZone( "UTC" ); |
65 | 0 | SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMddHHmmss" ); |
66 | 0 | fmt.setTimeZone( tz ); |
67 | 0 | String timestamp = fmt.format( new Date() ); |
68 | |
|
69 | 0 | if ( artifact.isSnapshot() ) |
70 | |
{ |
71 | 0 | File metadataFile = new File( file.getParentFile(), "maven-metadata-local.xml" ); |
72 | |
|
73 | 0 | Xpp3Dom metadata = new Xpp3Dom( "metadata" ); |
74 | 0 | addChild( metadata, "groupId", artifact.getGroupId() ); |
75 | 0 | addChild( metadata, "artifactId", artifact.getArtifactId() ); |
76 | 0 | addChild( metadata, "version", artifact.getBaseVersion() ); |
77 | 0 | Xpp3Dom versioning = new Xpp3Dom( "versioning" ); |
78 | 0 | versioning.addChild( addChild( new Xpp3Dom( "snapshot" ), "localCopy", "true" ) ); |
79 | 0 | addChild( versioning, "lastUpdated", timestamp ); |
80 | 0 | metadata.addChild( versioning ); |
81 | |
|
82 | 0 | writeMetadata( metadataFile, metadata ); |
83 | |
} |
84 | |
|
85 | |
{ |
86 | 0 | File metadataFile = new File( file.getParentFile().getParentFile(), "maven-metadata-local.xml" ); |
87 | |
|
88 | 0 | Set allVersions = new LinkedHashSet(); |
89 | |
|
90 | 0 | Xpp3Dom metadata = readMetadata( metadataFile ); |
91 | |
|
92 | 0 | if ( metadata != null ) |
93 | |
{ |
94 | 0 | Xpp3Dom versioning = metadata.getChild( "versioning" ); |
95 | 0 | if ( versioning != null ) |
96 | |
{ |
97 | 0 | Xpp3Dom versions = versioning.getChild( "versions" ); |
98 | 0 | if ( versions != null ) |
99 | |
{ |
100 | |
|
101 | 0 | Xpp3Dom[] children = versions.getChildren( "version" ); |
102 | 0 | for ( int i = 0; i < children.length; i++ ) |
103 | |
{ |
104 | 0 | allVersions.add( children[i].getValue() ); |
105 | |
} |
106 | |
} |
107 | |
} |
108 | |
} |
109 | |
|
110 | 0 | allVersions.add( artifact.getBaseVersion() ); |
111 | |
|
112 | 0 | metadata = new Xpp3Dom( "metadata" ); |
113 | 0 | addChild( metadata, "groupId", artifact.getGroupId() ); |
114 | 0 | addChild( metadata, "artifactId", artifact.getArtifactId() ); |
115 | 0 | Xpp3Dom versioning = new Xpp3Dom( "versioning" ); |
116 | 0 | versioning.addChild( addChildren( new Xpp3Dom( "versions" ), "version", allVersions ) ); |
117 | 0 | addChild( versioning, "lastUpdated", timestamp ); |
118 | 0 | metadata.addChild( versioning ); |
119 | |
|
120 | 0 | metadata = Xpp3DomUtils.mergeXpp3Dom( metadata, readMetadata( metadataFile ) ); |
121 | |
|
122 | 0 | writeMetadata( metadataFile, metadata ); |
123 | |
} |
124 | 0 | } |
125 | |
|
126 | |
private static Xpp3Dom addChild( Xpp3Dom parent, String childName, String childValue ) |
127 | |
{ |
128 | 0 | Xpp3Dom child = new Xpp3Dom( childName ); |
129 | 0 | child.setValue( childValue ); |
130 | 0 | parent.addChild( child ); |
131 | 0 | return parent; |
132 | |
} |
133 | |
|
134 | |
private static Xpp3Dom addChildren( Xpp3Dom parent, String childName, Collection childValues ) |
135 | |
{ |
136 | 0 | for ( Iterator it = childValues.iterator(); it.hasNext(); ) |
137 | |
{ |
138 | 0 | String childValue = (String) it.next(); |
139 | 0 | addChild( parent, childName, childValue ); |
140 | |
} |
141 | 0 | return parent; |
142 | |
} |
143 | |
|
144 | |
private static Xpp3Dom readMetadata( File metadataFile ) |
145 | |
throws IOException |
146 | |
{ |
147 | 0 | if ( !metadataFile.isFile() ) |
148 | |
{ |
149 | 0 | return null; |
150 | |
} |
151 | |
|
152 | 0 | Reader reader = ReaderFactory.newXmlReader( metadataFile ); |
153 | |
try |
154 | |
{ |
155 | |
try |
156 | |
{ |
157 | 0 | return Xpp3DomBuilder.build( reader ); |
158 | |
} |
159 | 0 | catch ( XmlPullParserException e ) |
160 | |
{ |
161 | 0 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
162 | |
} |
163 | |
} |
164 | |
finally |
165 | |
{ |
166 | 0 | IOUtil.close( reader ); |
167 | |
} |
168 | |
} |
169 | |
|
170 | |
private static void writeMetadata( File metadataFile, Xpp3Dom metadata ) |
171 | |
throws IOException |
172 | |
{ |
173 | 0 | metadataFile.getParentFile().mkdirs(); |
174 | |
|
175 | 0 | Writer writer = WriterFactory.newXmlWriter( metadataFile ); |
176 | |
try |
177 | |
{ |
178 | 0 | Xpp3DomWriter.write( writer, metadata ); |
179 | |
} |
180 | |
finally |
181 | |
{ |
182 | 0 | IOUtil.close( writer ); |
183 | 0 | } |
184 | 0 | } |
185 | |
|
186 | |
} |