Author: markh Date: Tue May 22 11:38:54 2012 New Revision: 1341416 URL: http://svn.apache.org/viewvc?rev=1341416&view=rev Log: [MSHARED-165] Mediate dependency versions for stricter Maven3 ProjectSorter
o Set all project dependency versions to the corresponding version discovered at runtime now that Maven3's ProjectSorter considers versions Added: maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenProjectUtils.java (with props) maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/ maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/pom.xml (with props) maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project1/ maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project1/pom.xml (with props) maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project2/ maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project2/pom.xml (with props) maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project3/ maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project3/pom.xml (with props) Modified: maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/XMLMavenRuntimeVisitor.java maven/shared/trunk/maven-runtime/src/test/java/org/apache/maven/shared/runtime/DefaultMavenRuntimeTest.java Added: maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenProjectUtils.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenProjectUtils.java?rev=1341416&view=auto ============================================================================== --- maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenProjectUtils.java (added) +++ maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenProjectUtils.java Tue May 22 11:38:54 2012 @@ -0,0 +1,108 @@ +package org.apache.maven.shared.runtime; + +/* + * 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.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.maven.artifact.ArtifactUtils; +import org.apache.maven.model.Dependency; +import org.apache.maven.project.MavenProject; + +/** + * Utility methods for working with Maven projects. + * + * @author <a href="mailto:ma...@apache.org">Mark Hobson</a> + * @version $Id$ + * @see MavenProject + */ +final class MavenProjectUtils +{ + // constructors ----------------------------------------------------------- + + private MavenProjectUtils() + { + throw new AssertionError(); + } + + // public methods --------------------------------------------------------- + + /** + * Aligns dependency versions to their corresponding project version for the specified projects. + * + * @param projects the projects whose dependency versions to align + */ + public static void mediateDependencyVersions( List<MavenProject> projects ) + { + Map<String, String> versionsByProjectId = getVersionsByProjectId( projects ); + + for ( MavenProject project : projects ) + { + mediateProject( project, versionsByProjectId ); + } + } + + // private methods -------------------------------------------------------- + + private static void mediateProject( MavenProject project, Map<String, String> versionsByProjectId ) + { + for ( Dependency dependency : project.getDependencies() ) + { + mediateDependency( dependency, versionsByProjectId ); + } + } + + private static void mediateDependency( Dependency dependency, Map<String, String> versionsByProjectId ) + { + String projectId = versionlessKey( dependency ); + String version = versionsByProjectId.get( projectId ); + + if ( version != null ) + { + dependency.setVersion( version ); + } + } + + private static Map<String, String> getVersionsByProjectId( List<MavenProject> projects ) + { + Map<String, String> versionsByProjectId = new HashMap<String, String>(); + + for ( MavenProject project : projects ) + { + String projectId = versionlessKey( project ); + String version = project.getVersion(); + + versionsByProjectId.put( projectId, version ); + } + + return versionsByProjectId; + } + + private static String versionlessKey( MavenProject project ) + { + return ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ); + } + + private static String versionlessKey( Dependency dependency ) + { + return ArtifactUtils.versionlessKey( dependency.getGroupId(), dependency.getArtifactId() ); + } +} Propchange: maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenProjectUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenProjectUtils.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/XMLMavenRuntimeVisitor.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/XMLMavenRuntimeVisitor.java?rev=1341416&r1=1341415&r2=1341416&view=diff ============================================================================== --- maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/XMLMavenRuntimeVisitor.java (original) +++ maven/shared/trunk/maven-runtime/src/main/java/org/apache/maven/shared/runtime/XMLMavenRuntimeVisitor.java Tue May 22 11:38:54 2012 @@ -103,9 +103,13 @@ class XMLMavenRuntimeVisitor implements */ public List<MavenProject> getSortedProjects() throws MavenRuntimeException { + // mediate dependency versions since declared versions can differ from runtime versions + List<MavenProject> mediatedProjects = new ArrayList<MavenProject>( projects ); + MavenProjectUtils.mediateDependencyVersions( mediatedProjects ); + try { - ProjectSorter projectSorter = new ProjectSorter( projects ); + ProjectSorter projectSorter = new ProjectSorter( mediatedProjects ); return genericList( projectSorter.getSortedProjects(), MavenProject.class ); } Modified: maven/shared/trunk/maven-runtime/src/test/java/org/apache/maven/shared/runtime/DefaultMavenRuntimeTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-runtime/src/test/java/org/apache/maven/shared/runtime/DefaultMavenRuntimeTest.java?rev=1341416&r1=1341415&r2=1341416&view=diff ============================================================================== --- maven/shared/trunk/maven-runtime/src/test/java/org/apache/maven/shared/runtime/DefaultMavenRuntimeTest.java (original) +++ maven/shared/trunk/maven-runtime/src/test/java/org/apache/maven/shared/runtime/DefaultMavenRuntimeTest.java Tue May 22 11:38:54 2012 @@ -77,6 +77,7 @@ public class DefaultMavenRuntimeTest ext packageProject( "testMultipleJars/project2/pom.xml" ); packageProject( "testMultipleJars/project3/pom.xml" ); packageProject( "testDependentJars/pom.xml" ); + packageProject( "testDependentJars2/pom.xml" ); initialized = true; } @@ -588,6 +589,25 @@ public class DefaultMavenRuntimeTest ext }, projects ); } + // MSHARED-165 + public void testGetSortedProjectsWithMediatedDependency() + throws MavenRuntimeException, IOException + { + File jar1 = getPackage( "testDependentJars2/project1/pom.xml" ); + File jar2 = getPackage( "testDependentJars/project2/pom.xml" ); + File jar3 = getPackage( "testDependentJars/project3/pom.xml" ); + + URLClassLoader classLoader = newClassLoader( new File[] { jar1, jar2, jar3 } ); + + List<MavenProject> projects = mavenRuntime.getSortedProjects( classLoader ); + + assertMavenProjects( new String[] { + "org.apache.maven.shared.runtime.tests:testDependentJars3:1.0", + "org.apache.maven.shared.runtime.tests:testDependentJars1:2.0", + "org.apache.maven.shared.runtime.tests:testDependentJars2:1.0" + }, projects ); + } + public void testGetSortedProjectsWithMultipleVersions() throws MavenRuntimeException, IOException { Added: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/pom.xml URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/pom.xml?rev=1341416&view=auto ============================================================================== --- maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/pom.xml (added) +++ maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/pom.xml Tue May 22 11:38:54 2012 @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ 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. + --> + +<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>org.apache.maven.shared.runtime.tests</groupId> + <artifactId>testDependentJars</artifactId> + <packaging>pom</packaging> + <version>2.0</version> + + <modules> + <module>project1</module> + <module>project2</module> + <module>project3</module> + </modules> + +</project> Propchange: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project1/pom.xml URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project1/pom.xml?rev=1341416&view=auto ============================================================================== --- maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project1/pom.xml (added) +++ maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project1/pom.xml Tue May 22 11:38:54 2012 @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ 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. + --> + +<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>org.apache.maven.shared.runtime.tests</groupId> + <artifactId>testDependentJars1</artifactId> + <packaging>jar</packaging> + <version>2.0</version> + + <dependencies> + <dependency> + <groupId>org.apache.maven.shared.runtime.tests</groupId> + <artifactId>testDependentJars3</artifactId> + <version>2.0</version> + </dependency> + </dependencies> + +</project> Propchange: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project1/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project1/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project2/pom.xml URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project2/pom.xml?rev=1341416&view=auto ============================================================================== --- maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project2/pom.xml (added) +++ maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project2/pom.xml Tue May 22 11:38:54 2012 @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ 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. + --> + +<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>org.apache.maven.shared.runtime.tests</groupId> + <artifactId>testDependentJars2</artifactId> + <packaging>jar</packaging> + <version>2.0</version> + + <dependencies> + <dependency> + <groupId>org.apache.maven.shared.runtime.tests</groupId> + <artifactId>testDependentJars1</artifactId> + <version>2.0</version> + </dependency> + </dependencies> + +</project> Propchange: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project2/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project2/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project3/pom.xml URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project3/pom.xml?rev=1341416&view=auto ============================================================================== --- maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project3/pom.xml (added) +++ maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project3/pom.xml Tue May 22 11:38:54 2012 @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ 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. + --> + +<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>org.apache.maven.shared.runtime.tests</groupId> + <artifactId>testDependentJars3</artifactId> + <packaging>jar</packaging> + <version>2.0</version> + +</project> Propchange: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project3/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-runtime/src/test/resources/testDependentJars2/project3/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision