diff --git a/README.md b/README.md index e7bf9c6..dd4413d 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ TODO: * Finalize strategy API * Add APT for doc/site * find out if anyone else cares about this. -* filter new versions into MavenProject and model ( potential changes to parent, dependency, and plugin versions) +* filter new versions into MavenProject and model ( dependency, dependency management, plugin, etc) Other Thoughts --------------- diff --git a/maven-external-version-plugin/src/main/java/org/apache/maven/version/ExternalVersionExtension.java b/maven-external-version-plugin/src/main/java/org/apache/maven/version/ExternalVersionExtension.java index 7459ed5..6e28cd8 100644 --- a/maven-external-version-plugin/src/main/java/org/apache/maven/version/ExternalVersionExtension.java +++ b/maven-external-version-plugin/src/main/java/org/apache/maven/version/ExternalVersionExtension.java @@ -49,6 +49,8 @@ import java.io.IOException; import java.io.Reader; import java.io.Writer; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Pattern; /** @@ -89,6 +91,8 @@ public void afterProjectsRead( MavenSession session ) { logger.info( "About to change project version in reactor." ); + Map gavVersionMap = new HashMap(); + for ( MavenProject mavenProject : session.getAllProjects() ) { // Lookup this plugin's configuration @@ -117,14 +121,47 @@ public void afterProjectsRead( MavenSession session ) logger.info( "Updating project.build.finalName: " + newFinalName ); mavenProject.getBuild().setFinalName( newFinalName ); - // write processed new pom out - createNewVersionPom( mavenProject ); + gavVersionMap.put( buildGavKey( mavenProject.getGroupId(), mavenProject.getArtifactId(), oldVersion ), + newVersion ); + logger.info( + "new version added to map: " + buildGavKey( mavenProject.getGroupId(), mavenProject.getArtifactId(), + oldVersion ) + ": " + newVersion ); + + if ( mavenProject.getParent() != null ) + { + logger.info( "My parent is: " + buildGavKey( mavenProject.getParent() ) ); + } + } catch ( ExternalVersionException e ) { throw new MavenExecutionException( e.getMessage(), e ); } + } + + // now we have only updated the versions of the projects, we need to update + // the references between the updated projects + + for ( MavenProject mavenProject : session.getAllProjects() ) + { + try + { + + if ( mavenProject.getParent() != null ) + { + logger.info( "looking for parent in map" ); + + if ( gavVersionMap.containsKey( buildGavKey( mavenProject.getParent() ) ) ) + { + // we need to update the parent + logger.info( "WE NEED TO ACT NOW!" ); + } + } + + // write processed new pom out + createNewVersionPom( mavenProject, gavVersionMap ); + } catch ( XmlPullParserException e ) { throw new MavenExecutionException( e.getMessage(), e ); @@ -133,10 +170,28 @@ public void afterProjectsRead( MavenSession session ) { throw new MavenExecutionException( e.getMessage(), e ); } + } + + } + + private String buildGavKey( MavenProject mavenProject ) + { + return buildGavKey( mavenProject.getGroupId(), mavenProject.getArtifactId(), mavenProject.getVersion() ); } - private void createNewVersionPom( MavenProject mavenProject ) + private String buildGavKey( MavenProject mavenProject, String oldVersion ) + { + return buildGavKey( mavenProject.getGroupId(), mavenProject.getArtifactId(), oldVersion ); + } + + private String buildGavKey( String groupId, String artifactId, String oldVersion ) + { + return new StringBuilder( groupId ).append( ":" ).append( artifactId ).append( ":" ).append( + oldVersion ).toString(); + } + + private void createNewVersionPom( MavenProject mavenProject, Map gavVersionMap ) throws IOException, XmlPullParserException { Reader fileReader = null; @@ -148,6 +203,18 @@ private void createNewVersionPom( MavenProject mavenProject ) model.setVersion( mavenProject.getVersion() ); + // TODO: this needs to be restructured when other references are updated (dependencies, dep-management, plugins, etc) + if ( model.getParent() != null ) + { + String key = buildGavKey( model.getParent().getGroupId(), model.getParent().getArtifactId(), + model.getParent().getVersion() ); + String newVersionForParent = gavVersionMap.get( key ); + if ( newVersionForParent != null ) + { + model.getParent().setVersion( newVersionForParent ); + } + } + File newPom = new File( mavenProject.getBasedir(), "pom.xml.new-version" ); fileWriter = new FileWriter( newPom ); new MavenXpp3Writer().write( fileWriter, model ); @@ -158,6 +225,7 @@ private void createNewVersionPom( MavenProject mavenProject ) finally { IOUtil.close( fileReader ); + IOUtil.close( fileWriter ); }