Skip to content

Commit

Permalink
Merge pull request #13 from pierre/add-classifier-support
Browse files Browse the repository at this point in the history
Add classifier support
  • Loading branch information
mkristian committed Jan 20, 2013
2 parents 5e844a6 + c60c648 commit 5236065
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
target
# no Gemfile.lock
Gemfile.lock
.idea
*.iml
*.log
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require 'fileutils'
task :default => [ :minispec]

task :build do
rmvn = Maven::RubyMaven.new
rmvn = Maven::Ruby::Maven.new
rmvn.options['-Dmaven.test.skip'] = true
if rmvn.exec('package')
puts 'you find the gem inside "target"'
Expand All @@ -23,15 +23,15 @@ task :build do
end

task :compile do
rmvn = Maven::RubyMaven.new
rmvn = Maven::Ruby::Maven.new
rmvn.options['-Dmaven.test.skip'] = true
unless rmvn.exec('prepare-package')
raise 'failed'
end
end

task :features => [:compile] do
rmvn = Maven::RubyMaven.new
rmvn = Maven::Ruby::Maven.new
rversion = RUBY_VERSION =~ /^1.8./ ? '--1.8': '--1.9'
rmvn.options['-Djruby.versions'] = '1.6.7.2'#JRUBY_VERSION
rmvn.options['-Djruby.switches'] = rversion
Expand Down
2 changes: 1 addition & 1 deletion lib/jbundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module JBundler
class Cli < Thor
no_tasks do
def mvn
@mvn ||= Maven::RubyMaven.new
@mvn ||= Maven::Ruby::Maven.new
end

def do_show
Expand Down
13 changes: 12 additions & 1 deletion lib/jbundler/pom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,25 @@ def initialize(name, version, deps, packaging = nil)

deps.each do |line|
if coord = to_coordinate(line)
group_id, artifact_id, extension, version = coord.split(/:/)
coords = coord.split(/:/)
group_id = coords[0]
artifact_id = coords[1]
extension = coords[2]
classifier = nil
if coords.size == 4
version = coords[3]
else
classifier = coords[3]
version = coords[4]
end

xmlStreamWriter.writeStartElement("dependency".to_java)
writeElement(xmlStreamWriter,"groupId", group_id)
writeElement(xmlStreamWriter,"artifactId", artifact_id)
writeElement(xmlStreamWriter,"version", version)

writeElement(xmlStreamWriter,"type", extension) if extension != 'jar'
writeElement(xmlStreamWriter,"classifier", classifier) if classifier
xmlStreamWriter.writeEndElement #dependency
end
end
Expand Down
38 changes: 38 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@
<artifactId>bundler</artifactId>
<type>gem</type>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -208,6 +220,32 @@
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions spec/pom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@
pom.coordinate.must_equal "ruby.bundler:second:jar:1"
end

it 'should respect classifiers' do
pom = JBundler::Pom.new("third", "1", ["jar \"org.jruby:jruby-core\", '~>1.7.0'", "pom \"f:g:jdk15\", \">1.2\", \"<=2.0\""])
File.read(pom.file).must_equal "<?xml version=\"1.0\" ?><project><modelVersion>4.0.0</modelVersion><groupId>ruby.bundler</groupId><artifactId>third</artifactId><version>1</version><dependencies><dependency><groupId>org.jruby</groupId><artifactId>jruby-core</artifactId><version>[1.7.0,1.7.99999]</version></dependency><dependency><groupId>f</groupId><artifactId>g</artifactId><version>(1.2,2.0]</version><type>pom</type><classifier>jdk15</classifier></dependency></dependencies></project>"
pom.coordinate.must_equal "ruby.bundler:third:jar:1"
end

end
38 changes: 24 additions & 14 deletions src/main/java/jbundler/Aether.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,38 @@ public String getClasspath() {
}

public List<String> getResolvedCoordinates() {
List<String> result = new ArrayList<String>();

PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
node.accept( nlg );

for ( DependencyNode node: nlg.getNodes() )
{
if ( node.getDependency() != null )
{
Artifact artifact = node.getDependency().getArtifact();
if ( artifact.getFile() != null)
{
StringBuilder coord = new StringBuilder(artifact.getGroupId()).append(":").append(artifact.getArtifactId())
.append(":").append(artifact.getExtension()).append(":").append(artifact.getVersion());

return generateCoordinatesForNodes(nlg.getNodes());
}

//@VisibleForTesting
static List<String> generateCoordinatesForNodes(final List<DependencyNode> nodes) {
final List<String> result = new ArrayList<String>();
for (final DependencyNode node : nodes) {
if (node.getDependency() != null) {
final Artifact artifact = node.getDependency().getArtifact();
if (artifact.getFile() != null) {
final StringBuilder coord = new StringBuilder(artifact.getGroupId()).append(":")
.append(artifact.getArtifactId())
.append(":")
.append(artifact.getExtension())
.append(":");
// The classifier should never be null
if (!artifact.getClassifier().isEmpty()) {
coord.append(artifact.getClassifier()).append(":");
}

coord.append(artifact.getVersion());
result.add(coord.toString());
}
}
}

return result;
}

public void install(String coordinate, String file) throws InstallationException{
LocalRepositoryManager lrm = session.getLocalRepositoryManager();

Expand All @@ -172,4 +182,4 @@ public void install(String coordinate, String file) throws InstallationException
installer.install(session, request);
}
}
}
}
52 changes: 52 additions & 0 deletions src/test/java/jbundler/TestAether.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package jbundler;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.mockito.Mockito;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.graph.DependencyNode;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.graph.DefaultDependencyNode;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestAether {

@Test
public void testGetResolvedCoordinates() throws Exception {
final String[] coords = new String[]{
"a:b:jar:[0,)",
"b:c:pom:(2.3.4,)",
"c:d:jar:2.3.4",
"d:e:jar:[1.8.2,1.8.99999]",
"e:f:pom:[1.8,1.9.9)",
"f:g:pom:(1.2,2.0]",
"a:b:jar:jdk15:[0,)",
"b:c:pom:jdk15:(2.3.4,)",
"c:d:jar:jdk15:2.3.4",
"d:e:jar:jdk15:[1.8.2,1.8.99999]",
"e:f:pom:jdk15:[1.8,1.9.9)",
"f:g:pom:jdk15:(1.2,2.0]"};

final List<DependencyNode> nodes = new ArrayList<DependencyNode>();
for (final String coord : coords) {
nodes.add(generateNode(coord));
}

final List<String> generatedCoords = Aether.generateCoordinatesForNodes(nodes);
Assert.assertEquals(generatedCoords, Arrays.asList(coords));
}

private DefaultDependencyNode generateNode(final String coordinates) {
final DefaultArtifact artifact = new DefaultArtifact(coordinates);
// Add a dummy file to pretend the artifact was resolved
final Artifact artifactWithFile = artifact.setFile(Mockito.mock(File.class));

final Dependency dependency = new Dependency(artifactWithFile, null);
return new DefaultDependencyNode(dependency);
}
}

0 comments on commit 5236065

Please sign in to comment.