Author: bentmann Date: Fri Aug 15 14:11:32 2008 New Revision: 686368 URL: http://svn.apache.org/viewvc?rev=686368&view=rev Log: [MINVOKER-56] Clone all projects which are locally reachable from the invoked IT project
Added: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/pom.xml (with props) maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/aggregator/ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/aggregator/pom.xml (with props) maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-a/ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-a/pom.xml (with props) maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-b/ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-b/pom.xml (with props) maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/parent/ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/parent/pom.xml (with props) Modified: maven/plugins/trunk/maven-invoker-plugin/pom.xml maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java Modified: maven/plugins/trunk/maven-invoker-plugin/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/pom.xml?rev=686368&r1=686367&r2=686368&view=diff ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/pom.xml (original) +++ maven/plugins/trunk/maven-invoker-plugin/pom.xml Fri Aug 15 14:11:32 2008 @@ -63,6 +63,11 @@ </dependency> <dependency> <groupId>org.apache.maven</groupId> + <artifactId>maven-model</artifactId> + <version>2.0.6</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0.6</version> </dependency> @@ -123,16 +128,17 @@ <artifactId>plexus-container-default</artifactId> <version>1.0-alpha-9-stable-1</version> </dependency> + <dependency> - <groupId>org.apache.maven.plugin-testing</groupId> - <artifactId>maven-plugin-testing-harness</artifactId> - <version>1.2</version> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.2</version> <scope>test</scope> </dependency> <dependency> - <groupId>org.apache.maven</groupId> - <artifactId>maven-model</artifactId> - <version>2.0.6</version> + <groupId>org.apache.maven.plugin-testing</groupId> + <artifactId>maven-plugin-testing-harness</artifactId> + <version>1.2</version> <scope>test</scope> </dependency> </dependencies> Added: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/pom.xml?rev=686368&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/pom.xml (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/pom.xml Fri Aug 15 14:11:32 2008 @@ -0,0 +1,66 @@ +<?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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.plugins.invoker</groupId> + <artifactId>project-cloning-reactor</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>pom</packaging> + + <description>Test to check that project cloning includes all parents/modules of the invoked project and not only sub directories.</description> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-invoker-plugin</artifactId> + <version>@pom.version@</version> + <configuration> + <projectsDirectory>src/it</projectsDirectory> + <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo> + <pomIncludes> + <!-- only invoke this one which requires all the other projects to be cloned as well --> + <pomInclude>aggregator/pom.xml</pomInclude> + </pomIncludes> + <goals> + <goal>validate</goal> + </goals> + </configuration> + <executions> + <execution> + <id>integration-test</id> + <goals> + <goal>run</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/aggregator/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/aggregator/pom.xml?rev=686368&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/aggregator/pom.xml (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/aggregator/pom.xml Fri Aug 15 14:11:32 2008 @@ -0,0 +1,36 @@ +<?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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>test</groupId> + <artifactId>aggregator</artifactId> + <version>0.1-SNAPSHOT</version> + <packaging>pom</packaging> + + <modules> + <module>../module-a</module> + <module>../module-b/pom.xml</module> + </modules> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> +</project> Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/aggregator/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/aggregator/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-a/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-a/pom.xml?rev=686368&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-a/pom.xml (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-a/pom.xml Fri Aug 15 14:11:32 2008 @@ -0,0 +1,38 @@ +<?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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>test</groupId> + <artifactId>parent</artifactId> + <version>0.1-SNAPSHOT</version> + <relativePath>../parent</relativePath> + </parent> + + <groupId>test</groupId> + <artifactId>module-a</artifactId> + <version>0.1-SNAPSHOT</version> + <packaging>pom</packaging> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> +</project> Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-a/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-a/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-b/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-b/pom.xml?rev=686368&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-b/pom.xml (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-b/pom.xml Fri Aug 15 14:11:32 2008 @@ -0,0 +1,38 @@ +<?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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>test</groupId> + <artifactId>parent</artifactId> + <version>0.1-SNAPSHOT</version> + <relativePath>../parent</relativePath> + </parent> + + <groupId>test</groupId> + <artifactId>module-b</artifactId> + <version>0.1-SNAPSHOT</version> + <packaging>pom</packaging> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> +</project> Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-b/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/module-b/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/parent/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/parent/pom.xml?rev=686368&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/parent/pom.xml (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/parent/pom.xml Fri Aug 15 14:11:32 2008 @@ -0,0 +1,30 @@ +<?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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>test</groupId> + <artifactId>parent</artifactId> + <version>0.1-SNAPSHOT</version> + <packaging>pom</packaging> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> +</project> Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/parent/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-invoker-plugin/src/it/project-cloning-reactor/src/it/parent/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java?rev=686368&r1=686367&r2=686368&view=diff ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java (original) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java Fri Aug 15 14:11:32 2008 @@ -29,10 +29,12 @@ import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -40,6 +42,8 @@ import java.util.StringTokenizer; import java.util.TreeSet; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -485,13 +489,19 @@ scriptInterpreters.put( "bsh", new BeanShellScriptInterpreter() ); scriptInterpreters.put( "groovy", new GroovyScriptInterpreter() ); + Collection collectedProjects = new LinkedHashSet(); + for ( int i = 0; i < includedPoms.length; i++ ) + { + collectProjects( projectsDirectory, includedPoms[i], collectedProjects, true ); + } + File projectsDir = projectsDirectory; if ( cloneProjectsTo != null ) { try { - cloneProjects( includedPoms ); + cloneProjects( collectedProjects ); // enable in-place filtering if ( !cloneProjectsTo.getCanonicalFile().equals( projectsDirectory.getCanonicalFile() ) ) @@ -561,34 +571,135 @@ } /** - * Copies the specified IT projects to the directory given by [EMAIL PROTECTED] #cloneProjectsTo}. A project may either be - * denoted by a path to a POM file or merely by a path to a base directory. + * Collects all projects locally reachable from the specified project. The method will as such try to read the POM + * and recursively follow its parent/module elements. * - * @param includedProjects The paths to the IT projects, relative to the projects directory, must not be - * <code>null</code>. - * @throws IOException The the projects could not be copied. + * @param projectsDir The base directory of all projects, must not be <code>null</code>. + * @param projectPath The relative path of the current project, can denote either the POM or its base directory, + * must not be <code>null</code>. + * @param projectPaths The set of already collected projects to add new projects to, must not be <code>null</code>. + * @param included A flag indicating whether the specified project has been explicitly included via the parameter + * [EMAIL PROTECTED] #pomIncludes}. Such projects will always be added to the result set even if there is no + * corresponding POM. + * @throws MojoExecutionException If the project tree could not be traversed. */ - private void cloneProjects( String[] includedProjects ) + private void collectProjects( File projectsDir, String projectPath, Collection projectPaths, boolean included ) + throws MojoExecutionException + { + projectPath = projectPath.replace( '\\', '/' ); + File pomFile = new File( projectsDir, projectPath ); + if ( pomFile.isDirectory() ) + { + pomFile = new File( pomFile, "pom.xml" ); + if ( !pomFile.exists() ) + { + if ( included ) + { + projectPaths.add( projectPath ); + } + return; + } + if ( !projectPath.endsWith( "/" ) ) + { + projectPath += '/'; + } + projectPath += "pom.xml"; + } + else if ( !pomFile.isFile() ) + { + return; + } + if ( !projectPaths.add( projectPath ) ) + { + return; + } + + Model model; + + Reader reader = null; + try + { + reader = ReaderFactory.newXmlReader( pomFile ); + model = new MavenXpp3Reader().read( reader ); + } + catch ( Exception e ) + { + throw new MojoExecutionException( "Failed to parse POM: " + pomFile, e ); + } + finally + { + IOUtil.close( reader ); + } + + try + { + String projectsRoot = projectsDir.getCanonicalPath(); + String projectDir = pomFile.getParent(); + + String parentPath = "../pom.xml"; + if ( model.getParent() != null && StringUtils.isNotEmpty( model.getParent().getRelativePath() ) ) + { + parentPath = model.getParent().getRelativePath(); + } + String parent = normalizePath( new File( projectDir, parentPath ), projectsRoot ); + if ( parent != null ) + { + collectProjects( projectsDir, parent, projectPaths, false ); + } + + if ( model.getModules() != null ) + { + for ( Iterator it = model.getModules().iterator(); it.hasNext(); ) + { + String modulePath = (String) it.next(); + String module = normalizePath( new File( projectDir, modulePath ), projectsRoot ); + if ( module != null ) + { + collectProjects( projectsDir, module, projectPaths, false ); + } + } + } + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Failed to analyze POM: " + pomFile, e ); + } + } + + /** + * Copies the specified projects to the directory given by [EMAIL PROTECTED] #cloneProjectsTo}. A project may either be denoted + * by a path to a POM file or merely by a path to a base directory. + * + * @param projectPaths The paths to the projects to clone, relative to the projects directory, must not be + * <code>null</code> nor contain <code>null</code> elements. + * @throws IOException If the the projects could not be copied. + */ + private void cloneProjects( Collection projectPaths ) throws IOException { cloneProjectsTo.mkdirs(); + Collection dirs = new LinkedHashSet(); + for ( Iterator it = projectPaths.iterator(); it.hasNext(); ) + { + String projectPath = (String) it.next(); + if ( !new File( projectsDirectory, projectPath ).isDirectory() ) + { + projectPath = getParentPath( projectPath ); + } + dirs.add( projectPath ); + } + List clonedSubpaths = new ArrayList(); - for ( int i = 0; i < includedProjects.length; i++ ) + for ( Iterator it = dirs.iterator(); it.hasNext(); ) { - String subpath = includedProjects[i]; - if ( !new File( projectsDirectory, subpath ).isDirectory() ) + String subpath = (String) it.next(); + + // skip this project if its parent directory is also scheduled for cloning + if ( !".".equals( subpath ) && dirs.contains( getParentPath( subpath ) ) ) { - int lastSep = subpath.lastIndexOf( File.separator ); - if ( lastSep > -1 ) - { - subpath = subpath.substring( 0, lastSep ); - } - else - { - subpath = "."; - } + continue; } // avoid copying subdirs that are already cloned. @@ -629,6 +740,18 @@ } /** + * Gets the parent path of the specified relative path. + * + * @param path The relative path whose parent should be retrieved, must not be <code>null</code>. + * @return The parent path or "." if the specified path has no parent, never <code>null</code>. + */ + private String getParentPath( String path ) + { + int lastSep = Math.max( path.lastIndexOf( '/' ), path.lastIndexOf( '\\' ) ); + return ( lastSep < 0 ) ? "." : path.substring( 0, lastSep ); + } + + /** * Copied a directory structure with deafault exclusions (.svn, CVS, etc) * * @param sourceDir The source directory to copy, must not be <code>null</code>.