Example usage for org.apache.maven.artifact Artifact setBaseVersion

List of usage examples for org.apache.maven.artifact Artifact setBaseVersion

Introduction

In this page you can find the example usage for org.apache.maven.artifact Artifact setBaseVersion.

Prototype

void setBaseVersion(String baseVersion);

Source Link

Usage

From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java

License:Apache License

@SuppressWarnings("unchecked")
private boolean copyPom(Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction)
        throws ArtifactConversionException {
    Artifact pom = artifactFactory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(),
            artifact.getVersion());/*from  w  w w  . j a v  a  2  s. c o m*/
    pom.setBaseVersion(artifact.getBaseVersion());
    ArtifactRepository repository = artifact.getRepository();
    File file = new File(repository.getBasedir(), repository.pathOf(pom));

    boolean result = true;
    if (file.exists()) {
        File targetFile = new File(targetRepository.getBasedir(), targetRepository.pathOf(pom));

        String contents = null;
        boolean checksumsValid = false;
        try {
            if (testChecksums(artifact, file)) {
                checksumsValid = true;
            }

            // Even if the checksums for the POM are invalid we should still convert the POM
            contents = FileUtils.readFileToString(file, Charset.defaultCharset());
        } catch (IOException e) {
            throw new ArtifactConversionException(
                    Messages.getString("unable.to.read.source.pom", e.getMessage()), e); //$NON-NLS-1$
        }

        if (checksumsValid && contents.indexOf("modelVersion") >= 0) //$NON-NLS-1$
        {
            // v4 POM
            try {
                boolean matching = false;
                if (!force && targetFile.exists()) {
                    String targetContents = FileUtils.readFileToString(targetFile, Charset.defaultCharset());
                    matching = targetContents.equals(contents);
                }
                if (force || !matching) {
                    transaction.createFile(contents, targetFile, digesters);
                }
            } catch (IOException e) {
                throw new ArtifactConversionException(
                        Messages.getString("unable.to.write.target.pom", e.getMessage()), e); //$NON-NLS-1$
            }
        } else {
            // v3 POM
            try (StringReader stringReader = new StringReader(contents)) {

                try (StringWriter writer = new StringWriter()) {
                    org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
                    org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read(stringReader);

                    if (doRelocation(artifact, v3Model, targetRepository, transaction)) {
                        Artifact relocatedPom = artifactFactory.createProjectArtifact(artifact.getGroupId(),
                                artifact.getArtifactId(), artifact.getVersion());
                        targetFile = new File(targetRepository.getBasedir(),
                                targetRepository.pathOf(relocatedPom));
                    }

                    Model v4Model = translator.translate(v3Model);

                    translator.validateV4Basics(v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
                            v3Model.getVersion(), v3Model.getPackage());

                    MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
                    xpp3Writer.write(writer, v4Model);

                    transaction.createFile(writer.toString(), targetFile, digesters);

                    List<String> warnings = translator.getWarnings();

                    for (String message : warnings) {
                        addWarning(artifact, message);
                    }
                } catch (XmlPullParserException e) {
                    addWarning(artifact, Messages.getString("invalid.source.pom", e.getMessage())); //$NON-NLS-1$
                    result = false;
                } catch (IOException e) {
                    throw new ArtifactConversionException(Messages.getString("unable.to.write.converted.pom"),
                            e); //$NON-NLS-1$
                } catch (PomTranslationException e) {
                    addWarning(artifact, Messages.getString("invalid.source.pom", e.getMessage())); //$NON-NLS-1$
                    result = false;
                }
            }
        }
    } else {
        addWarning(artifact, Messages.getString("warning.missing.pom")); //$NON-NLS-1$
    }
    return result;
}

From source file:org.sonatype.flexmojos.utilities.MavenUtils.java

License:Apache License

/**
 * Get the Release version for the Artifact.
 * // w  w  w .jav  a 2 s .co  m
 * @param artifact The artifact to update with the release version.
 * @param localRepository artifact repository
 * @param remoteRepositories List of remote repositories
 * @param artifactMetadataSource artifactMetadataSource
 * @throws MojoExecutionException thrown if an exception occured during artifact resolving
 */
@SuppressWarnings("unchecked")
private static void getReleaseVersion(Artifact artifact, ArtifactRepository localRepository,
        List remoteRepositories, ArtifactMetadataSource artifactMetadataSource) throws MojoExecutionException {
    try {
        List<ArtifactVersion> artifactVersions = artifactMetadataSource.retrieveAvailableVersions(artifact,
                localRepository, remoteRepositories);
        if (artifactVersions != null && !artifactVersions.isEmpty()) {
            ArtifactVersion release = artifactVersions.get(artifactVersions.size() - 1);
            artifact.setBaseVersion(release.toString());
            artifact.setVersion(release.toString());
        }
    } catch (ArtifactMetadataRetrievalException ex) {
        throw new MojoExecutionException("Error retrieving release version for artifact: " + artifact.getId(),
                ex);
    }
}