Beispiel #1
0
  /**
   * Generates the maven-metadata.xml file dynamically for a given groupId/artifactId pair. This
   * will list all of the versions available for that groupId+artifactId, along with the latest
   * release and snapshot versions.
   *
   * @param gavInfo
   * @param inputData
   * @throws ResourceDoesNotExistException
   */
  private void doGenerateArtifactDirMavenMetaData(MavenGavInfo gavInfo, InputData inputData)
      throws ResourceDoesNotExistException {
    // See the comment in {@link SrampWagon#fillInputData(InputData)} about why we're doing this
    // context classloader magic.
    ClassLoader oldCtxCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(SrampWagon.class.getClassLoader());
    try {
      String artyPath = gavInfo.getFullName();
      if (gavInfo.isHash()) {
        artyPath = artyPath.substring(0, artyPath.lastIndexOf('.'));
      }
      SrampArchiveEntry entry = this.archive.getEntry(artyPath);
      if (entry == null) {
        QueryResultSet resultSet =
            client
                .buildQuery("/s-ramp[@maven.groupId = ? and @maven.artifactId = ?]") // $NON-NLS-1$
                .parameter(gavInfo.getGroupId())
                .parameter(gavInfo.getArtifactId())
                .propertyName("maven.version") // $NON-NLS-1$
                .count(500)
                .orderBy("createdTimestamp")
                .ascending()
                .query(); //$NON-NLS-1$
        if (resultSet.size() == 0) {
          throw new Exception(Messages.i18n.format("NO_ARTIFACTS_FOUND")); // $NON-NLS-1$
        }

        String groupId = gavInfo.getGroupId();
        String artifactId = gavInfo.getArtifactId();
        String latest = null;
        String release = null;
        String lastUpdated = null;

        LinkedHashSet<String> versions = new LinkedHashSet<String>();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); // $NON-NLS-1$
        for (ArtifactSummary artifactSummary : resultSet) {
          String version = artifactSummary.getCustomPropertyValue("maven.version"); // $NON-NLS-1$
          if (versions.add(version)) {
            latest = version;
            if (!version.endsWith("-SNAPSHOT")) { // $NON-NLS-1$
              release = version;
            }
          }
          lastUpdated = format.format(artifactSummary.getCreatedTimestamp());
        }

        StringBuilder mavenMetadata = new StringBuilder();
        mavenMetadata.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // $NON-NLS-1$
        mavenMetadata.append("<metadata>\n"); // $NON-NLS-1$
        mavenMetadata
            .append("  <groupId>")
            .append(groupId)
            .append("</groupId>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata
            .append("  <artifactId>")
            .append(artifactId)
            .append("</artifactId>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata.append("  <versioning>\n"); // $NON-NLS-1$
        mavenMetadata
            .append("    <latest>")
            .append(latest)
            .append("</latest>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata
            .append("    <release>")
            .append(release)
            .append("</release>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata.append("    <versions>\n"); // $NON-NLS-1$
        for (String version : versions) {
          mavenMetadata
              .append("      <version>")
              .append(version)
              .append("</version>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        }
        mavenMetadata.append("    </versions>\n"); // $NON-NLS-1$
        mavenMetadata
            .append("    <lastUpdated>")
            .append(lastUpdated)
            .append("</lastUpdated>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata.append("  </versioning>\n"); // $NON-NLS-1$
        mavenMetadata.append("</metadata>\n"); // $NON-NLS-1$

        BaseArtifactType artifact =
            ArtifactType.ExtendedDocument("MavenMetaData").newArtifactInstance(); // $NON-NLS-1$
        this.archive.addEntry(artyPath, artifact, IOUtils.toInputStream(mavenMetadata.toString()));

        entry = this.archive.getEntry(artyPath);
      }

      if (!gavInfo.isHash()) {
        inputData.setInputStream(this.archive.getInputStream(entry));
      } else {
        String hash = generateHash(this.archive.getInputStream(entry), gavInfo.getHashAlgorithm());
        inputData.setInputStream(IOUtils.toInputStream(hash));
      }
    } catch (Exception e) {
      throw new ResourceDoesNotExistException(
          Messages.i18n.format("FAILED_TO_GENERATE_METADATA"), e); // $NON-NLS-1$
    } finally {
      Thread.currentThread().setContextClassLoader(oldCtxCL);
    }
  }