Author: brianf Date: Sun Jan 14 15:20:49 2007 New Revision: 496189 URL: http://svn.apache.org/viewvc?view=rev&rev=496189 Log: MDEP-26 - applied patch from Anagnostopoulos Kostis - writes out the classpath to a file that can be consumed by java -cp
Added: maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/BuildClasspathMojo.java maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestBuildClasspathMojo.java maven/plugins/trunk/maven-dependency-plugin/src/test/resources/unit/build-classpath-test/ maven/plugins/trunk/maven-dependency-plugin/src/test/resources/unit/build-classpath-test/plugin-config.xml (with props) Modified: maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestUnpackMojo.java Added: maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/BuildClasspathMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/BuildClasspathMojo.java?view=auto&rev=496189 ============================================================================== --- maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/BuildClasspathMojo.java (added) +++ maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/BuildClasspathMojo.java Sun Jan 14 15:20:49 2007 @@ -0,0 +1,305 @@ +package org.apache.maven.plugin.dependency; + +/* + * 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.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.dependency.utils.DependencyUtil; +import org.apache.maven.plugin.dependency.utils.filters.ArtifactsFilter; + +/** + * Goal that copies the project dependencies from the repository to a defined + * location. + * + * @goal build-classpath + * @requiresDependencyResolution compile + * @phase process-sources + * @author ankostis + */ +public class BuildClasspathMojo + extends AbstractDependencyFilterMojo + implements Comparator +{ + + /** + * Strip artifact version during copy + * + * @parameter expression="${stripVersion}" default-value="false" + * @parameter + */ + private boolean stripVersion = false; + + /** + * The prefix to preppend on each dependent artifact. If undefined, the + * paths refer to the actual files store in the local repository (the + * stipVersion parameter does nothing then). + * + * @parameter expression="${maven.dep.prefix}" + */ + private String prefix; + + /** + * The file to write the classpath string. If undefined, it just prints the + * classpath as [INFO]. + * + * @parameter expression="${maven.dep.cpFile}" + */ + private File cpFile; + + /** + * If 'true', it skips the up-to-date-check, and always regenerates the + * classpath file. + * + * @parameter default-value="false" expression="${maven.dep.regenerateFile}" + */ + private boolean regenerateFile; + + /** + * Main entry into mojo. Gets the list of dependencies and iterates through + * calling copyArtifact. + * + * @throws MojoExecutionException + * with a message if an error occurs. + * + * @see #getDependencies + * @see #copyArtifact(Artifact, boolean) + */ + public void execute() + throws MojoExecutionException + { + Set artifacts = getResolvedDependencies( true ); + + if ( artifacts == null || artifacts.isEmpty() ) + { + getLog().info( "No dependencies found." ); + } + + List artList = new ArrayList( artifacts ); + + StringBuffer sb = new StringBuffer(); + Iterator i = artList.iterator(); + + if ( i.hasNext() ) + { + appendArtifactPath( (Artifact) i.next(), sb ); + + while ( i.hasNext() ) + { + sb.append( ":" ); + appendArtifactPath( (Artifact) i.next(), sb ); + } + } + + String cpString = sb.toString(); + + if ( cpFile == null ) + { + getLog().info( "Dependencies classpath:\n" + cpString ); + } + else + { + if ( regenerateFile || !isUpdToDate( cpString ) ) + { + storeClasspathFile( cpString ); + } + else + { + this.getLog().info( "Skipped writting classpath file '" + cpFile + "'. No changes found." ); + } + } + } + + /** + * Appends the artifact path into the specified stringBuffer. + * + * @param art + * @param sb + */ + protected void appendArtifactPath( Artifact art, StringBuffer sb ) + { + if ( prefix == null ) + { + sb.append( art.getFile() ); + } + else + { + // TODO: add param for prepending groupId and version. + sb.append( prefix ); + sb.append( File.separatorChar ); + sb.append( DependencyUtil.getFormattedFileName( art, this.stripVersion ) ); + } + } + + /** + * Checks that new classpath differs from that found inside the old + * classpathFile. + * + * @param cpString + * @return true if the specified classpath equals to that found inside the + * file, false otherwise (including when file does not exists but + * new classpath does). + */ + private boolean isUpdToDate( String cpString ) + { + try + { + String oldCp = readClasspathFile(); + return ( cpString == oldCp || ( cpString != null && cpString.equals( oldCp ) ) ); + } + catch ( Exception ex ) + { + this.getLog().warn( "Error while reading old classpath file '" + cpFile + "' for up-to-date check: " + ex ); + + return false; + } + } + + /** + * It stores the specified string into that file. + * + * @param cpString + * the string to be written into the file. + * @throws MojoExecutionException + */ + private void storeClasspathFile( String cpString ) + throws MojoExecutionException + { + try + { + Writer w = new BufferedWriter( new FileWriter( cpFile ) ); + + try + { + w.write( cpString ); + + getLog().info( "Written classpath file '" + cpFile + "'." ); + } + catch ( IOException ex ) + { + throw new MojoExecutionException( "Error while writting to classpath file '" + cpFile + "': " + + ex.toString(), ex ); + } + finally + { + w.close(); + } + } + catch ( IOException ex ) + { + throw new MojoExecutionException( "Error while opening/closing classpath file '" + cpFile + "': " + + ex.toString(), ex ); + } + } + + /** + * Reads into a string the file specified by the mojo param 'cpFile'. + * Assumes, the instance variable 'cpFile' is not null. + * + * @return the string contained in the classpathFile, if exists, or null + * ortherwise. + * @throws MojoExecutionException + */ + private String readClasspathFile() + throws IOException + { + if ( !cpFile.isFile() ) + { + return null; + } + StringBuffer sb = new StringBuffer(); + BufferedReader r = new BufferedReader( new FileReader( cpFile ) ); + + try + { + String l; + while ( ( l = r.readLine() ) != null ) + { + sb.append( l ); + } + + return sb.toString(); + } + finally + { + r.close(); + } + } + + /** + * Compares artifacts lexicographically, using pattern + * [group_id][artifact_id][version]. + * @param arg1 first object + * @param arg2 second object + * @return the value <code>0</code> if the argument string is equal to + * this string; a value less than <code>0</code> if this string + * is lexicographically less than the string argument; and a + * value greater than <code>0</code> if this string is + * lexicographically greater than the string argument. + */ + public int compare( Object arg1, Object arg2 ) + { + if ( arg1 instanceof Artifact && arg2 instanceof Artifact ) + { + if ( arg1 == arg2 ) + { + return 0; + } + else if ( arg1 == null ) + { + return -1; + } + else if ( arg2 == null ) + { + return +1; + } + + Artifact art1 = (Artifact) arg1; + Artifact art2 = (Artifact) arg2; + + String s1 = art1.getGroupId() + art1.getArtifactId() + art1.getVersion(); + String s2 = art2.getGroupId() + art2.getArtifactId() + art2.getVersion(); + + return s1.compareTo( s2 ); + } + else + { + return 0; + } + } + + protected ArtifactsFilter getMarkedArtifactFilter() + { + return null; + } +} Added: maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestBuildClasspathMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestBuildClasspathMojo.java?view=auto&rev=496189 ============================================================================== --- maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestBuildClasspathMojo.java (added) +++ maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestBuildClasspathMojo.java Sun Jan 14 15:20:49 2007 @@ -0,0 +1,68 @@ +package org.apache.maven.plugin.dependency; + +/* + * 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.io.File; +import java.util.Set; + +import org.apache.maven.plugin.dependency.resolvers.ResolveDependenciesMojo; +import org.apache.maven.plugin.dependency.utils.DependencyStatusSets; +import org.apache.maven.plugin.dependency.utils.SilentLog; +import org.apache.maven.project.MavenProject; + +public class TestBuildClasspathMojo + extends AbstractDependencyMojoTestCase +{ + + protected void setUp() + throws Exception + { + // required for mojo lookups to work + super.setUp( "build-classpath", true ); + } + + /** + * tests the proper discovery and configuration of the mojo + * + * @throws Exception + */ + public void testEnvironment() + throws Exception + { + File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" ); + BuildClasspathMojo mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom ); + + assertNotNull( mojo ); + assertNotNull( mojo.getProject() ); + MavenProject project = mojo.getProject(); + + //mojo.silent = true; + Set artifacts = this.stubFactory.getScopedArtifacts(); + Set directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts(); + artifacts.addAll( directArtifacts ); + + project.setArtifacts( artifacts ); + project.setDependencyArtifacts( directArtifacts ); + + mojo.execute(); + + } + +} \ No newline at end of file Modified: maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java?view=diff&rev=496189&r1=496188&r2=496189 ============================================================================== --- maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java (original) +++ maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java Sun Jan 14 15:20:49 2007 @@ -55,7 +55,7 @@ File testPom = new File( getBasedir(), "target/test-classes/unit/copy-test/plugin-config.xml" ); mojo = (CopyMojo) lookupMojo( "copy", testPom ); mojo.setOutputDirectory( new File( this.testDir, "outputDirectory" ) ); - // mojo.silent = true; + mojo.silent = true; assertNotNull( mojo ); assertNotNull( mojo.getProject() ); Modified: maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestUnpackMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestUnpackMojo.java?view=diff&rev=496189&r1=496188&r2=496189 ============================================================================== --- maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestUnpackMojo.java (original) +++ maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestUnpackMojo.java Sun Jan 14 15:20:49 2007 @@ -35,7 +35,6 @@ import org.apache.maven.plugin.dependency.testUtils.stubs.StubArtifactRepository; import org.apache.maven.plugin.dependency.testUtils.stubs.StubArtifactResolver; import org.apache.maven.plugin.dependency.utils.markers.DefaultFileMarkerHandler; -import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; public class TestUnpackMojo @@ -58,7 +57,7 @@ mojo = (UnpackMojo) lookupMojo( "unpack", testPom ); mojo.setOutputDirectory( new File( this.testDir, "outputDirectory" ) ); mojo.setMarkersDirectory( new File( this.testDir, "markers" ) ); - // mojo.silent = true; + mojo.silent = true; assertNotNull( mojo ); assertNotNull( mojo.getProject() ); Added: maven/plugins/trunk/maven-dependency-plugin/src/test/resources/unit/build-classpath-test/plugin-config.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/test/resources/unit/build-classpath-test/plugin-config.xml?view=auto&rev=496189 ============================================================================== --- maven/plugins/trunk/maven-dependency-plugin/src/test/resources/unit/build-classpath-test/plugin-config.xml (added) +++ maven/plugins/trunk/maven-dependency-plugin/src/test/resources/unit/build-classpath-test/plugin-config.xml Sun Jan 14 15:20:49 2007 @@ -0,0 +1,38 @@ +<!-- + * 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> + <build> + <plugins> + <plugin> + <artifactId>maven-dependency-plugin</artifactId> + <configuration> + <project implementation="org.apache.maven.plugin.dependency.testUtils.stubs.DependencyProjectStub"/> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + <version>2.0.4</version> + </dependency> + </dependencies> +</project> \ No newline at end of file Propchange: maven/plugins/trunk/maven-dependency-plugin/src/test/resources/unit/build-classpath-test/plugin-config.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml