1 package org.apache.maven.plugins.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.LinkedHashSet;
30 import java.util.Set;
31 import java.util.TimeZone;
32
33 import org.apache.maven.artifact.Artifact;
34 import org.codehaus.plexus.util.ReaderFactory;
35 import org.codehaus.plexus.util.WriterFactory;
36 import org.codehaus.plexus.util.xml.Xpp3Dom;
37 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
38 import org.codehaus.plexus.util.xml.Xpp3DomUtils;
39 import org.codehaus.plexus.util.xml.Xpp3DomWriter;
40 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
41
42
43
44
45
46
47 class MetadataUtils
48 {
49
50
51
52
53
54
55
56
57
58
59 public static void createMetadata( File file, Artifact artifact )
60 throws IOException
61 {
62 TimeZone tz = java.util.TimeZone.getTimeZone( "UTC" );
63 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMddHHmmss" );
64 fmt.setTimeZone( tz );
65 String timestamp = fmt.format( new Date() );
66
67 if ( artifact.isSnapshot() )
68 {
69 File metadataFile = new File( file.getParentFile(), "maven-metadata-local.xml" );
70
71 Xpp3Dom metadata = new Xpp3Dom( "metadata" );
72 addChild( metadata, "groupId", artifact.getGroupId() );
73 addChild( metadata, "artifactId", artifact.getArtifactId() );
74 addChild( metadata, "version", artifact.getBaseVersion() );
75 Xpp3Dom versioning = new Xpp3Dom( "versioning" );
76 versioning.addChild( addChild( new Xpp3Dom( "snapshot" ), "localCopy", "true" ) );
77 addChild( versioning, "lastUpdated", timestamp );
78 metadata.addChild( versioning );
79
80 writeMetadata( metadataFile, metadata );
81 }
82
83 File metadataFile = new File( file.getParentFile().getParentFile(), "maven-metadata-local.xml" );
84
85 Set<String> allVersions = new LinkedHashSet<String>();
86
87 Xpp3Dom metadata = readMetadata( metadataFile );
88
89 if ( metadata != null )
90 {
91 Xpp3Dom versioning = metadata.getChild( "versioning" );
92 if ( versioning != null )
93 {
94 Xpp3Dom versions = versioning.getChild( "versions" );
95 if ( versions != null )
96 {
97
98 Xpp3Dom[] children = versions.getChildren( "version" );
99 for ( Xpp3Dom aChildren : children )
100 {
101 allVersions.add( aChildren.getValue() );
102 }
103 }
104 }
105 }
106
107 allVersions.add( artifact.getBaseVersion() );
108
109 metadata = new Xpp3Dom( "metadata" );
110 addChild( metadata, "groupId", artifact.getGroupId() );
111 addChild( metadata, "artifactId", artifact.getArtifactId() );
112 Xpp3Dom versioning = new Xpp3Dom( "versioning" );
113 versioning.addChild( addChildren( new Xpp3Dom( "versions" ), "version", allVersions ) );
114 addChild( versioning, "lastUpdated", timestamp );
115 metadata.addChild( versioning );
116
117 metadata = Xpp3DomUtils.mergeXpp3Dom( metadata, readMetadata( metadataFile ) );
118
119 writeMetadata( metadataFile, metadata );
120 }
121
122 private static Xpp3Dom addChild( Xpp3Dom parent, String childName, String childValue )
123 {
124 Xpp3Dom child = new Xpp3Dom( childName );
125 child.setValue( childValue );
126 parent.addChild( child );
127 return parent;
128 }
129
130 private static Xpp3Dom addChildren( Xpp3Dom parent, String childName, Collection<String> childValues )
131 {
132 for ( String childValue : childValues )
133 {
134 addChild( parent, childName, childValue );
135 }
136 return parent;
137 }
138
139 private static Xpp3Dom readMetadata( File metadataFile )
140 throws IOException
141 {
142 if ( !metadataFile.isFile() )
143 {
144 return null;
145 }
146
147 try ( Reader reader = ReaderFactory.newXmlReader( metadataFile ) )
148 {
149 return Xpp3DomBuilder.build( reader );
150 }
151 catch ( XmlPullParserException e )
152 {
153 throw (IOException) new IOException( e.getMessage() ).initCause( e );
154 }
155 }
156
157 private static void writeMetadata( File metadataFile, Xpp3Dom metadata )
158 throws IOException
159 {
160 metadataFile.getParentFile().mkdirs();
161
162 try ( Writer writer = WriterFactory.newXmlWriter( metadataFile ) )
163 {
164 Xpp3DomWriter.write( writer, metadata );
165 }
166 }
167
168 }