Java Code Examples for org.codehaus.mojo.versions.api.PomHelper#setProjectParentVersion()

The following examples show how to use org.codehaus.mojo.versions.api.PomHelper#setProjectParentVersion() . 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: UseReactorMojo.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
private void useReactor( ModifiedPomXMLEventReader pom, MavenProject parent )
        throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
    for ( Object reactorProject : reactorProjects )
    {
        MavenProject project = (MavenProject) reactorProject;
        if ( StringUtils.equals( project.getGroupId(), parent.getGroupId() )
                && StringUtils.equals( project.getArtifactId(), parent.getArtifactId() )
                && !StringUtils.equals( project.getVersion(), parent.getVersion() ) )
        {
            if ( PomHelper.setProjectParentVersion( pom, project.getVersion() ) )
            {
                getLog().info( "Updated parent " + toString( parent ) + " to version " + project.getVersion() );
            }
        }

    }
}
 
Example 2
Source File: POM.java    From pomutils with Apache License 2.0 6 votes vote down vote up
/**
 * Saves the pom, if it was changed.
 */
public void savePom() throws IOException, XMLStreamException {
	if (!changed) {
		return;
	}

	if (this.projectVersion != null) {
		changed |= PomHelper.setProjectVersion(pom, this.projectVersion);
	}

	if (this.parentVersion != null) {
		changed |= PomHelper.setProjectParentVersion(pom, this.parentVersion);
	}

	if (this.scmTag != null) {
		changed |= PomHelper.setProjectValue(pom, "/project/scm/tag", this.scmTag);
	}

	if (!changed) {
		return;
	}

	FileUtils.fileWrite(pomFile.getAbsolutePath(), pom.asStringBuilder().toString());
}
 
Example 3
Source File: LockSnapshotsMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void lockParentSnapshot( ModifiedPomXMLEventReader pom, MavenProject parent )
    throws XMLStreamException, MojoExecutionException
{
    if ( parent == null )
    {
        getLog().info( "Project does not have a parent" );
        return;
    }

    if ( reactorProjects.contains( parent ) )
    {
        getLog().info( "Project's parent is part of the reactor" );
        return;
    }

    Artifact parentArtifact = parent.getArtifact();
    String parentVersion = parentArtifact.getVersion();

    Matcher versionMatcher = matchSnapshotRegex.matcher( parentVersion );
    if ( versionMatcher.find() && versionMatcher.end() == parentVersion.length() )
    {
        String lockedParentVersion = resolveSnapshotVersion( parentArtifact );
        if ( !parentVersion.equals( lockedParentVersion ) )
        {
            if ( PomHelper.setProjectParentVersion( pom, lockedParentVersion ) )
            {
                getLog().info( "Locked parent " + parentArtifact.toString() + " to version "
                    + lockedParentVersion );
            }
        }
    }
}
 
Example 4
Source File: UnlockSnapshotsMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void unlockParentSnapshot( ModifiedPomXMLEventReader pom, MavenProject parent )
    throws XMLStreamException, MojoExecutionException
{
    if ( parent == null )
    {
        getLog().info( "Project does not have a parent" );
        return;
    }

    if ( reactorProjects.contains( parent ) )
    {
        getLog().info( "Project's parent is part of the reactor" );
        return;
    }

    Artifact parentArtifact = parent.getArtifact();
    String parentVersion = parentArtifact.getVersion();

    Matcher versionMatcher = matchSnapshotRegex.matcher( parentVersion );
    if ( versionMatcher.find() && versionMatcher.end() == parentVersion.length() )
    {
        String unlockedParentVersion = versionMatcher.replaceFirst( "-SNAPSHOT" );
        if ( PomHelper.setProjectParentVersion( pom, unlockedParentVersion ) )
        {
            getLog().info( "Unlocked parent " + parentArtifact.toString() + " to version "
                + unlockedParentVersion );
        }
    }
}
 
Example 5
Source File: ParentVersionChanger.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void apply( VersionChange versionChange )
    throws XMLStreamException
{
    if ( getModel().getParent() != null && versionChange.getGroupId().equals( getModel().getParent().getGroupId() )
        && versionChange.getArtifactId().equals( getModel().getParent().getArtifactId() ) )
    {
        if ( PomHelper.setProjectParentVersion( getPom(), versionChange.getNewVersion() ) )
        {
            info( "    Updating parent " + versionChange.getGroupId() + ":" + versionChange.getArtifactId() );
            info( "        from version " + versionChange.getOldVersion() + " to "
                + versionChange.getNewVersion() );
        }
    }
}
 
Example 6
Source File: UpdateChildModulesMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the pom file.
 *
 * @param pom The pom file to update.
 * @throws MojoExecutionException when things go wrong.
 * @throws MojoFailureException when things go wrong.
 * @throws XMLStreamException when things go wrong.
 */
protected synchronized void update( ModifiedPomXMLEventReader pom )
    throws MojoExecutionException, MojoFailureException, XMLStreamException
{
    getLog().debug( "Updating parent to " + sourceVersion );

    if ( PomHelper.setProjectParentVersion( pom, sourceVersion ) )
    {
        getLog().debug( "Made an update to " + sourceVersion );
    }
}
 
Example 7
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 8
Source File: UseReleasesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void useReleases( ModifiedPomXMLEventReader pom, MavenProject project )
    throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
    String version = project.getVersion();
    Matcher versionMatcher = matchSnapshotRegex.matcher( version );
    if ( versionMatcher.matches() )
    {
        String releaseVersion = versionMatcher.group( 1 );

        VersionRange versionRange;
        try
        {
            versionRange = VersionRange.createFromVersionSpec( releaseVersion );
        }
        catch ( InvalidVersionSpecificationException e )
        {
            throw new MojoExecutionException( "Invalid version range specification: " + version, e );
        }

        Artifact artifact = artifactFactory.createDependencyArtifact( getProject().getParent().getGroupId(),
                                                                      getProject().getParent().getArtifactId(),
                                                                      versionRange, "pom", null, null );
        if ( !isIncluded( artifact ) )
        {
            return;
        }

        getLog().debug( "Looking for a release of " + toString( project ) );
        // Force releaseVersion version because org.apache.maven.artifact.metadata.MavenMetadataSource does not
        // retrieve release version if provided snapshot version.
        artifact.setVersion( releaseVersion );
        ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
        if ( !allowRangeMatching ) // standard behaviour
        {
            if ( versions.containsVersion( releaseVersion ) )
            {
                if ( PomHelper.setProjectParentVersion( pom, releaseVersion ) )
                {
                    getLog().info( "Updated " + toString( project ) + " to version " + releaseVersion );
                }
            }
            else if ( failIfNotReplaced )
            {
                throw new NoSuchElementException( "No matching release of " + toString( project )
                    + " found for update." );
            }
        }
        else
        {
            ArtifactVersion finalVersion = null;
            for ( ArtifactVersion proposedVersion : versions.getVersions( false ) )
            {
                if ( proposedVersion.toString().startsWith( releaseVersion ) )
                {
                    getLog().debug( "Found matching version for " + toString( project ) + " to version "
                        + releaseVersion );
                    finalVersion = proposedVersion;
                }
            }

            if ( finalVersion != null )
            {
                if ( PomHelper.setProjectParentVersion( pom, finalVersion.toString() ) )
                {
                    getLog().info( "Updated " + toString( project ) + " to version " + finalVersion.toString() );
                }
            }
            else
            {
                getLog().info( "No matching release of " + toString( project ) + " to update via rangeMatching." );
                if ( failIfNotReplaced )
                {
                    throw new NoSuchElementException( "No matching release of " + toString( project )
                        + " found for update via rangeMatching." );
                }
            }

        }
    }
}
 
Example 9
Source File: UseLatestVersionsMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
    throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
    int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
    MajorMinorIncrementalFilter majorMinorIncfilter =
        new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );

    for ( Dependency dep : dependencies )
    {
        if ( isExcludeReactor() && isProducedByReactor( dep ) )
        {
            getLog().info( "Ignoring reactor dependency: " + toString( dep ) );
            continue;
        }

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

        String version = dep.getVersion();
        Artifact artifact = this.toArtifact( dep );
        if ( !isIncluded( artifact ) )
        {
            continue;
        }

        ArtifactVersion selectedVersion = new DefaultArtifactVersion( version );
        getLog().debug( "Selected version:" + selectedVersion.toString() );

        getLog().debug( "Looking for newer versions of " + toString( dep ) );
        ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );

        ArtifactVersion[] newerVersions = versions.getNewerVersions( version, segment, allowSnapshots );

        ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion, newerVersions );
        if ( filteredVersions.length > 0 )
        {
            String newVersion = filteredVersions[filteredVersions.length - 1].toString();
            if ( getProject().getParent() != null )
            {
                if ( artifact.getId().equals( getProject().getParentArtifact().getId() ) && isProcessingParent() )
                {
                    if ( PomHelper.setProjectParentVersion( pom, newVersion ) ) {
                        getLog().debug("Made parent update from " + version + " to " + newVersion);
                    }
                }
            }
            if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, newVersion,
                                                 getProject().getModel() ) ) {
                getLog().info( "Updated " + toString( dep ) + " to version " + newVersion );

            }
        }

    }
}
 
Example 10
Source File: UseLatestReleasesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void useLatestReleases( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
    throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
    int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
    MajorMinorIncrementalFilter majorMinorIncfilter =
        new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );

    for ( Dependency dep : dependencies )
    {
        if ( isExcludeReactor() && isProducedByReactor( dep ) )
        {
            getLog().info( "Ignoring reactor dependency: " + toString( dep ) );
            continue;
        }

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

        String version = dep.getVersion();
        Matcher versionMatcher = matchSnapshotRegex.matcher( version );
        if ( !versionMatcher.matches() )
        {
            Artifact artifact = this.toArtifact( dep );
            if ( !isIncluded( artifact ) )
            {
                continue;
            }

            ArtifactVersion selectedVersion = new DefaultArtifactVersion( version );
            getLog().debug( "Selected version:" + selectedVersion.toString() );

            getLog().debug( "Looking for newer versions of " + toString( dep ) );
            ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
            ArtifactVersion[] newer = versions.getNewerVersions( version, segment, false );
            newer = filterVersionsWithIncludes( newer, artifact );

            ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion, newer );
            if ( filteredVersions.length > 0 )
            {
                String newVersion = filteredVersions[filteredVersions.length - 1].toString();
                if ( getProject().getParent() != null )
                {
                    if ( artifact.getId().equals( getProject().getParentArtifact().getId() ) && isProcessingParent() )
                    {
                        if ( PomHelper.setProjectParentVersion( pom, newVersion.toString() ) )
                        {
                            getLog().debug( "Made parent update from " + version + " to " + newVersion.toString() );
                        }
                    }
                }
                if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
                                                     newVersion, getProject().getModel() ) )
                {
                    getLog().info( "Updated " + toString( dep ) + " to version " + newVersion );
                }
            }
        }
    }
}
 
Example 11
Source File: UseLatestSnapshotsMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
    throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
    int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
    MajorMinorIncrementalFilter majorMinorIncfilter =
            new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );

    for ( Dependency dep : dependencies )
    {
        if ( isExcludeReactor() && isProducedByReactor( dep ) )
        {
            getLog().info( "Ignoring reactor dependency: " + toString( dep ) );
            continue;
        }

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

        String version = dep.getVersion();
        Matcher versionMatcher = matchSnapshotRegex.matcher(version);
        if ( !versionMatcher.matches() )
        {
            getLog().debug( "Looking for latest snapshot of " + toString( dep ) );
            Artifact artifact = this.toArtifact( dep );
            if ( !isIncluded( artifact ) )
            {
                continue;
            }

            ArtifactVersion selectedVersion = new DefaultArtifactVersion( version );

            ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
            final VersionComparator versionComparator = versions.getVersionComparator();
            final DefaultArtifactVersion lowerBound = new DefaultArtifactVersion( version );
            if ( segment + 1 > versionComparator.getSegmentCount( lowerBound ) )
            {
                getLog().info( "Ignoring " + toString( dep ) + " as the version number is too short" );
                continue;
            }
            ArtifactVersion upperBound =
                segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null;
            getLog().info( "Upper bound: " + (upperBound == null ? "none" : upperBound.toString() ) );
            ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false );
            getLog().debug( "Candidate versions " + Arrays.asList( newer ) );

            String latestVersion = null;
            ArrayList snapshotsOnly = new ArrayList();

            for ( int j = 0; j < newer.length; j++ )
            {
                String newVersion = newer[j].toString();
                if ( matchSnapshotRegex.matcher( newVersion ).matches() )
                {
                    snapshotsOnly.add( newer[j] );
                }
            }
            getLog().debug("Snapshot Only versions " + snapshotsOnly.toString());

            ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion,
                                                                            (ArtifactVersion[]) snapshotsOnly.toArray(
                                                                                new ArtifactVersion[snapshotsOnly.size()] ) );
            getLog().debug( "Filtered versions " + Arrays.asList( filteredVersions ) );


            if ( filteredVersions.length > 0 )
            {
                latestVersion = filteredVersions[filteredVersions.length - 1].toString();
                if ( getProject().getParent() != null )
                {
                    if ( artifact.getId().equals(getProject().getParentArtifact().getId()) && isProcessingParent() )
                    {
                        if ( PomHelper.setProjectParentVersion( pom, latestVersion.toString() ) )
                        {
                            getLog().debug( "Made parent update from " + version + " to " + latestVersion.toString() );
                        }
                    }
                }

                if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
                                                     latestVersion, getProject().getModel() ) )
                {
                    getLog().info( "Updated " + toString( dep ) + " to version " + latestVersion );
                }
            }
        }
    }
}