Java Code Examples for org.apache.maven.artifact.versioning.ArtifactVersion#toString()

The following examples show how to use org.apache.maven.artifact.versioning.ArtifactVersion#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VersionComparators.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
static ArtifactVersion copySnapshot( ArtifactVersion source, ArtifactVersion destination )
{
    if ( isSnapshot( destination ) )
    {
        destination = stripSnapshot( destination );
    }
    Pattern matchSnapshotRegex = SNAPSHOT_PATTERN;
    final Matcher matcher = matchSnapshotRegex.matcher( source.toString() );
    if ( matcher.find() )
    {
        return new DefaultArtifactVersion( destination.toString() + "-" + matcher.group( 0 ) );
    }
    else
    {
        return new DefaultArtifactVersion( destination.toString() + "-SNAPSHOT" );
    }
}
 
Example 2
Source File: CodeTemplates.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private static String getCurrentVersion(String group, String artifact, String defaultVersion) {
  ArtifactVersion version = ArtifactRetriever.DEFAULT.getBestVersion(group, artifact);
  if (version == null) {
    return defaultVersion;
  }
  return version.toString();
}
 
Example 3
Source File: LibraryFile.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
/**
 * Check Maven Central to find the latest release version of this artifact.
 * This check is made at most once. Subsequent checks are no-ops.
 */
void updateVersion() {
  if (!fixedVersion && !isPinned()) {
    ArtifactVersion remoteVersion = ArtifactRetriever.DEFAULT.getBestVersion(
        mavenCoordinates.getGroupId(), mavenCoordinates.getArtifactId());
    if (remoteVersion != null) {
      DefaultArtifactVersion localVersion = new DefaultArtifactVersion(mavenCoordinates.getVersion());
      if (remoteVersion.compareTo(localVersion) > 0) {
        String updatedVersion = remoteVersion.toString(); 
        mavenCoordinates = mavenCoordinates.toBuilder().setVersion(updatedVersion).build();
      }
    }
    fixedVersion = true;
  }
}
 
Example 4
Source File: Pom.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private void handleDependencyManaged(LibraryFile artifact, Element dependency) {
  MavenCoordinates coordinates = artifact.getMavenCoordinates();
  String groupId = coordinates.getGroupId();
  String artifactId = coordinates.getArtifactId();
  Node versionNode = findChildByName(dependency, "version");
  if (!dependencyManaged(groupId, artifactId)) {
    if (versionNode == null) {
      String version = coordinates.getVersion();
      if (!artifact.isPinned()) {
        ArtifactVersion latestVersion =
            ArtifactRetriever.DEFAULT.getBestVersion(groupId, artifactId);
        if (latestVersion != null) {
          version = latestVersion.toString();
        }
      }
      // todo latest version may not be needed anymore.
      if (!MavenCoordinates.LATEST_VERSION.equals(version)) {
        Element versionElement =
            document.createElementNS("http://maven.apache.org/POM/4.0.0", "version");
        versionElement.setTextContent(version);
        dependency.appendChild(versionElement);
      }
    }
  } else {
    if (versionNode != null) {
      dependency.removeChild(versionNode);
    }
  }
}
 
Example 5
Source File: NumericVersionComparator.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected int innerGetSegmentCount( ArtifactVersion v )
{
    final String version = v.toString();
    StringTokenizer tok = new StringTokenizer( version, "." );
    return tok.countTokens();
}
 
Example 6
Source File: VersionComparators.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
static ArtifactVersion stripSnapshot( ArtifactVersion v )
{
    final String version = v.toString();
    final Matcher matcher = SNAPSHOT_PATTERN.matcher( version );
    if ( matcher.find() )
    {
        return new DefaultArtifactVersion( version.substring( 0, matcher.start( 1 ) - 1 ) );
    }
    return v;
}
 
Example 7
Source File: LibraryFactory.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
private static MavenCoordinates getMavenCoordinates(IConfigurationElement[] children) {
  if (children.length != 1) {
    logger.warning(
        "Single configuration element for MavenCoordinates was expected, found: " //$NON-NLS-1$
        + children.length);
  }
  IConfigurationElement mavenCoordinatesElement = children[0];
  String groupId = mavenCoordinatesElement.getAttribute(ATTRIBUTE_NAME_GROUP_ID);
  String artifactId = mavenCoordinatesElement.getAttribute(ATTRIBUTE_NAME_ARTIFACT_ID);

  MavenCoordinates.Builder builder = new MavenCoordinates.Builder()
      .setGroupId(groupId)
      .setArtifactId(artifactId);

  String repository = mavenCoordinatesElement.getAttribute(ATTRIBUTE_NAME_REPOSITORY_URI);
  if (!Strings.isNullOrEmpty(repository)) {
    builder.setRepository(repository);
  }

  // Only look up latest version if version isn't specified in file.
  String version = mavenCoordinatesElement.getAttribute(ATTRIBUTE_NAME_VERSION);
  if (Strings.isNullOrEmpty(version) || "LATEST".equals(version)) {
    ArtifactVersion artifactVersion =
        ArtifactRetriever.DEFAULT.getBestVersion(groupId, artifactId);
    if (artifactVersion != null) {
      version = artifactVersion.toString();
    }
  }

  if (!Strings.isNullOrEmpty(version)) {
    builder.setVersion(version);
  }
  String type = mavenCoordinatesElement.getAttribute(ATTRIBUTE_NAME_TYPE);
  if (!Strings.isNullOrEmpty(type)) {
    builder.setType(type);
  }
  String classifier = mavenCoordinatesElement.getAttribute(ATTRIBUTE_NAME_CLASSIFIER);
  if (!Strings.isNullOrEmpty(classifier)) {
    builder.setClassifier(classifier);
  }
  return builder.build();
}
 
Example 8
Source File: Pom.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
private String getBestVersion(String groupId, String artifactId) {
  ArtifactVersion latestVersion = ArtifactRetriever.DEFAULT.getBestVersion(groupId, artifactId);
  return latestVersion != null ? latestVersion.toString() : MavenCoordinates.LATEST_VERSION;
}
 
Example 9
Source File: DisplayDependencyUpdatesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void logUpdates( Map<Dependency, ArtifactVersions> updates, String section )
{
    List<String> withUpdates = new ArrayList<>();
    List<String> usingCurrent = new ArrayList<>();
    Iterator i = updates.values().iterator();
    while ( i.hasNext() )
    {
        ArtifactVersions versions = (ArtifactVersions) i.next();
        String left = "  " + ArtifactUtils.versionlessKey( versions.getArtifact() ) + " ";
        final String current;
        ArtifactVersion latest;
        if ( versions.isCurrentVersionDefined() )
        {
            current = versions.getCurrentVersion().toString();
            latest = versions.getNewestUpdate( calculateUpdateScope(), allowSnapshots );
        }
        else
        {
            ArtifactVersion newestVersion =
                versions.getNewestVersion( versions.getArtifact().getVersionRange(), allowSnapshots );
            current = versions.getArtifact().getVersionRange().toString();
            latest = newestVersion == null ? null
                            : versions.getNewestUpdate( newestVersion, calculateUpdateScope(), allowSnapshots );
            if ( latest != null
                && ArtifactVersions.isVersionInRange( latest, versions.getArtifact().getVersionRange() ) )
            {
                latest = null;
            }
        }
        String right = " " + ( latest == null ? current : current + " -> " + latest.toString() );
        List<String> t = latest == null ? usingCurrent : withUpdates;
        if ( right.length() + left.length() + 3 > INFO_PAD_SIZE )
        {
            t.add( left + "..." );
            t.add( StringUtils.leftPad( right, INFO_PAD_SIZE ) );

        }
        else
        {
            t.add( StringUtils.rightPad( left, INFO_PAD_SIZE - right.length(), "." ) + right );
        }
    }

    if ( isVerbose() )
    {
        if ( usingCurrent.isEmpty() )
        {
            if ( !withUpdates.isEmpty() )
            {
                logLine( false, "No dependencies in " + section + " are using the newest version." );
                logLine( false, "" );
            }
        }
        else
        {
            logLine( false, "The following dependencies in " + section + " are using the newest version:" );
            i = usingCurrent.iterator();
            while ( i.hasNext() )
            {
                logLine( false, (String) i.next() );
            }
            logLine( false, "" );
        }
    }        
    
    
    if ( withUpdates.isEmpty() )
    {
        if ( !usingCurrent.isEmpty() )
        {
            logLine( false, "No dependencies in " + section + " have newer versions." );
            logLine( false, "" );
        }
    }
    else
    {
        logLine( false, "The following dependencies in " + section + " have newer versions:" );
        i = withUpdates.iterator();
        while ( i.hasNext() )
        {
            logLine( false, (String) i.next() );
        }
        logLine( false, "" );
    }
}
 
Example 10
Source File: ResolveRangesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void resolveRangesInParent( ModifiedPomXMLEventReader pom )
    throws MojoExecutionException, ArtifactMetadataRetrievalException, XMLStreamException
{
    Matcher versionMatcher = matchRangeRegex.matcher( getProject().getModel().getParent().getVersion() );

    if ( versionMatcher.find() )
    {
        Artifact artifact = this.toArtifact( getProject().getModel().getParent() );

        if ( artifact != null && isIncluded( artifact ) )
        {
            getLog().debug( "Resolving version range for parent: " + artifact );

            String artifactVersion = artifact.getVersion();
            if ( artifactVersion == null )
            {
                ArtifactVersion latestVersion =
                    findLatestVersion( artifact, artifact.getVersionRange(), allowSnapshots, false );

                if ( latestVersion != null )
                {
                    artifactVersion = latestVersion.toString();
                }
                else
                {
                    getLog().warn( "Not updating version " + artifact + " : could not resolve any versions" );
                }
            }

            if ( artifactVersion != null )
            {
                if ( PomHelper.setProjectParentVersion( pom, artifactVersion ) )
                {
                    getLog().debug( "Version set to " + artifactVersion + " for parent: " + artifact );
                }
                else
                {
                    getLog().warn( "Could not find the version tag for parent " + artifact + " in project "
                        + getProject().getId() + " so unable to set version to " + artifactVersion );
                }
            }
        }
    }

}
 
Example 11
Source File: ResolveRangesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void resolveRanges( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
    throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{

    for ( Dependency dep : dependencies )
    {
        if ( isExcludeReactor() && isProducedByReactor( dep ) )
        {
            continue;
        }

        if ( isHandledByProperty( dep ) )
        {
            getLog().debug( "Ignoring dependency with property as version: " + toString( dep ) );
            continue;
        }

        Matcher versionMatcher = matchRangeRegex.matcher( dep.getVersion() );

        if ( versionMatcher.find() )
        {
            Artifact artifact = this.toArtifact( dep );

            if ( artifact != null && isIncluded( artifact ) )
            {
                getLog().debug( "Resolving version range for dependency: " + artifact );

                String artifactVersion = artifact.getVersion();
                if ( artifactVersion == null )
                {
                    ArtifactVersion latestVersion =
                        findLatestVersion( artifact, artifact.getVersionRange(), allowSnapshots, false );

                    if ( latestVersion != null )
                    {
                        artifactVersion = latestVersion.toString();
                    }
                    else
                    {
                        getLog().warn( "Not updating version " + artifact + " : could not resolve any versions" );
                    }
                }

                if ( artifactVersion != null )
                {
                    if ( PomHelper.setDependencyVersion( pom, artifact.getGroupId(), artifact.getArtifactId(),
                                                         dep.getVersion(), artifactVersion,
                                                         getProject().getModel() ) )
                    {
                        getLog().debug( "Version set to " + artifactVersion + " for dependency: " + artifact );
                    }
                    else
                    {
                        getLog().debug( "Could not find the version tag for dependency " + artifact + " in project "
                            + getProject().getId() + " so unable to set version to " + artifactVersion );
                    }
                }
            }
        }
    }
}
 
Example 12
Source File: MercuryVersionComparator.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
protected int innerGetSegmentCount( ArtifactVersion v )
{
    final String version = v.toString();
    StringTokenizer tok = new StringTokenizer( version, ".-" );
    return tok.countTokens();
}
 
Example 13
Source File: MavenVersionComparator.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected int innerGetSegmentCount( ArtifactVersion v )
{
    // if the version does not match the maven rules, then we have only one segment
    // i.e. the qualifier
    if ( v.getBuildNumber() != 0 )
    {
        // the version was successfully parsed, and we have a build number
        // have to have four segments
        return 4;
    }
    if ( ( v.getMajorVersion() != 0 || v.getMinorVersion() != 0 || v.getIncrementalVersion() != 0 )
        && v.getQualifier() != null )
    {
        // the version was successfully parsed, and we have a qualifier
        // have to have four segments
        return 4;
    }
    final String version = v.toString();
    if ( version.indexOf( '-' ) != -1 )
    {
        // the version has parts and was not parsed successfully
        // have to have one segment
        return version.equals( v.getQualifier() ) ? 1 : 4;
    }
    if ( version.indexOf( '.' ) != -1 )
    {
        // the version has parts and was not parsed successfully
        // have to have one segment
        return version.equals( v.getQualifier() ) ? 1 : 3;
    }
    if ( StringUtils.isEmpty( version ) )
    {
        return 3;
    }
    try
    {
        Integer.parseInt( version );
        return 3;
    }
    catch ( NumberFormatException e )
    {
        return 1;
    }
}
 
Example 14
Source File: MavenVersionComparator.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment )
{
    int segmentCount = innerGetSegmentCount( v );
    if ( segment < 0 || segment >= segmentCount )
    {
        throw new InvalidSegmentException( segment, segmentCount, v.toString() );
    }
    String version = v.toString();
    if ( segmentCount == 1 )
    {
        // only the qualifier
        version = VersionComparators.alphaNumIncrement( version );
        return new DefaultArtifactVersion( version );
    }
    else
    {
        int major = v.getMajorVersion();
        int minor = v.getMinorVersion();
        int incremental = v.getIncrementalVersion();
        int build = v.getBuildNumber();
        String qualifier = v.getQualifier();

        int minorIndex = version.indexOf( '.' );
        boolean haveMinor = minorIndex != -1;
        int incrementalIndex = haveMinor ? version.indexOf( '.', minorIndex + 1 ) : -1;
        boolean haveIncremental = incrementalIndex != -1;
        int buildIndex = version.indexOf( '-' );
        boolean haveBuild = buildIndex != -1 && qualifier == null;
        boolean haveQualifier = buildIndex != -1 && qualifier != null;

        switch ( segment )
        {
            case 0:
                major++;
                minor = 0;
                incremental = 0;
                build = 0;
                qualifier = null;
                break;
            case 1:
                minor++;
                incremental = 0;
                build = 0;
                if ( haveQualifier && qualifier.endsWith( "SNAPSHOT" ) )
                {
                    qualifier = "SNAPSHOT";
                }
                break;
            case 2:
                incremental++;
                build = 0;
                qualifier = null;
                break;
            case 3:
                if ( haveQualifier )
                {
                    qualifier = qualifierIncrement( qualifier );
                }
                else
                {
                    build++;
                }
                break;
        }
        StringBuilder result = new StringBuilder();
        result.append( major );
        if ( haveMinor || minor > 0 || incremental > 0 )
        {
            result.append( '.' );
            result.append( minor );
        }
        if ( haveIncremental || incremental > 0 )
        {
            result.append( '.' );
            result.append( incremental );
        }
        if ( haveQualifier && qualifier != null )
        {
            result.append( '-' );
            result.append( qualifier );
        }
        else if ( haveBuild || build > 0 )
        {
            result.append( '-' );
            result.append( build );
        }
        return new DefaultArtifactVersion( result.toString() );
    }
}
 
Example 15
Source File: DisplayPluginUpdatesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private String getRequiredMavenVersion( MavenProject mavenProject, String defaultValue )
{
    ArtifactVersion requiredMavenVersion = new RequiredMavenVersionFinder( mavenProject ).find();

    return requiredMavenVersion == null ? defaultValue : requiredMavenVersion.toString();
}
 
Example 16
Source File: ReleasedVersionMojo.java    From build-helper-maven-plugin with MIT License 4 votes vote down vote up
@SuppressWarnings( "unchecked" )
public void execute()
{
    org.apache.maven.artifact.Artifact artifact =
        artifactFactory.createArtifact( getProject().getGroupId(), getProject().getArtifactId(), getProject().getVersion(), "", "" );
    try
    {
        ArtifactVersion releasedVersion = null;
        List<ArtifactVersion> versions =
            artifactMetadataSource.retrieveAvailableVersions( artifact, localRepository,
                                                              remoteArtifactRepositories );
        for ( ArtifactVersion version : versions )
        {
            if ( !ArtifactUtils.isSnapshot( version.toString() )
                && ( releasedVersion == null || version.compareTo( releasedVersion ) > 0 ) )
            {
                releasedVersion = version;
            }
        }

        if ( releasedVersion != null )
        {
            // Use ArtifactVersion.toString(), the major, minor and incrementalVersion return all an int.
            String releasedVersionValue = releasedVersion.toString();

            // This would not always reflect the expected version.
            int dashIndex = releasedVersionValue.indexOf( '-' );
            if ( dashIndex >= 0 )
            {
                releasedVersionValue = releasedVersionValue.substring( 0, dashIndex );
            }

            defineVersionProperty( "version", releasedVersionValue );
            defineVersionProperty( "majorVersion", releasedVersion.getMajorVersion() );
            defineVersionProperty( "minorVersion", releasedVersion.getMinorVersion() );
            defineVersionProperty( "incrementalVersion", releasedVersion.getIncrementalVersion() );
            defineVersionProperty( "buildNumber", releasedVersion.getBuildNumber() );
            defineVersionProperty( "qualifier", releasedVersion.getQualifier() );
        }
        else {
            getLog().debug("No released version found.");
        }

    }
    catch ( ArtifactMetadataRetrievalException e )
    {
        if ( getLog().isWarnEnabled() )
        {
            getLog().warn( "Failed to retrieve artifacts metadata, cannot resolve the released version" );
        }
    }
}