From 66f606b5a9b18aa6b14599b23e90d8fd2ba0080f Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Fri, 26 Sep 2008 13:35:48 +0000 Subject: [PATCH] o Added new IT plugin to test things that involve dependency resolution git-svn-id: https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk@699315 13f79535-47bb-0310-9956-ffa450edef68 --- .../pom.xml | 62 ++++++ .../plugin/coreit/AbstractDependencyMojo.java | 178 ++++++++++++++++++ .../maven/plugin/coreit/CompileMojo.java | 76 ++++++++ .../maven/plugin/coreit/RuntimeMojo.java | 76 ++++++++ .../apache/maven/plugin/coreit/TestMojo.java | 76 ++++++++ its/core-it-support/core-it-plugins/pom.xml | 1 + 6 files changed, 469 insertions(+) create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/pom.xml create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/AbstractDependencyMojo.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/CompileMojo.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/RuntimeMojo.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/TestMojo.java diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/pom.xml b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/pom.xml new file mode 100644 index 0000000000..5a5fafa954 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/pom.xml @@ -0,0 +1,62 @@ + + + + + + 4.0.0 + + + maven-it-plugins + org.apache.maven.its.plugins + 2.1-SNAPSHOT + + + maven-it-plugin-dependency-resolution + maven-plugin + + Maven Integration Test Plugin :: Dependency Resolution + + A test plugin that provides several goals which employ @requiresDependencyResolution with different scopes. If + desired, the resulting class path or artifact identifiers can be written to a text file. + + 2008 + + + true + + + + + org.apache.maven + maven-plugin-api + 2.0 + + + org.apache.maven + maven-project + 2.0 + + + org.apache.maven + maven-artifact + 2.0 + + + diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/AbstractDependencyMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/AbstractDependencyMojo.java new file mode 100644 index 0000000000..99bbcc6a08 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/AbstractDependencyMojo.java @@ -0,0 +1,178 @@ +package org.apache.maven.plugin.coreit; + +/* + * 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 org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.util.Iterator; +import java.util.List; + +/** + * Provides common services for all mojos of this plugin. + * + * @author Benjamin Bentmann + * @version $Id$ + */ +public abstract class AbstractDependencyMojo + extends AbstractMojo +{ + + /** + * The current Maven project. + * + * @parameter default-value="${project}" + * @required + * @readonly + */ + protected MavenProject project; + + /** + * Writes the specified artifacts to the given output file. + * + * @param pathname The path to the output file, relative to the project base directory, may be null or + * empty if the output file should not be written. + * @param artifacts The list of artifacts to write to the file, may be null. + * @throws MojoExecutionException If the output file could not be written. + */ + protected void writeArtifacts( String pathname, List artifacts ) + throws MojoExecutionException + { + if ( pathname == null || pathname.length() <= 0 ) + { + return; + } + + // NOTE: We don't want to test path translation here so resolve relative path manually for robustness + File file = new File( pathname ); + if ( !file.isAbsolute() ) + { + file = new File( project.getBasedir(), pathname ); + } + + getLog().info( "[MAVEN-CORE-IT-LOG] Dumping artifact list: " + file ); + + BufferedWriter writer = null; + try + { + file.getParentFile().mkdirs(); + + writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ) ); + + if ( artifacts != null ) + { + for ( Iterator it = artifacts.iterator(); it.hasNext(); ) + { + Artifact artifact = (Artifact) it.next(); + writer.write( artifact.getId() ); + writer.newLine(); + } + } + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Failed to write artifact list", e ); + } + finally + { + if ( writer != null ) + { + try + { + writer.close(); + } + catch ( IOException e ) + { + // just ignore + } + } + } + } + + /** + * Writes the specified class path elements to the given output file. + * + * @param pathname The path to the output file, relative to the project base directory, may be null or + * empty if the output file should not be written. + * @param classPath The list of class path elements to write to the file, may be null. + * @throws MojoExecutionException If the output file could not be written. + */ + protected void writeClassPath( String pathname, List classPath ) + throws MojoExecutionException + { + if ( pathname == null || pathname.length() <= 0 ) + { + return; + } + + // NOTE: We don't want to test path translation here so resolve relative path manually for robustness + File file = new File( pathname ); + if ( !file.isAbsolute() ) + { + file = new File( project.getBasedir(), pathname ); + } + + getLog().info( "[MAVEN-CORE-IT-LOG] Dumping class path: " + file ); + + BufferedWriter writer = null; + try + { + file.getParentFile().mkdirs(); + + writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ) ); + + if ( classPath != null ) + { + for ( Iterator it = classPath.iterator(); it.hasNext(); ) + { + Object element = it.next(); + writer.write( element.toString() ); + writer.newLine(); + } + } + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Failed to write class path list", e ); + } + finally + { + if ( writer != null ) + { + try + { + writer.close(); + } + catch ( IOException e ) + { + // just ignore + } + } + } + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/CompileMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/CompileMojo.java new file mode 100644 index 0000000000..7f5074ce36 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/CompileMojo.java @@ -0,0 +1,76 @@ +package org.apache.maven.plugin.coreit; + +/* + * 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 org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Creates text files that list the dependencies with scope compile in the order returned from the Maven core. + * + * @goal compile + * @requiresDependencyResolution compile + * + * @author Benjamin Bentmann + * @version $Id$ + */ +public class CompileMojo + extends AbstractDependencyMojo +{ + + /** + * The path to the output file for the compile artifacts, relative to the project base directory. Each line of this + * UTF-8 encoded file specifies an artifact identifier. If not specified, the artifact list will not be written to + * disk. + * + * @parameter + */ + private String compileArtifacts; + + /** + * The path to the output file for the compile class path, relative to the project base directory. Each line of + * this UTF-8 encoded file specifies the absolute path to a class path element. If not specified, the class path + * will not be written to disk. + * + * @parameter + */ + private String compileClassPath; + + /** + * Runs this mojo. + * + * @throws MojoExecutionException If the output file could not be created or any dependency could not be resolved. + */ + public void execute() + throws MojoExecutionException + { + try + { + writeArtifacts( compileArtifacts, project.getCompileArtifacts() ); + writeClassPath( compileClassPath, project.getCompileClasspathElements() ); + } + catch ( DependencyResolutionRequiredException e ) + { + throw new MojoExecutionException( "Failed to resolve dependencies", e ); + } + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/RuntimeMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/RuntimeMojo.java new file mode 100644 index 0000000000..9df6f969a9 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/RuntimeMojo.java @@ -0,0 +1,76 @@ +package org.apache.maven.plugin.coreit; + +/* + * 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 org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Creates text files that list the dependencies with scope runtime in the order returned from the Maven core. + * + * @goal runtime + * @requiresDependencyResolution runtime + * + * @author Benjamin Bentmann + * @version $Id$ + */ +public class RuntimeMojo + extends AbstractDependencyMojo +{ + + /** + * The path to the output file for the runtime artifacts, relative to the project base directory. Each line of this + * UTF-8 encoded file specifies an artifact identifier. If not specified, the artifact list will not be written to + * disk. + * + * @parameter + */ + private String runtimeArtifacts; + + /** + * The path to the output file for the runtime class path, relative to the project base directory. Each line of + * this UTF-8 encoded file specifies the absolute path to a class path element. If not specified, the class path + * will not be written to disk. + * + * @parameter + */ + private String runtimeClassPath; + + /** + * Runs this mojo. + * + * @throws MojoExecutionException If the output file could not be created or any dependency could not be resolved. + */ + public void execute() + throws MojoExecutionException + { + try + { + writeArtifacts( runtimeArtifacts, project.getRuntimeArtifacts() ); + writeClassPath( runtimeClassPath, project.getRuntimeClasspathElements() ); + } + catch ( DependencyResolutionRequiredException e ) + { + throw new MojoExecutionException( "Failed to resolve dependencies", e ); + } + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/TestMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/TestMojo.java new file mode 100644 index 0000000000..dec1c91fed --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-dependency-resolution/src/main/java/org/apache/maven/plugin/coreit/TestMojo.java @@ -0,0 +1,76 @@ +package org.apache.maven.plugin.coreit; + +/* + * 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 org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Creates text files that list the dependencies with scope test in the order returned from the Maven core. + * + * @goal test + * @requiresDependencyResolution test + * + * @author Benjamin Bentmann + * @version $Id$ + */ +public class TestMojo + extends AbstractDependencyMojo +{ + + /** + * The path to the output file for the test artifacts, relative to the project base directory. Each line of this + * UTF-8 encoded file specifies an artifact identifier. If not specified, the artifact list will not be written to + * disk. + * + * @parameter + */ + private String testArtifacts; + + /** + * The path to the output file for the test class path, relative to the project base directory. Each line of + * this UTF-8 encoded file specifies the absolute path to a class path element. If not specified, the class path + * will not be written to disk. + * + * @parameter + */ + private String testClassPath; + + /** + * Runs this mojo. + * + * @throws MojoExecutionException If the output file could not be created or any dependency could not be resolved. + */ + public void execute() + throws MojoExecutionException + { + try + { + writeArtifacts( testArtifacts, project.getTestArtifacts() ); + writeClassPath( testClassPath, project.getTestClasspathElements() ); + } + catch ( DependencyResolutionRequiredException e ) + { + throw new MojoExecutionException( "Failed to resolve dependencies", e ); + } + } + +} diff --git a/its/core-it-support/core-it-plugins/pom.xml b/its/core-it-support/core-it-plugins/pom.xml index e0f3453f68..aa567b35de 100644 --- a/its/core-it-support/core-it-plugins/pom.xml +++ b/its/core-it-support/core-it-plugins/pom.xml @@ -38,6 +38,7 @@ under the License. maven-it-plugin-configuration maven-it-plugin-context-passing maven-it-plugin-core-stubs + maven-it-plugin-dependency-resolution maven-it-plugin-file maven-it-plugin-fork maven-it-plugin-generate-envar-properties