Author: rfscholte Date: Sat Aug 6 17:46:34 2011 New Revision: 1154560 URL: http://svn.apache.org/viewvc?rev=1154560&view=rev Log: Fix MECLIPSE-587: maven-eclipse-plugin creates wrong org.eclipse.wst.common.project.facet.core.xml for ear-projects when javaee:javaee-api is used
Modified: maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/ide/IdeUtils.java maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/ide/IdeUtilsTest.java Modified: maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/ide/IdeUtils.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/ide/IdeUtils.java?rev=1154560&r1=1154559&r2=1154560&view=diff ============================================================================== --- maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/ide/IdeUtils.java (original) +++ maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/ide/IdeUtils.java Sat Aug 6 17:46:34 2011 @@ -30,6 +30,8 @@ import org.apache.maven.artifact.reposit import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.model.Dependency; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginExecution; @@ -38,7 +40,6 @@ import org.apache.maven.plugin.eclipse.M import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.PropertyUtils; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; @@ -234,7 +235,14 @@ public class IdeUtils /** * Extracts the version of the first matching artifact in the given list. - * + * <p> + * The {@code len} parameter indicated what to to return: + * <ul> + * <li><strong>1</strong> indicated <code>major</code> version</li> + * <li><strong>3</strong> indicated <code>major dot minor</code> version</li> + * <li><strong>5 and above</strong> indicates <code>major dot minor dot incremental</code> version + * </ul> + * * @param artifactIds artifact names to compare against for extracting version * @param artifacts Set of artifacts for our project * @param len expected length of the version sub-string @@ -242,6 +250,44 @@ public class IdeUtils */ public static String getArtifactVersion( String[] artifactIds, List dependencies, int len ) { + String version = null; + ArtifactVersion artifactVersion = getArtifactVersion( artifactIds, dependencies ); + if ( artifactVersion != null ) + { + StringBuffer versionBuffer = new StringBuffer(); + if( len >= 1 ) + { + versionBuffer.append( artifactVersion.getMajorVersion() ); + } + if( len >= 2 ) + { + versionBuffer.append( '.' ); + } + if( len >= 3 ) + { + versionBuffer.append( artifactVersion.getMinorVersion() ); + } + if( len >= 4 ) + { + versionBuffer.append( '.' ); + } + if( len >= 5 ) + { + versionBuffer.append( artifactVersion.getIncrementalVersion() ); + } + version = versionBuffer.toString(); + } + return version; + } + + /** + * + * @param artifactIds an array of artifactIds, should not be <code>null</code> + * @param dependencies a list of {@link Dependency}-objects, should not be <code>null</code> + * @return the resolved ArtifactVersion, otherwise <code>null</code> + */ + public static ArtifactVersion getArtifactVersion( String[] artifactIds, List /*<Dependency>*/ dependencies ) + { for ( int j = 0; j < artifactIds.length; j++ ) { String id = artifactIds[j]; @@ -251,7 +297,7 @@ public class IdeUtils Dependency dep = (Dependency) depIter.next(); if ( id.equals( dep.getArtifactId() ) ) { - return StringUtils.substring( dep.getVersion(), 0, len ); + return VersionRange.createFromVersion( dep.getVersion() ).getRecommendedVersion(); } } Modified: maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/ide/IdeUtilsTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/ide/IdeUtilsTest.java?rev=1154560&r1=1154559&r2=1154560&view=diff ============================================================================== --- maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/ide/IdeUtilsTest.java (original) +++ maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/ide/IdeUtilsTest.java Sat Aug 6 17:46:34 2011 @@ -20,9 +20,11 @@ package org.apache.maven.plugin.ide; import java.io.File; import java.io.IOException; +import java.util.Collections; import junit.framework.TestCase; +import org.apache.maven.model.Dependency; import org.codehaus.plexus.util.Os; /** @@ -137,5 +139,38 @@ public class IdeUtilsTest assertEquals( expected, actual ); } + + public void testGetArtifactVersion() + { + Dependency dep = new Dependency(); + dep.setArtifactId( "artifactId" ); + + dep.setVersion( "5" ); + assertEquals( "5", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 1 ) ); + assertEquals( "5.0", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 3 ) ); + assertEquals( "5.0.0", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 5 ) ); + + dep.setVersion( "5.3" ); + assertEquals( "5", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 1 ) ); + assertEquals( "5.3", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 3 ) ); + assertEquals( "5.3.0", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 5 ) ); + + dep.setVersion( "5.3.1" ); + assertEquals( "5", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 1 ) ); + assertEquals( "5.3", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 3 ) ); + assertEquals( "5.3.1", + IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 5 ) ); + + assertNull( IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.EMPTY_LIST, 5 ) ); + assertNull( IdeUtils.getArtifactVersion( new String[0], Collections.singletonList( dep ), 5 ) ); + } }