Bug 433079 - Add option to specify dependencies from a plain text file Extended <dependencies> element to recognize optional "file" attribute that provides dependencies via a line-based plain text file
Project: http://git-wip-us.apache.org/repos/asf/maven-aether/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-aether/commit/755fdff3 Tree: http://git-wip-us.apache.org/repos/asf/maven-aether/tree/755fdff3 Diff: http://git-wip-us.apache.org/repos/asf/maven-aether/diff/755fdff3 Branch: refs/heads/ant-tasks Commit: 755fdff313ffb4a64ff1025d0541aca834036e0d Parents: abc5293 Author: Benjamin Bentmann <bentm...@sonatype.com> Authored: Sat Apr 19 19:10:39 2014 +0200 Committer: Benjamin Bentmann <bentm...@sonatype.com> Committed: Sat Apr 19 19:10:39 2014 +0200 ---------------------------------------------------------------------- README.md | 10 ++++ .../java/org/eclipse/aether/ant/AntRepoSys.java | 59 ++++++++++++++++++++ .../eclipse/aether/ant/types/Dependencies.java | 31 +++++++++- .../org/eclipse/aether/ant/ResolveTest.java | 12 ++++ src/test/resources/ant/Resolve/ant.xml | 9 +++ src/test/resources/ant/Resolve/dependencies.txt | 11 ++++ 6 files changed, 131 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-aether/blob/755fdff3/README.md ---------------------------------------------------------------------- diff --git a/README.md b/README.md index 00e66d2..c062d4e 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,16 @@ transitively. <exclusion coords="g:a"/> <!-- global exclusion for all dependencies of this group --> </dependencies> + <dependencies id="depsFromPom" pomRef="pom"/> + + <dependencies id="depsFromPlainTextFile" file="dependencies.txt"/> + <!-- + Each non-empty line of that text file declares one dependency, using the same syntax as for the `coords` attribute + of the `<dependency>` element, i.e. + <groupId>:<artifactId>:<version>[[:<type>[:<classifier>]]:<scope>] + Everything after the first hash (#) character on a line is considered a comment. + --> + ## Tasks http://git-wip-us.apache.org/repos/asf/maven-aether/blob/755fdff3/src/main/java/org/eclipse/aether/ant/AntRepoSys.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/eclipse/aether/ant/AntRepoSys.java b/src/main/java/org/eclipse/aether/ant/AntRepoSys.java index d9af18b..cf81357 100644 --- a/src/main/java/org/eclipse/aether/ant/AntRepoSys.java +++ b/src/main/java/org/eclipse/aether/ant/AntRepoSys.java @@ -10,7 +10,12 @@ *******************************************************************************/ package org.eclipse.aether.ant; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -635,6 +640,8 @@ public class AntRepoSys dependency.setVersion( dep.getVersion() ); if ( ids.contains( dependency.getVersionlessKey() ) ) { + project.log( "Ignoring dependency " + dependency.getVersionlessKey() + " from " + model.getId() + + ", already declared locally", Project.MSG_VERBOSE ); continue; } if ( dep.getSystemPath() != null && dep.getSystemPath().length() > 0 ) @@ -653,6 +660,21 @@ public class AntRepoSys collectRequest.addDependency( ConverterUtils.toDependency( dependency, globalExclusions, session ) ); } } + + if ( dependencies.getFile() != null ) + { + List<Dependency> deps = readDependencies( dependencies.getFile() ); + for ( Dependency dependency : deps ) + { + if ( ids.contains( dependency.getVersionlessKey() ) ) + { + project.log( "Ignoring dependency " + dependency.getVersionlessKey() + " from " + + dependencies.getFile() + ", already declared locally", Project.MSG_VERBOSE ); + continue; + } + collectRequest.addDependency( ConverterUtils.toDependency( dependency, globalExclusions, session ) ); + } + } } task.getProject().log( "Collecting dependencies", Project.MSG_VERBOSE ); @@ -670,4 +692,41 @@ public class AntRepoSys return result; } + private List<Dependency> readDependencies( File file ) + { + List<Dependency> dependencies = new ArrayList<Dependency>(); + try + { + BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( file ), "UTF-8" ) ); + try + { + for ( String line = reader.readLine(); line != null; line = reader.readLine() ) + { + int comment = line.indexOf( '#' ); + if ( comment >= 0 ) + { + line = line.substring( 0, comment ); + } + line = line.trim(); + if ( line.length() <= 0 ) + { + continue; + } + Dependency dependency = new Dependency(); + dependency.setCoords( line ); + dependencies.add( dependency ); + } + } + finally + { + reader.close(); + } + } + catch ( IOException e ) + { + throw new BuildException( "Cannot read " + file, e ); + } + return dependencies; + } + } http://git-wip-us.apache.org/repos/asf/maven-aether/blob/755fdff3/src/main/java/org/eclipse/aether/ant/types/Dependencies.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/eclipse/aether/ant/types/Dependencies.java b/src/main/java/org/eclipse/aether/ant/types/Dependencies.java index f113755..d26627d 100644 --- a/src/main/java/org/eclipse/aether/ant/types/Dependencies.java +++ b/src/main/java/org/eclipse/aether/ant/types/Dependencies.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2011 Sonatype, Inc. + * Copyright (c) 2010, 2014 Sonatype, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.aether.ant.types; +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -26,6 +27,8 @@ public class Dependencies extends DataType { + private File file; + private Pom pom; private List<Dependency> dependencies = new ArrayList<Dependency>(); @@ -74,6 +77,22 @@ public class Dependencies super.setRefid( ref ); } + public void setFile( File file ) + { + checkAttributesAllowed(); + this.file = file; + checkExternalSources(); + } + + public File getFile() + { + if ( isReference() ) + { + return getRef().getFile(); + } + return file; + } + public void addPom( Pom pom ) { checkChildrenAllowed(); @@ -82,6 +101,7 @@ public class Dependencies throw new BuildException( "You must not specify multiple <pom> elements" ); } this.pom = pom; + checkExternalSources(); } public Pom getPom() @@ -101,6 +121,15 @@ public class Dependencies pom.setProject( getProject() ); } pom.setRefid( ref ); + checkExternalSources(); + } + + private void checkExternalSources() + { + if ( file != null && pom != null ) + { + throw new BuildException( "You must not specify both a text file and a POM to list dependencies" ); + } } public void addDependency( Dependency dependency ) http://git-wip-us.apache.org/repos/asf/maven-aether/blob/755fdff3/src/test/java/org/eclipse/aether/ant/ResolveTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/eclipse/aether/ant/ResolveTest.java b/src/test/java/org/eclipse/aether/ant/ResolveTest.java index 5505c20..921f80d 100644 --- a/src/test/java/org/eclipse/aether/ant/ResolveTest.java +++ b/src/test/java/org/eclipse/aether/ant/ResolveTest.java @@ -96,4 +96,16 @@ public class ResolveTest hasItemInArray( allOf( containsString( "aether-api" ), endsWith( ".jar" ) ) ) ); } + public void testResolveDepsFromFile() + { + executeTarget( "testResolveDepsFromFile" ); + + String prop = getProject().getProperty( "test.resolve.path.org.eclipse.aether:aether-spi:jar" ); + assertThat( "aether-spi was not resolved as a property", prop, notNullValue() ); + assertThat( "aether-spi was not resolved to default local repository", prop, + allOf( containsString( "aether-spi" ), endsWith( ".jar" ) ) ); + prop = getProject().getProperty( "test.resolve.path.org.eclipse.aether:aether-api:jar" ); + assertThat( "aether-api was resolved as a property", prop, nullValue() ); + } + } http://git-wip-us.apache.org/repos/asf/maven-aether/blob/755fdff3/src/test/resources/ant/Resolve/ant.xml ---------------------------------------------------------------------- diff --git a/src/test/resources/ant/Resolve/ant.xml b/src/test/resources/ant/Resolve/ant.xml index 2dbfa58..e7c4890 100644 --- a/src/test/resources/ant/Resolve/ant.xml +++ b/src/test/resources/ant/Resolve/ant.xml @@ -78,4 +78,13 @@ <echo>${tostring:out}</echo> </target> + <target name="testResolveDepsFromFile"> + <repo:resolve> + <dependencies file="${project.dir}/dependencies.txt"> + <exclusion coords="org.eclipse.aether:aether-api"/> + </dependencies> + <properties prefix="test.resolve.path" classpath="runtime"/> + </repo:resolve> + </target> + </project> http://git-wip-us.apache.org/repos/asf/maven-aether/blob/755fdff3/src/test/resources/ant/Resolve/dependencies.txt ---------------------------------------------------------------------- diff --git a/src/test/resources/ant/Resolve/dependencies.txt b/src/test/resources/ant/Resolve/dependencies.txt new file mode 100644 index 0000000..f65dba0 --- /dev/null +++ b/src/test/resources/ant/Resolve/dependencies.txt @@ -0,0 +1,11 @@ +# +# Copyright (c) 2012, 2014 Sonatype, Inc. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# + +# each line specifies one dependency +org.eclipse.aether:aether-spi:0.9.0.v20140226:runtime # a comment +