1 package org.apache.archiva.maven2.metadata;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.model.ArchivaRepositoryMetadata;
22 import org.apache.archiva.model.Plugin;
23 import org.apache.archiva.model.SnapshotVersion;
24 import org.apache.archiva.xml.XMLException;
25 import org.apache.archiva.xml.XMLReader;
26 import org.apache.commons.lang.math.NumberUtils;
27 import org.dom4j.Element;
30 import java.util.Date;
33 * @author Olivier Lamy
36 public class MavenMetadataReader
39 <?xml version="1.0" encoding="UTF-8"?>
40 <metadata modelVersion="1.1.0">
41 <groupId>org.apache.archiva</groupId>
42 <artifactId>archiva</artifactId>
43 <version>1.4-M3-SNAPSHOT</version>
46 <timestamp>20120310.230917</timestamp>
47 <buildNumber>2</buildNumber>
49 <lastUpdated>20120310230917</lastUpdated>
52 <extension>pom</extension>
53 <value>1.4-M3-20120310.230917-2</value>
54 <updated>20120310230917</updated>
62 * Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
64 * @param metadataFile the maven-metadata.xml file to read.
65 * @return the archiva repository metadata object that represents the provided file contents.
66 * @throws XMLException
68 public static ArchivaRepositoryMetadata read( File metadataFile )
72 XMLReader xml = new XMLReader( "metadata", metadataFile );
73 // invoke this to remove namespaces, see MRM-1136
74 xml.removeNamespaces();
76 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
78 metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
79 metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
80 metadata.setVersion( xml.getElementText( "//metadata/version" ) );
81 metadata.setFileLastModified( new Date( metadataFile.lastModified() ) );
82 metadata.setFileSize( metadataFile.length() );
84 metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
85 metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
86 metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
87 metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
89 Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
90 if ( snapshotElem != null )
92 SnapshotVersion snapshot = new SnapshotVersion();
93 snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) );
94 String tmp = snapshotElem.elementTextTrim( "buildNumber" );
95 if ( NumberUtils.isNumber( tmp ) )
97 snapshot.setBuildNumber( NumberUtils.toInt( tmp ) );
99 metadata.setSnapshotVersion( snapshot );
102 for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) )
104 Plugin p = new Plugin();
105 p.setPrefix( plugin.elementTextTrim( "prefix" ) );
106 p.setArtifactId( plugin.elementTextTrim( "artifactId" ) );
107 p.setName( plugin.elementTextTrim( "name" ) );
108 metadata.addPlugin( p );