org.codehaus.mojo.versions.api.PomHelper Java Examples

The following examples show how to use org.codehaus.mojo.versions.api.PomHelper. 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: AbstractVersionsUpdaterMojo.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
protected void updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property,
                                              PropertyVersions version, String currentVersion,
                                              boolean allowDowngrade, int segment )
    throws MojoExecutionException, XMLStreamException
{
    ArtifactVersion winner =
        version.getNewestVersion( currentVersion, property, this.allowSnapshots, this.reactorProjects,
                                  this.getHelper(), allowDowngrade, segment );

    if ( winner == null || currentVersion.equals( winner.toString() ) )
    {
        getLog().info( "Property ${" + property.getName() + "}: Leaving unchanged as " + currentVersion );
    }
    else if ( PomHelper.setPropertyVersion( pom, version.getProfileId(), property.getName(), winner.toString() ) )
    {
        getLog().info( "Updated ${" + property.getName() + "} from " + currentVersion + " to " + winner );
    }
}
 
Example #2
Source File: ReactorDepthComparator.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
public int compare( String o1, String o2 )
{
    final Model m1 = reactor.get( o1 );
    final Model m2 = reactor.get( o2 );
    final int d1 = PomHelper.getReactorParentCount( reactor, m1 );
    final int d2 = PomHelper.getReactorParentCount( reactor, m2 );
    if ( d1 < d2 )
    {
        return -1;
    }
    else if ( d1 > d2 )
    {
        return 1;
    }
    return PomHelper.getGAV( m1 ).compareTo( PomHelper.getGAV( m2 ) );
}
 
Example #3
Source File: UseReleasesMojo.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
private void noRangeMatching( ModifiedPomXMLEventReader pom, Dependency dep, String version, String releaseVersion,
                              ArtifactVersions versions )
    throws XMLStreamException
{
    if ( versions.containsVersion( releaseVersion ) )
    {
        if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, releaseVersion,
                                             getProject().getModel() ) )
        {
            getLog().info( "Updated " + toString( dep ) + " to version " + releaseVersion );
        }
    }
    else if ( failIfNotReplaced )
    {
        throw new NoSuchElementException( "No matching release of " + toString( dep ) + " found for update." );
    }
}
 
Example #4
Source File: UseReleasesMojo.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * @param pom the pom to update.
 * @throws org.apache.maven.plugin.MojoExecutionException when things go wrong
 * @throws org.apache.maven.plugin.MojoFailureException when things go wrong in a very bad way
 * @throws javax.xml.stream.XMLStreamException when things go wrong with XML streaming
 * @see org.codehaus.mojo.versions.AbstractVersionsUpdaterMojo#update(org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader)
 */
protected void update( ModifiedPomXMLEventReader pom )
    throws MojoExecutionException, MojoFailureException, XMLStreamException
{
    try
    {
        if ( getProject().getParent() != null && isProcessingParent() )
        {
            useReleases( pom, getProject().getParent() );
        }

        if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
        {
            useReleases( pom, PomHelper.readImportedPOMsFromDependencyManagementSection( pom ) );
            useReleases( pom, getProject().getDependencyManagement().getDependencies() );
        }
        if ( getProject().getDependencies() != null && isProcessingDependencies() )
        {
            useReleases( pom, getProject().getDependencies() );
        }
    }
    catch ( ArtifactMetadataRetrievalException e )
    {
        throw new MojoExecutionException( e.getMessage(), e );
    }
}
 
Example #5
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 #6
Source File: SetScmTagMojo.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected void update(ModifiedPomXMLEventReader pom) throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
{
    try
    {
        Model model = PomHelper.getRawModel( pom );
        Scm scm = model.getScm();
        if (scm == null)
        {
            throw new MojoFailureException( "No <scm> was present" );
        }
        getLog().info( "Updating from tag " + scm.getTag() + " > " + newTag );

        boolean success = PomHelper.setProjectValue(pom, "/project/scm/tag", newTag );
        if ( !success )
        {
            throw new MojoFailureException( "Could not update the SCM tag" );
        }
    }
    catch ( IOException e )
    {
        throw new MojoExecutionException( e.getMessage(), e );
    }
}
 
Example #7
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 #8
Source File: PomMergeDriverTest.java    From pomutils with Apache License 2.0 6 votes vote down vote up
public void testAutoMergeSucceded_prompt() throws Exception {
	String myTestSubFolder = "merge/autoMergeSucceded_prompt";

	TestUtils.prepareTestFolder(myTestSubFolder);

	String basePomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/base.pom.xml";
	String ourPomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/our.pom.xml";
	String theirPomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/their.pom.xml";

	Ruleset ruleset = new Ruleset(SelectionStrategy.THEIR);

	PomMergeDriver pomMergeDriver = new PomMergeDriver(ruleset, basePomFile, ourPomFile, theirPomFile);
	int mergeReturnValue = pomMergeDriver.merge();

	assertTrue("merge succeeded", mergeReturnValue == 0);
	POM theirPom = new POM(theirPomFile);
	POM ourPom = new POM(ourPomFile);

	assertEquals("their", ourPom.getParentVersion());
	assertEquals("their", theirPom.getParentVersion());

	String theirDependecyVersoin = PomHelper.getRawModel(new File(theirPomFile)).getDependencies().get(0).getVersion();
	String ourDependencyVersion = PomHelper.getRawModel(new File(ourPomFile)).getDependencies().get(0).getVersion();

	assertEquals("dependency version change merged", theirDependecyVersoin, ourDependencyVersion);
}
 
Example #9
Source File: SetMojo.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 org.apache.maven.plugin.MojoExecutionException when things go wrong.
 * @throws org.apache.maven.plugin.MojoFailureException when things go wrong.
 * @throws javax.xml.stream.XMLStreamException when things go wrong.
 */
protected synchronized void update( ModifiedPomXMLEventReader pom )
    throws MojoExecutionException, MojoFailureException, XMLStreamException
{
    ContextualLog log = new DelegatingContextualLog( getLog() );
    try
    {
        Model model = PomHelper.getRawModel( pom );
        log.setContext( "Processing " + PomHelper.getGroupId( model ) + ":" + PomHelper.getArtifactId( model ) );

        VersionChangerFactory versionChangerFactory = new VersionChangerFactory();
        versionChangerFactory.setPom( pom );
        versionChangerFactory.setLog( log );
        versionChangerFactory.setModel( model );

        VersionChanger changer = versionChangerFactory.newVersionChanger( processParent, processProject,
                                                                          processDependencies, processPlugins );

        for ( VersionChange versionChange : sourceChanges )
        {
            changer.apply( versionChange );
        }
    }
    catch ( IOException e )
    {
        throw new MojoExecutionException( e.getMessage(), e );
    }
    log.clearContext();
}
 
Example #10
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 #11
Source File: UseNextVersionsMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void useNextVersions( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
    throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
    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;
        }

        getLog().debug( "Looking for newer versions of " + toString( dep ) );
        ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
        ArtifactVersion[] newer = versions.getNewerVersions( version, Boolean.TRUE.equals( allowSnapshots ) );
        if ( newer.length > 0 )
        {
            String newVersion = newer[0].toString();
            if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, newVersion,
                                                 getProject().getModel() ) )
            {
                getLog().info( "Updated " + toString( dep ) + " to version " + newVersion );
            }
        }
    }
}
 
Example #12
Source File: DisplayPluginUpdatesMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private String compactKey( String groupId, String artifactId )
{
    if ( PomHelper.APACHE_MAVEN_PLUGINS_GROUPID.equals( groupId ) )
    {
        // a core plugin... group id is not needed
        return artifactId;
    }
    return groupId + ":" + artifactId;
}
 
Example #13
Source File: PomUpdater.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
void write(ModelWrapper wrapper, VersionsFromBom versionsFromBom, File pom) {
	try {
		VersionChangerFactory versionChangerFactory = new VersionChangerFactory();
		StringBuilder input = PomHelper.readXmlFile(pom);
		ModifiedPomXMLEventReader parsedPom = newModifiedPomXER(input);
		versionChangerFactory.setPom(parsedPom);
		LoggerToMavenLog loggerToMavenLog = new LoggerToMavenLog(PomWriter.log);
		versionChangerFactory.setLog(loggerToMavenLog);
		versionChangerFactory.setModel(wrapper.model);
		log.info(
				"Applying version / parent / plugin / project changes to the pom [{}]",
				pom);
		VersionChanger changer = versionChangerFactory.newVersionChanger(true, true,
				true, true);
		for (VersionChange versionChange : wrapper.sourceChanges) {
			changer.apply(versionChange);
		}
		log.debug("Applying properties changes to the pom [{}]", pom);
		new PropertyVersionChanger(wrapper, versionsFromBom, parsedPom,
				loggerToMavenLog).apply(null);
		try (BufferedWriter bw = new BufferedWriter(new FileWriter(pom))) {
			bw.write(input.toString());
		}
		log.debug("Flushed changes to the pom file [{}]", pom);
	}
	catch (Exception e) {
		log.error("Exception occurred while trying to apply changes to the POM", e);
	}
}
 
Example #14
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 #15
Source File: ProjectVersionChanger.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void apply( VersionChange versionChange )
    throws XMLStreamException
{
    if ( versionChange.getGroupId().equals( PomHelper.getGroupId( getModel() ) )
        && versionChange.getArtifactId().equals( PomHelper.getArtifactId( getModel() ) ) )
    {
        if ( PomHelper.setProjectVersion( getPom(), versionChange.getNewVersion() ) )
        {
            info( "    Updating project " + versionChange.getGroupId() + ":" + versionChange.getArtifactId() );
            info( "        from version " + versionChange.getOldVersion() + " to "
                + versionChange.getNewVersion() );
        }
    }
}
 
Example #16
Source File: UnlockSnapshotsMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void unlockSnapshots( ModifiedPomXMLEventReader pom, List<Dependency> dependencies )
    throws XMLStreamException, MojoExecutionException
{
    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;
        }

        if ( !isIncluded( this.toArtifact( dep ) ) )
        {
            continue;
        }

        String version = dep.getVersion();
        Matcher versionMatcher = matchSnapshotRegex.matcher( version );
        if ( versionMatcher.find() && versionMatcher.end() == version.length() )
        {
            String unlockedVersion = versionMatcher.replaceFirst( "-SNAPSHOT" );
            if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
                                                 unlockedVersion, getProject().getModel() ) )
            {
                getLog().info( "Unlocked " + toString( dep ) + " to version " + unlockedVersion );
            }
        }
    }
}
 
Example #17
Source File: CompareDependenciesMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the properties holding a version if necessary.
 */
private List<String> updatePropertyVersions( ModifiedPomXMLEventReader pom,
                                             Map<Property, PropertyVersions> versionProperties,
                                             Map<String, Dependency> remoteDependencies )
    throws XMLStreamException
{
    List<String> result = new ArrayList<>();
    for ( Map.Entry<Property, PropertyVersions> entry : versionProperties.entrySet() )
    {
        Property property = entry.getKey();
        PropertyVersions version = entry.getValue();

        String candidateVersion = computeCandidateVersion( remoteDependencies, property, version );
        if ( candidateVersion != null )
        {
            String originalVersion = version.getAssociations()[0].getArtifact().getVersion(); // Yekes
            if ( !candidateVersion.equals( originalVersion ) ) // Update needed
            {
                result.add( writeDiffMessage( property.getName(), originalVersion, candidateVersion ).toString() );
                if ( !reportMode
                    && PomHelper.setPropertyVersion( pom, null, property.getName(), candidateVersion ) )
                {
                    getLog().info( "Updated ${" + property.getName() + "} from " + originalVersion + " to "
                        + candidateVersion );
                }
            }
        }
    }
    return result;
}
 
Example #18
Source File: PluginVersionChanger.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void apply( VersionChange versionChange )
    throws XMLStreamException
{
    if ( PomHelper.setPluginVersion( getPom(), versionChange.getGroupId(), versionChange.getArtifactId(),
                                     versionChange.getOldVersion(), versionChange.getNewVersion() ) )
    {
        info( "    Updating plugin " + versionChange.getGroupId() + ":" + versionChange.getArtifactId() );
        info( "        from version " + versionChange.getOldVersion() + " to " + versionChange.getNewVersion() );
    }
}
 
Example #19
Source File: DependencyVersionChanger.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void apply( VersionChange versionChange )
    throws XMLStreamException
{
    if ( PomHelper.setDependencyVersion( getPom(), versionChange.getGroupId(), versionChange.getArtifactId(),
                                         versionChange.getOldVersion(), versionChange.getNewVersion(),
                                         getModel() ) )
    {
        info( "    Updating dependency " + versionChange.getGroupId() + ":" + versionChange.getArtifactId() );
        info( "        from version " + versionChange.getOldVersion() + " to " + versionChange.getNewVersion() );
    }
}
 
Example #20
Source File: UseReleasesMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void rangeMatching( ModifiedPomXMLEventReader pom, Dependency dep, String version, String releaseVersion,
                            ArtifactVersions versions )
    throws XMLStreamException
{
    ArtifactVersion finalVersion = null;
    for ( ArtifactVersion proposedVersion : versions.getVersions( false ) )
    {
        if ( proposedVersion.toString().startsWith( releaseVersion ) )
        {
            getLog().debug( "Found matching version for " + toString( dep ) + " to version " + releaseVersion );
            finalVersion = proposedVersion;
        }
    }

    if ( finalVersion != null )
    {
        if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
                                             finalVersion.toString(), getProject().getModel() ) )
        {
            getLog().info( "Updated " + toString( dep ) + " to version " + finalVersion.toString() );
        }
    }
    else
    {
        getLog().info( "No matching release of " + toString( dep ) + " to update via rangeMatching." );
        if ( failIfNotReplaced )
        {
            throw new NoSuchElementException( "No matching release of " + toString( dep )
                + " found for update via rangeMatching." );
        }
    }
}
 
Example #21
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 #22
Source File: POM.java    From pomutils with Apache License 2.0 5 votes vote down vote up
private void initialize() throws IOException, XMLStreamException {
	StringBuilder input = new StringBuilder(FileUtils.fileRead(pomFile));
	pom = new ModifiedPomXMLEventReader(input, XML_INPUT_FACTORY);
	rawModel = PomHelper.getRawModel(pom);
	projectIdentifier = calculateProjectIdentifier();
	projectVersion = rawModel.getVersion();
	parentVersion = rawModel.getParent() != null
	        ? rawModel.getParent().getVersion()
	        : null;
	scmTag = rawModel.getScm() != null
			? rawModel.getScm().getTag()
			: null;
}
 
Example #23
Source File: POM.java    From pomutils with Apache License 2.0 5 votes vote down vote up
public void setPropertyToValue(String profileId, String property, String newPropertyValue) throws XMLStreamException, IOException {
	if (property == null) {
		logger.debug("Property is null, nothing to do.");
		return;
	}
	if (newPropertyValue == null) {
		logger.debug("newPropertyValue of property [{}] is null, nothing to do.", property);
		return;
	}

	if (profileId == null && newPropertyValue.equals(getProperties().getProperty(property))) {
		return;
	}

	if (profileId != null && newPropertyValue.equals(getProfileProperties(profileId).getProperty(property))) {
		return;
	}

	if (profileId == null) {
		logger.debug("Adjusting property  [{}] from [{}] to [{}] of [{}]", property, getProperties().getProperty(property), newPropertyValue, getPath());

	} else {
		logger.debug("Adjusting property [{}] from [{}] to [{}] of profile [{}] of [{}]", property, getProperties().getProperty(property),
		        newPropertyValue,
		        profileId, getPath());
	}
	boolean propertyChanged = PomHelper.setPropertyVersion(pom, profileId, property, newPropertyValue);
	if (propertyChanged) {
		changed = true;
		rawModel = PomHelper.getRawModel(pom);
	}
}
 
Example #24
Source File: UseReactorMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void useReactor( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
    throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{

    for ( Dependency dep : dependencies )
    {
        Artifact artifact = this.toArtifact( dep );
        if ( !isIncluded( artifact ) )
        {
            continue;
        }

        for ( Object reactorProject : reactorProjects )
        {
            MavenProject project = (MavenProject) reactorProject;
            if ( StringUtils.equals( project.getGroupId(), dep.getGroupId() )
                && StringUtils.equals( project.getArtifactId(), dep.getArtifactId() )
                && !StringUtils.equals( project.getVersion(), dep.getVersion() ) )
            {
                if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
                                                     project.getVersion(), getProject().getModel() ) )
                {
                    getLog().info( "Updated " + toString( dep ) + " to version " + project.getVersion() );
                }
                break;
            }
        }
    }
}
 
Example #25
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 #26
Source File: LockSnapshotsMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void lockSnapshots( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
    throws XMLStreamException, MojoExecutionException
{
    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;
        }

        if ( !isIncluded( this.toArtifact( dep ) ) )
        {
            continue;
        }

        String version = dep.getVersion();
        Matcher versionMatcher = matchSnapshotRegex.matcher( version );
        if ( versionMatcher.find() && versionMatcher.end() == version.length() )
        {
            String lockedVersion = resolveSnapshotVersion( dep );
            if ( !version.equals( lockedVersion ) )
            {
                if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
                                                     lockedVersion, getProject().getModel() ) )
                {
                    getLog().info( "Locked " + toString( dep ) + " to version " + lockedVersion );
                }
            }
        }
    }
}
 
Example #27
Source File: PomUpdater.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
private boolean setPropertyVersion(String propertyName, String version) {
	try {
		if (StringUtils.isEmpty(version)) {
			this.log.warn(
					"Version for [" + propertyName + "] is empty. Will not set it");
			return false;
		}
		return PomHelper.setPropertyVersion(this.pom, null, propertyName, version);
	}
	catch (XMLStreamException e) {
		this.log.error("Exception occurred while trying to set property version", e);
		return false;
	}
}
 
Example #28
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 );
                }
            }
        }
    }
}
 
Example #29
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 #30
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 );

            }
        }

    }
}