Skip to content

Commit

Permalink
MVERSIONS-146, Added versions:update-property goal
Browse files Browse the repository at this point in the history
  • Loading branch information
epabst committed Mar 16, 2011
1 parent a7c56a1 commit 4d396ec
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
*.ser
*.class
*~
*.bak
#*.off
*.old

# eclipse conf file
.settings
.classpath
.project
.manager
.scala_dependencies

# idea
.idea
*.iws
*.ipr
*.iml

# building
target
build
null
tmp*
test-output
*.log
serverLog.xml

# other scm
.svn
.CVS
.hg*
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:update-property -Dproperty=api -DnewVersion=[2.0]
48 changes: 48 additions & 0 deletions mojo/versions-maven-plugin/src/it/it-update-property-001/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localhost</groupId>
<artifactId>it-001</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>update-properties via autolink with one property only</name>

<properties>
<api>1.0</api>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>${api}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.io.*;
import org.codehaus.plexus.util.FileUtils;

try
{
File file = new File( basedir, "pom.xml" );
String buf = FileUtils.fileRead( file, "UTF-8" );

if ( buf.indexOf( "<api>2.0</api>" ) < 0 )
{
System.err.println( "Version not updated to 2.0" );
return false;
}
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:update-property -Dproperty=api
48 changes: 48 additions & 0 deletions mojo/versions-maven-plugin/src/it/it-update-property-002/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localhost</groupId>
<artifactId>it-001</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>update-properties via autolink with one property only</name>

<properties>
<api>1.0</api>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>${api}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.io.*;
import org.codehaus.plexus.util.FileUtils;

try
{
File file = new File( basedir, "pom.xml" );
String buf = FileUtils.fileRead( file, "UTF-8" );

if ( buf.indexOf( "<api>3.0</api>" ) < 0 )
{
System.err.println( "Version not updated to 2.0" );
return false;
}
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.codehaus.mojo.versions;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.util.Iterator;
import java.util.Map;
import javax.xml.stream.XMLStreamException;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
* Sets a property to the latest version in a given range of associated artifacts.
*
* @author Eric Pabst
* @goal update-property
* @requiresProject true
* @requiresDirectInvocation true
* @since 1.3
*/
public class UpdatePropertyMojo
extends AbstractVersionsUpdaterMojo
{

// ------------------------------ FIELDS ------------------------------

/**
* A property to update.
*
* @parameter expression="${property}"
* @since 1.3
*/
private String property = null;

/**
* The new version to set the property to (can be a version range to find a version within).
*
* @parameter expression="${newVersion}"
* @since 1.3
*/
private String newVersion = null;

/**
* Whether properties linking versions should be auto-detected or not.
*
* @parameter expression="${autoLinkItems}" defaultValue="true"
* @since 1.0-alpha-2
*/
private Boolean autoLinkItems;

// -------------------------- STATIC METHODS --------------------------

// -------------------------- OTHER METHODS --------------------------

/**
* @param pom the pom to update.
* @throws MojoExecutionException when things go wrong
* @throws MojoFailureException when things go wrong in a very bad way
* @throws XMLStreamException when things go wrong with XML streaming
* @see AbstractVersionsUpdaterMojo#update(ModifiedPomXMLEventReader)
* @since 1.0-alpha-1
*/
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException
{
Property propertyConfig = new Property(property);
propertyConfig.setVersion(newVersion);
Map propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), new Property[]{propertyConfig}, property, "",
!Boolean.FALSE.equals( autoLinkItems ) );
Iterator i = propertyVersions.entrySet().iterator();
while ( i.hasNext() )
{
Map.Entry/*<Property,PropertyVersions>*/ entry = (Map.Entry/*<Property,PropertyVersions>*/) i.next();
Property property = (Property) entry.getKey();
PropertyVersions version = (PropertyVersions) entry.getValue();

final String currentVersion = getProject().getProperties().getProperty( property.getName() );
if ( currentVersion == null )
{
continue;
}

ArtifactVersion winner =
version.getNewestVersion( currentVersion, property, this.allowSnapshots, this.reactorProjects,
this.getHelper() );

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 );
}

}
}

}

0 comments on commit 4d396ec

Please sign in to comment.