You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MavenMetadataReader.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package org.apache.archiva.maven2.metadata;
  2. /*
  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
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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
  18. * under the License.
  19. */
  20. import org.apache.archiva.model.ArchivaRepositoryMetadata;
  21. import org.apache.archiva.model.Plugin;
  22. import org.apache.archiva.model.SnapshotVersion;
  23. import org.apache.archiva.repository.storage.StorageAsset;
  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;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import java.io.IOException;
  31. import java.nio.file.Files;
  32. import java.nio.file.Path;
  33. import java.util.Date;
  34. /**
  35. * @author Olivier Lamy
  36. * @since 1.4-M3
  37. */
  38. public class MavenMetadataReader
  39. {
  40. /*
  41. <?xml version="1.0" encoding="UTF-8"?>
  42. <metadata modelVersion="1.1.0">
  43. <groupId>org.apache.archiva</groupId>
  44. <artifactId>archiva</artifactId>
  45. <version>1.4-M3-SNAPSHOT</version>
  46. <versioning>
  47. <snapshot>
  48. <timestamp>20120310.230917</timestamp>
  49. <buildNumber>2</buildNumber>
  50. </snapshot>
  51. <lastUpdated>20120310230917</lastUpdated>
  52. <snapshotVersions>
  53. <snapshotVersion>
  54. <extension>pom</extension>
  55. <value>1.4-M3-20120310.230917-2</value>
  56. <updated>20120310230917</updated>
  57. </snapshotVersion>
  58. </snapshotVersions>
  59. </versioning>
  60. </metadata>
  61. */
  62. private static final Logger log = LoggerFactory.getLogger( MavenMetadataReader.class );
  63. public static ArchivaRepositoryMetadata read(StorageAsset metadataFile) throws XMLException, IOException {
  64. if (metadataFile.isFileBased()) {
  65. return read(metadataFile.getFilePath());
  66. } else {
  67. throw new IOException("StorageAsset is not file based");
  68. }
  69. }
  70. /**
  71. * Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
  72. *
  73. * @param metadataFile the maven-metadata.xml file to read.
  74. * @return the archiva repository metadata object that represents the provided file contents.
  75. * @throws XMLException
  76. */
  77. public static ArchivaRepositoryMetadata read( Path metadataFile )
  78. throws XMLException, IOException {
  79. XMLReader xml = new XMLReader( "metadata", metadataFile );
  80. // invoke this to remove namespaces, see MRM-1136
  81. xml.removeNamespaces();
  82. ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
  83. metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
  84. metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
  85. metadata.setVersion( xml.getElementText( "//metadata/version" ) );
  86. Date modTime;
  87. modTime = new Date(Files.getLastModifiedTime(metadataFile).toMillis());
  88. metadata.setFileLastModified( modTime );
  89. metadata.setFileSize( Files.size(metadataFile) );
  90. metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
  91. metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
  92. metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
  93. metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
  94. Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
  95. if ( snapshotElem != null )
  96. {
  97. SnapshotVersion snapshot = new SnapshotVersion();
  98. snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) );
  99. String tmp = snapshotElem.elementTextTrim( "buildNumber" );
  100. if ( NumberUtils.isNumber( tmp ) )
  101. {
  102. snapshot.setBuildNumber( NumberUtils.toInt( tmp ) );
  103. }
  104. metadata.setSnapshotVersion( snapshot );
  105. }
  106. for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) )
  107. {
  108. Plugin p = new Plugin();
  109. p.setPrefix( plugin.elementTextTrim( "prefix" ) );
  110. p.setArtifactId( plugin.elementTextTrim( "artifactId" ) );
  111. p.setName( plugin.elementTextTrim( "name" ) );
  112. metadata.addPlugin( p );
  113. }
  114. return metadata;
  115. }
  116. }