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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 {
  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. try {
  88. modTime = new Date(Files.getLastModifiedTime(metadataFile).toMillis());
  89. } catch (IOException e) {
  90. modTime = new Date();
  91. log.error("Could not read modification time of {}", metadataFile);
  92. }
  93. metadata.setFileLastModified( modTime );
  94. try {
  95. metadata.setFileSize(Files.size(metadataFile));
  96. } catch (IOException e) {
  97. metadata.setFileSize( 0 );
  98. log.error("Could not read file size of {}", metadataFile);
  99. }
  100. metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
  101. metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
  102. metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
  103. metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
  104. Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
  105. if ( snapshotElem != null )
  106. {
  107. SnapshotVersion snapshot = new SnapshotVersion();
  108. snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) );
  109. String tmp = snapshotElem.elementTextTrim( "buildNumber" );
  110. if ( NumberUtils.isNumber( tmp ) )
  111. {
  112. snapshot.setBuildNumber( NumberUtils.toInt( tmp ) );
  113. }
  114. metadata.setSnapshotVersion( snapshot );
  115. }
  116. for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) )
  117. {
  118. Plugin p = new Plugin();
  119. p.setPrefix( plugin.elementTextTrim( "prefix" ) );
  120. p.setArtifactId( plugin.elementTextTrim( "artifactId" ) );
  121. p.setName( plugin.elementTextTrim( "name" ) );
  122. metadata.addPlugin( p );
  123. }
  124. return metadata;
  125. }
  126. }