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

The following examples show how to use org.apache.maven.artifact.versioning.ArtifactVersion#getMajorVersion() . 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: Utils.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Compare the major and minor components of the two given version numbers.
 * The other components are ignored.
 *
 * @param first is the first version number.
 * @param second is the second version number.
 * @return A negative value if {@code first} is lower than {@code second}; a positive value if {@code first} is
 *     greater than {@code second}; otherwise {@code 0} if {@code first} and {@code second} are equal.
 * @since 0.10
 */
public static int compareMajorMinorVersions(final ArtifactVersion first, final ArtifactVersion second) {
	if (first == second) {
		return 0;
	}
	if (first == null) {
		return Integer.MIN_VALUE;
	}
	if (second == null) {
		return Integer.MAX_VALUE;
	}
	int na = first.getMajorVersion();
	int nb = first.getMajorVersion();
	final int cmp = na - nb;
	if (cmp != 0) {
		return cmp;
	}
	na = first.getMinorVersion();
	nb = first.getMinorVersion();
	return na - nb;
}
 
Example 2
Source File: ArtifactRetriever.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private static boolean isReleased(ArtifactVersion version) {
  String qualifier = version.getQualifier();
  if (version.getMajorVersion() <= 0) {
    return false;
  } else if (Strings.isNullOrEmpty(qualifier)) {
    return true; 
  } else if ("final".equalsIgnoreCase(qualifier.toLowerCase(Locale.US))) {
    return true; 
  }
  
  return false;
}
 
Example 3
Source File: VersionUtil.java    From maven-confluence-plugin with Apache License 2.0 5 votes vote down vote up
public static String calculateVersionTagNamePart(String version, CalculateRuleForSinceTagName calculateRuleForSinceTagName) {

        if (calculateRuleForSinceTagName.equals(CalculateRuleForSinceTagName.NO_RULE)) {
            return null;
        }
        ArtifactVersion artifactVersion = parseArtifactVersion(version);
        int major = artifactVersion.getMajorVersion();
        int minor = artifactVersion.getMinorVersion();
        int patch = artifactVersion.getIncrementalVersion();

        switch (calculateRuleForSinceTagName) {
            case CURRENT_MAJOR_VERSION:
                minor = 0;
                patch = 0;
                break;
            case CURRENT_MINOR_VERSION:
                patch = 0;
                break;
            case LATEST_RELEASE_VERSION:
                patch = patch == 0 ? 0 : patch - 1;
                break;
            default:
                throw new RuntimeException("cannot parse " + calculateRuleForSinceTagName);
        }

        return major + "." + minor + "." + patch;
    }
 
Example 4
Source File: AbstractCompileMojo.java    From sarl with Apache License 2.0 5 votes vote down vote up
private void ensureSARLVersions() throws MojoExecutionException, MojoFailureException {
	final String compilerVersionString = this.mavenHelper.getConfig("plugin.version"); //$NON-NLS-1$
	final ArtifactVersion compilerVersion = new DefaultArtifactVersion(compilerVersionString);
	final ArtifactVersion maxCompilerVersion = new DefaultArtifactVersion(
			compilerVersion.getMajorVersion() + "." //$NON-NLS-1$
			+ (compilerVersion.getMinorVersion() + 1)
			+ ".0"); //$NON-NLS-1$
	getLog().info(MessageFormat.format(Messages.CompileMojo_0, compilerVersionString, maxCompilerVersion));
	final StringBuilder classpath = new StringBuilder();
	final Set<String> foundVersions = findSARLLibrary(compilerVersion, maxCompilerVersion, classpath,
			this.tycho);
	if (foundVersions.isEmpty()) {
		throw new MojoFailureException(MessageFormat.format(Messages.CompileMojo_1, classpath.toString()));
	}
	final StringBuilder versions = new StringBuilder();
	for (final String version : foundVersions) {
		if (versions.length() > 0) {
			versions.append(", "); //$NON-NLS-1$
		}
		versions.append(version);
	}
	if (foundVersions.size() > 1) {
		getLog().info(MessageFormat.format(Messages.CompileMojo_2, versions));
	} else {
		getLog().info(MessageFormat.format(Messages.CompileMojo_3, versions));
	}
}
 
Example 5
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 6
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 7
Source File: MajorMinorIncrementalFilter.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * @param selectedVersion The version which will be checked.
 * @param newerVersions The list of identified versions which are greater or equal than the selectedVersion.
 * @return The cleaned up list which obeys usage of {@link #allowMajorUpdates}, {@link #allowMinorUpdates},
 *         {@link #allowIncrementalUpdates}.
 */
public ArtifactVersion[] filter( ArtifactVersion selectedVersion, ArtifactVersion[] newerVersions )
{
    List<ArtifactVersion> versionsToUse = new LinkedList<ArtifactVersion>();
    for ( ArtifactVersion artifactVersion : newerVersions )
    {
        if ( artifactVersion.getMajorVersion() != selectedVersion.getMajorVersion() )
        {
            if ( allowMajorUpdates )
            {
                if ( !versionsToUse.contains( artifactVersion ) )
                {
                    versionsToUse.add( artifactVersion );
                }
            }
        }
        else if ( artifactVersion.getMinorVersion() != selectedVersion.getMinorVersion() )
        {
            if ( allowMinorUpdates )
            {
                if ( !versionsToUse.contains( artifactVersion ) )
                {
                    versionsToUse.add( artifactVersion );
                }
            }
        }
        else if ( artifactVersion.getIncrementalVersion() != selectedVersion.getIncrementalVersion() )
        {
            if ( allowIncrementalUpdates )
            {
                if ( !versionsToUse.contains( artifactVersion ) )
                {
                    versionsToUse.add( artifactVersion );
                }
            }
        }
        else {
            // build number or qualifier.  Will already be sorted and higher
            if ( !versionsToUse.contains( artifactVersion ) )
            {
                    versionsToUse.add( artifactVersion );
            }
        }
    }
    return versionsToUse.toArray( new ArtifactVersion[versionsToUse.size()] );

}