From 425f632772c8415182f624a5293f1af6d6c68a30 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Fri, 12 Jun 2009 18:45:37 +0000 Subject: [PATCH] o Restored backward-compat with regard to handling of duplicate dependencies during metadata retrieval git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@784227 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/project/PomConstructionTest.java | 21 +++++++++++++++++-- .../normalization/DefaultModelNormalizer.java | 15 +++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java b/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java index 6e0dc6bcb5..fdc60361c3 100644 --- a/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java @@ -176,6 +176,14 @@ public class PomConstructionTest } */ + public void testDuplicateDependenciesCauseLastDeclarationToBePickedInLenientMode() + throws Exception + { + PomTestWrapper pom = buildPom( "dependencies-different-versions", true, null ); + assertEquals( 1, ( (List) pom.getValue( "dependencies" ) ).size() ); + assertEquals( "1.1", pom.getValue( "dependencies[1]/version" ) ); + } + /* MNG-3567*/ public void testParentInterpolation() throws Exception @@ -1573,6 +1581,13 @@ public class PomConstructionTest private PomTestWrapper buildPom( String pomPath, Properties executionProperties, String... profileIds ) throws ProjectBuildingException + { + return buildPom( pomPath, false, executionProperties, profileIds ); + } + + private PomTestWrapper buildPom( String pomPath, boolean lenientValidation, Properties executionProperties, + String... profileIds ) + throws ProjectBuildingException { File pomFile = new File( testDirectory, pomPath ); if ( pomFile.isDirectory() ) @@ -1582,12 +1597,14 @@ public class PomConstructionTest ProjectBuildingRequest config = new DefaultProjectBuildingRequest(); - String localRepoUrl = System.getProperty( "maven.repo.local", System.getProperty( "user.home" ) + "/.m2/repository" ); + String localRepoUrl = + System.getProperty( "maven.repo.local", System.getProperty( "user.home" ) + "/.m2/repository" ); localRepoUrl = "file://" + localRepoUrl; config.setLocalRepository( new DefaultArtifactRepository( "local", localRepoUrl, new DefaultRepositoryLayout() ) ); config.setActiveProfileIds( Arrays.asList( profileIds ) ); config.setExecutionProperties( executionProperties ); - + config.setLenientValidation( lenientValidation ); + return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ) ); } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/normalization/DefaultModelNormalizer.java b/maven-model-builder/src/main/java/org/apache/maven/model/normalization/DefaultModelNormalizer.java index 4f6027a628..030b4e3c61 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/normalization/DefaultModelNormalizer.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/normalization/DefaultModelNormalizer.java @@ -67,6 +67,21 @@ public class DefaultModelNormalizer build.setPlugins( new ArrayList( normalized.values() ) ); } + + if ( request.istLenientValidation() ) + { + /* + * NOTE: This is to keep backward-compat with Maven 2.x which did not validate that dependencies are unique + * within a single POM. Upon multiple declarations, 2.x just kept the last one. So when we're in lenient + * mode for metadata retrieval, we have to deal with such broken POMs and mimic the way 2.x works. + */ + Map dependencies = new LinkedHashMap(); + for ( Dependency dependency : model.getDependencies() ) + { + dependencies.put( dependency.getManagementKey(), dependency ); + } + model.setDependencies( new ArrayList( dependencies.values() ) ); + } } private static class DuplicateMerger