Java Code Examples for org.apache.maven.artifact.ArtifactUtils#isSnapshot()

The following examples show how to use org.apache.maven.artifact.ArtifactUtils#isSnapshot() . 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: PropertyVersions.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Uses the {@link DefaultVersionsHelper} to find all available versions that match all the associations with this
 * property.
 *
 * @param includeSnapshots Whether to include snapshot versions in our search.
 * @return The (possibly empty) array of versions.
 */
public synchronized ArtifactVersion[] getVersions( boolean includeSnapshots )
{
    Set<ArtifactVersion> result;
    if ( includeSnapshots )
    {
        result = versions;
    }
    else
    {
        result = new TreeSet<>( getVersionComparator() );
        for ( ArtifactVersion candidate : versions )
        {
            if ( ArtifactUtils.isSnapshot( candidate.toString() ) )
            {
                continue;
            }
            result.add( candidate );
        }
    }
    return asArtifactVersionArray( result );
}
 
Example 2
Source File: ArtifactVersions.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
public ArtifactVersion[] getVersions( boolean includeSnapshots )
{
    Set<ArtifactVersion> result;
    if ( includeSnapshots )
    {
        result = versions;
    }
    else
    {
        result = new TreeSet<>( versionComparator );
        for ( ArtifactVersion candidate : versions )
        {
            if ( ArtifactUtils.isSnapshot( candidate.toString() ) )
            {
                continue;
            }
            result.add( candidate );
        }
    }
    return result.toArray( new ArtifactVersion[result.size()] );
}
 
Example 3
Source File: ExternalVersionExtension.java    From maven-external-version with Apache License 2.0 6 votes vote down vote up
private String getNewVersion( ExternalVersionStrategy strategy, MavenProject mavenProject )
    throws ExternalVersionException
{

    // snapshot detection against the old version.
    boolean isSnapshot = ArtifactUtils.isSnapshot( mavenProject.getVersion() );

    // lookup the new version
    String newVersion = strategy.getVersion( mavenProject );

    if ( newVersion != null )
    {
        newVersion = newVersion.trim();
    }

    boolean isNewSnapshot = ArtifactUtils.isSnapshot( newVersion );
    // make sure we still have a SNAPSHOT if we had one previously.
    if ( isSnapshot && !isNewSnapshot )
    {
        newVersion = newVersion + "-SNAPSHOT";
    }
    return newVersion;
}
 
Example 4
Source File: DependencyNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public String getHtmlDisplayName() {
    StringBuilder n = new StringBuilder("<html>");
    n.append(getDisplayName());
    if (ArtifactUtils.isSnapshot(data.art.getVersion()) && data.art.getVersion().indexOf("SNAPSHOT") < 0) { //NOI18N
        n.append(" <b>[").append(data.art.getVersion()).append("]</b>");
    }
    if (!data.art.getArtifactHandler().isAddedToClasspath() && !Artifact.SCOPE_COMPILE.equals(data.art.getScope())) {
        n.append("  <i>[").append(data.art.getScope()).append("]</i>");
    }
    n.append("</html>");
    return n.toString();
}
 
Example 5
Source File: AbstractVersionDetails.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
public final ArtifactVersion getNewestVersion( VersionRange versionRange, ArtifactVersion lowerBound,
                                               ArtifactVersion upperBound, boolean includeSnapshots,
                                               boolean includeLower, boolean includeUpper )
{
    ArtifactVersion latest = null;
    final VersionComparator versionComparator = getVersionComparator();
    for ( ArtifactVersion candidate : getVersions( includeSnapshots ) )
    {
        if ( versionRange != null && !ArtifactVersions.isVersionInRange( candidate, versionRange ) )
        {
            continue;
        }
        int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate );
        int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate );
        if ( lower > 0 || upper < 0 )
        {
            continue;
        }
        if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) )
        {
            continue;
        }
        if ( !includeSnapshots && ArtifactUtils.isSnapshot( candidate.toString() ) )
        {
            continue;
        }
        if ( latest == null )
        {
            latest = candidate;
        }
        else if ( versionComparator.compare( latest, candidate ) < 0 )
        {
            latest = candidate;
        }

    }
    return latest;
}
 
Example 6
Source File: AbstractVersionDetails.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
public final ArtifactVersion getOldestVersion( VersionRange versionRange, ArtifactVersion lowerBound,
                                               ArtifactVersion upperBound, boolean includeSnapshots,
                                               boolean includeLower, boolean includeUpper )
{
    ArtifactVersion oldest = null;
    final VersionComparator versionComparator = getVersionComparator();
    for ( ArtifactVersion candidate : getVersions( includeSnapshots ) )
    {
        if ( versionRange != null && !ArtifactVersions.isVersionInRange( candidate, versionRange ) )
        {
            continue;
        }
        int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate );
        int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate );
        if ( lower > 0 || upper < 0 )
        {
            continue;
        }
        if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) )
        {
            continue;
        }
        if ( !includeSnapshots && ArtifactUtils.isSnapshot( candidate.toString() ) )
        {
            continue;
        }
        if ( oldest == null )
        {
            oldest = candidate;
        }
        else if ( versionComparator.compare( oldest, candidate ) > 0 )
        {
            oldest = candidate;
        }
    }
    return oldest;
}
 
Example 7
Source File: AbstractVersionDetails.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
public final ArtifactVersion[] getVersions( VersionRange versionRange, ArtifactVersion lowerBound,
                                            ArtifactVersion upperBound, boolean includeSnapshots,
                                            boolean includeLower, boolean includeUpper )
{
    final VersionComparator versionComparator = getVersionComparator();
    Set<ArtifactVersion> result = new TreeSet<>( versionComparator );
    for ( ArtifactVersion candidate : Arrays.asList( getVersions( includeSnapshots ) ) )
    {
        if ( versionRange != null && !ArtifactVersions.isVersionInRange( candidate, versionRange ) )
        {
            continue;
        }
        int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate );
        int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate );
        if ( lower > 0 || upper < 0 )
        {
            continue;
        }
        if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) )
        {
            continue;
        }
        if ( !includeSnapshots && ArtifactUtils.isSnapshot( candidate.toString() ) )
        {
            continue;
        }
        result.add( candidate );
    }
    return result.toArray( new ArtifactVersion[result.size()] );
}
 
Example 8
Source File: EnforceVersionsMojo.java    From gitflow-helper-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected void execute(final GitBranchInfo branchInfo) throws MojoExecutionException, MojoFailureException {
    if (branchInfo.isVersioned()) {
        getLog().debug("Versioned Branch: " + branchInfo);
        Matcher gitMatcher = Pattern.compile(branchInfo.getPattern()).matcher(branchInfo.getName());

        // We're in a versioned branch, we expect a non-SNAPSHOT version in the POM.
        if (gitMatcher.matches()) {
            // Always assert that pom versions match our expectations.
            if (hasSnapshotInModel(project)) {
                throw new MojoFailureException("The current git branch: [" + branchInfo.getName() + "] is defined as a release branch. The maven project or one of its parents is currently a snapshot version.");
            }

            // Non-master version branches require a pom version match of some kind to the branch subgroups.
            if (gitMatcher.groupCount() > 0 && gitMatcher.group(gitMatcher.groupCount()) != null) {
                checkReleaseTypeBranchVersion(branchInfo, gitMatcher);
            }

            // Optionally (default true) reinforce that no dependencies may be snapshots.
            if (enforceNonSnapshots) {
                Set<String> snapshotDeps = getSnapshotDeps();
                if (!snapshotDeps.isEmpty()) {
                    throw new MojoFailureException("The current git branch: [" + branchInfo.getName() + "] is defined as a release branch. The maven project has the following SNAPSHOT dependencies: " + snapshotDeps.toString());
                }

                Set<String> snapshotPluginDeps = getSnapshotPluginDeps();
                if (!snapshotPluginDeps.isEmpty()) {
                    throw new MojoFailureException("The current git branch: [" + branchInfo.getName() + "] is defined as a release branch. The maven project has the following SNAPSHOT plugin dependencies: " + snapshotPluginDeps.toString());
                }
            }
        }
    } else if (branchInfo.isSnapshot() && !ArtifactUtils.isSnapshot(project.getVersion())) {
        throw new MojoFailureException("The current git branch: [" + branchInfo.getName() + "] is detected as a SNAPSHOT-type branch, and expects a maven project version ending with -SNAPSHOT. The maven project version found was: [" + project.getVersion() + "]");
    }
}
 
Example 9
Source File: EnforceVersionsMojo.java    From gitflow-helper-maven-plugin with Apache License 2.0 5 votes vote down vote up
private boolean hasSnapshotInModel(final MavenProject project) {
    MavenProject parent = project.getParent();

    boolean projectIsSnapshot = ArtifactUtils.isSnapshot(project.getVersion());
    boolean parentIsSnapshot = parent != null && hasSnapshotInModel(parent);

    return projectIsSnapshot || parentIsSnapshot;
}
 
Example 10
Source File: EnforceVersionsMojo.java    From gitflow-helper-maven-plugin with Apache License 2.0 5 votes vote down vote up
private Set<String> getSnapshotDeps() {
    Set<String> snapshotDeps = new HashSet<>();
    for (Artifact dep : project.getArtifacts()) {
        if (ArtifactUtils.isSnapshot(dep.getVersion())) {
            getLog().debug("SNAPSHOT dependency found: " + dep.toString());
            snapshotDeps.add(dep.toString());
        }
    }

    return snapshotDeps;
}
 
Example 11
Source File: AbstractGitFlowMojo.java    From gitflow-maven-plugin with Apache License 2.0 5 votes vote down vote up
protected void checkSnapshotDependencies() throws MojoFailureException {
    getLog().info("Checking for SNAPSHOT versions in dependencies.");

    List<String> snapshots = new ArrayList<String>();
    List<String> builtArtifacts = new ArrayList<String>();

    List<MavenProject> projects = mavenSession.getProjects();
    for (MavenProject project : projects) {
        final MavenProject reloadedProject = reloadProject(project);

        builtArtifacts.add(reloadedProject.getGroupId() + ":" + reloadedProject.getArtifactId() + ":" + reloadedProject.getVersion());

        List<Dependency> dependencies = reloadedProject.getDependencies();
        for (Dependency d : dependencies) {
            String id = d.getGroupId() + ":" + d.getArtifactId() + ":" + d.getVersion();
            if (!builtArtifacts.contains(id) && ArtifactUtils.isSnapshot(d.getVersion())) {
                snapshots.add(reloadedProject + " -> " + d);
            }
        }
    }

    if (!snapshots.isEmpty()) {
        for (String s : snapshots) {
            getLog().warn(s);
        }
        throw new MojoFailureException(
                "There is some SNAPSHOT dependencies in the project, see warnings above. Change them or ignore with `allowSnapshots` property.");
    }
}
 
Example 12
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" );
        }
    }
}