Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
TYCHO-483 add maven dependencies for checked-in jars
Browse files Browse the repository at this point in the history
Dependencies to nested jars within the same reactor
are now added as system scope dependencies to
maven dependency model if they exist in the filesystem.
This covers the scenario of jars checked in to SCM.
Move corresponding testcase from maven-osgi-compiler-plugin
where it was misplaced to a dedicated testcase in
tycho-osgi-components.
  • Loading branch information
jsievers committed Feb 14, 2011
1 parent 1071246 commit 245676f
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.io.IOException;
import java.util.List;

import org.apache.bcel.classfile.ClassFormatException;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -15,9 +18,6 @@
import org.junit.Assert;
import org.sonatype.tycho.classpath.SourcepathEntry;

import com.sun.org.apache.bcel.internal.classfile.ClassFormatException;
import com.sun.org.apache.bcel.internal.classfile.ClassParser;
import com.sun.org.apache.bcel.internal.classfile.JavaClass;

public class OsgiCompilerTest extends AbstractTychoMojoTestCase {

Expand Down Expand Up @@ -112,45 +112,8 @@ public void testClasspath() throws Exception {
assertEquals( getClasspathElement( project.getBasedir(), "target/classes", "" ), cp.get( 0 ) );
assertEquals( getClasspathElement( new File( getBasedir() ), plainJarPath, "[?**/*]" ), cp.get( 1 ) );
assertEquals( getClasspathElement( new File( getBasedir() ), nestedJarPath, "[?**/*]" ), cp.get( 2 ) );
List<Dependency> mavenDependencies = project.getModel().getDependencies();
// assert that dependencies to both plain jar and nested jar are injected back into maven model
Assert.assertEquals( 2, mavenDependencies.size() );
assertContainsSystemScopeDependency( mavenDependencies, null, new File( getBasedir(), plainJarPath ) );
assertContainsSystemScopeDependency( mavenDependencies, "lib/lib.jar", new File( getBasedir(), nestedJarPath ) );
}

private void assertContainsSystemScopeDependency( List<Dependency> mavenDependencies, String expectedClassifier,
File expectedLocation )
{
for ( Dependency dependency : mavenDependencies )
{
if ( "p2.eclipse-plugin".equals( dependency.getGroupId() ) //
&& "p003".equals( dependency.getArtifactId() )//
&& "1.0.0".equals( dependency.getVersion() ) //
&& "jar".equals( dependency.getType() )//
&& expectedLocation.getAbsolutePath().equals( dependency.getSystemPath() ) //
&& Artifact.SCOPE_SYSTEM.equals( dependency.getScope() ) //
)
{
if ( expectedClassifier == null )
{
if ( dependency.getClassifier() == null )
{
return;
}
}
else
{
if ( expectedClassifier.equals( dependency.getClassifier() ) )
{
return;
}
}
}
}
fail( "Expected system scope dependency p2.eclipse-plugin:p003:1.0.0:jar:" + expectedClassifier + " to file "
+ expectedLocation + " not found" );
}

private String getClasspathElement(File base, String path, String accessRules) throws IOException {
String file = new File(base, path).getCanonicalPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void addDependency( ArtifactDescriptor artifact )
{
if ( !artifact.getMavenProject().sameProject( project ) )
{
dependencyList.add( newProjectDependency( artifact ) );
dependencyList.addAll( newProjectDependencies( artifact ) );
}
}
else
Expand Down Expand Up @@ -150,35 +150,71 @@ private List<String> getClasspathElements( File bundleLocation )
}

private Dependency createSystemScopeDependency( ArtifactKey artifactKey, File location )
{
/* see RepositoryLayoutHelper#getP2Gav */
return createSystemScopeDependency( artifactKey, "p2." + artifactKey.getType() , location );
}

private Dependency createSystemScopeDependency( ArtifactKey artifactKey, String groupId, File location )
{
Dependency dependency = new Dependency();
dependency.setGroupId( groupId );
dependency.setArtifactId( artifactKey.getId() );
dependency.setGroupId( "p2." + artifactKey.getType() ); // See also RepositoryLayoutHelper#getP2Gav
dependency.setVersion( artifactKey.getVersion() );
dependency.setScope( Artifact.SCOPE_SYSTEM );
dependency.setSystemPath( location.getAbsolutePath() );
return dependency;
}

protected Dependency newProjectDependency( ArtifactDescriptor artifact )
protected List<Dependency> newProjectDependencies( ArtifactDescriptor artifact )
{
ReactorProject dependentMavenProjectProxy = artifact.getMavenProject();
Dependency dependency = new Dependency();
dependency.setArtifactId( dependentMavenProjectProxy.getArtifactId() );
dependency.setGroupId( dependentMavenProjectProxy.getGroupId() );
dependency.setVersion( dependentMavenProjectProxy.getVersion() );
dependency.setType( dependentMavenProjectProxy.getPackaging() );
dependency.setScope( Artifact.SCOPE_PROVIDED );
List<Dependency> result = new ArrayList<Dependency>();
if ( ArtifactKey.TYPE_ECLIPSE_PLUGIN.equals( dependentMavenProjectProxy.getPackaging() ) )
{
List<String> classPathElements = getClasspathElements( dependentMavenProjectProxy.getBasedir() );
if ( classPathElements.size() > 1 )
for ( String classpathElement : getClasspathElements( dependentMavenProjectProxy.getBasedir() ) )
{
logger.warn( "Dependency from " + project.getBasedir() + " to nested classpath entries "
+ classPathElements + " at location " + dependentMavenProjectProxy.getBasedir()
+ " can not be represented in Maven model and will not be visible to non-OSGi aware Maven plugins" );
if ( ".".equals( classpathElement ) )
{
result.add( createProvidedScopeDependency( dependentMavenProjectProxy ) );
}
else /* nested classpath entry */
{
File jar = new File( dependentMavenProjectProxy.getBasedir(), classpathElement );
// we can only add a system scope dependency for an existing (checked-in) jar file
// otherwise maven will throw a DependencyResolutionException
if ( jar.isFile() )
{
Dependency systemScopeDependency =
createSystemScopeDependency( artifact.getKey(), artifact.getMavenProject().getGroupId(),
jar );
systemScopeDependency.setClassifier( classpathElement );
result.add( systemScopeDependency );
}
else
{
logger.warn( "Dependency from "
+ project.getBasedir()
+ " to nested classpath entry "
+ jar.getAbsolutePath()
+ " can not be represented in Maven model and will not be visible to non-OSGi aware Maven plugins" );
}
}
}
} else {
result.add( createProvidedScopeDependency( dependentMavenProjectProxy ) );
}
return result;
}

private Dependency createProvidedScopeDependency( ReactorProject dependentReactorProject )
{
Dependency dependency = new Dependency();
dependency.setArtifactId( dependentReactorProject.getArtifactId() );
dependency.setGroupId( dependentReactorProject.getGroupId() );
dependency.setVersion( dependentReactorProject.getVersion() );
dependency.setType( dependentReactorProject.getPackaging() );
dependency.setScope( Artifact.SCOPE_PROVIDED );
return dependency;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package org.codehaus.tycho.maven.test;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;
import org.codehaus.tycho.testing.AbstractTychoMojoTestCase;
import org.junit.Assert;

public class MavenDependencyCollectorTest
extends AbstractTychoMojoTestCase
{

public void testNestedJars()
throws Exception
{
File targetPlatform = getBasedir( "targetplatforms/nestedJar" );
List<MavenProject> projects = getSortedProjects( getBasedir( "projects/mavendeps" ), targetPlatform );
{
// 1. project with dependency to external bundle with nested jar
MavenProject project = projects.get( 1 );
final String plainJarPath = "target/targetplatforms/nestedJar/plugins/nested_1.0.0.jar";
final String nestedJarPath = "target/local-repo/.cache/tycho/nested_1.0.0.jar/lib/lib.jar";
List<Dependency> mavenDependencies = project.getModel().getDependencies();
Assert.assertEquals( 2, mavenDependencies.size() );
final String expectedGroupId = "p2.eclipse-plugin";
final String expectedArtifactId = "nested";
final String expectedVersion = "1.0.0";
final String expectedType = "jar";
final String expectedScope = Artifact.SCOPE_SYSTEM;
// assert that dependencies to both plain jar and nested jar are injected back into maven model
assertDependenciesContains( mavenDependencies, expectedGroupId, expectedArtifactId, expectedVersion, null,
expectedType, expectedScope, new File( getBasedir(), plainJarPath ) );
assertDependenciesContains( mavenDependencies, expectedGroupId, expectedArtifactId, expectedVersion,
"lib/lib.jar", expectedType, expectedScope, new File( getBasedir(),
nestedJarPath ) );
}
{
// 2. project with dependency to bundle with nested jar within the same reactor
MavenProject project = projects.get( 3 );
List<Dependency> mavenDependencies = project.getModel().getDependencies();
// assert that dependencies to both reactor module and checked-in nested jar are injected back into maven
// model.
// Also, dependency to missing nested jar must *not* be injected (would throw
// MavenDependencyResolutionException otherwise)
Assert.assertEquals( 2, mavenDependencies.size() );
final String expectedGroupId = "mavenDependencies";
final String expectedArtifactId = "p002";
final String expectedVersion = "1.0.0";
assertDependenciesContains( mavenDependencies, expectedGroupId, expectedArtifactId, expectedVersion, null,
"eclipse-plugin", Artifact.SCOPE_PROVIDED, null );
assertDependenciesContains( mavenDependencies, expectedGroupId, expectedArtifactId, expectedVersion,
"lib/lib.jar", "jar", Artifact.SCOPE_SYSTEM,
new File( getBasedir( "projects/mavendeps" ), "p002/lib/lib.jar" ) );
}
}

private void assertDependenciesContains( List<Dependency> mavenDependencies, String groupId, String artifactId,
String version, String classifier, String type, String scope,
File systemLocation )
throws IOException
{
for ( Dependency dependency : mavenDependencies )
{
boolean systemLocationEquals = true;
if ( systemLocation != null )
{
systemLocationEquals =
dependency.getSystemPath() != null
&& systemLocation.getCanonicalFile().getAbsolutePath().equals( new File(
dependency.getSystemPath() ).getCanonicalFile().getAbsolutePath() );
}
if ( systemLocationEquals //
&& groupId.equals( dependency.getGroupId() ) //
&& artifactId.equals( dependency.getArtifactId() )//
&& version.equals( dependency.getVersion() ) //
&& type.equals( dependency.getType() )//
&& scope.equals( dependency.getScope() ) //
)
{
if ( classifier == null )
{
if ( dependency.getClassifier() == null )
{
return;
}
}
else
{
if ( classifier.equals( dependency.getClassifier() ) )
{
return;
}
}
}
}
fail( "Expected dependency [" + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type
+ ":" + scope + ":" + systemLocation + "] not found in actual dependencies: "
+ toDebugString( mavenDependencies ) );
}

private static String toDebugString( List<Dependency> mavenDependencies )
{
StringBuilder sb = new StringBuilder();
for ( Dependency dependency : mavenDependencies )
{
sb.append( toDebugString( dependency ) );
}
return sb.toString();
}

private static String toDebugString( Dependency dependency )
{
StringBuilder sb = new StringBuilder();
sb.append( '[' );
sb.append( dependency.getGroupId() );
sb.append( ':' );
sb.append( dependency.getArtifactId() );
sb.append( ':' );
sb.append( dependency.getVersion() );
sb.append( ':' );
sb.append( dependency.getClassifier() );
sb.append( ':' );
sb.append( dependency.getType() );
sb.append( ", scope: " );
sb.append( dependency.getScope() );
sb.append( ", systemPath: " );
sb.append( dependency.getSystemPath() );
sb.append( ']' );
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
hManifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: P001 Plug-in
Bundle-SymbolicName: p001
Bundle-Version: 1.0.0
Bundle-ClassPath: .
Require-Bundle: nested
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>projects</artifactId>
<groupId>mavenDependencies</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>p001</artifactId>
<packaging>eclipse-plugin</packaging>
<version>1.0.0</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
hManifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: P002 Plug-in
Bundle-SymbolicName: p002
Bundle-Version: 1.0.0
Bundle-ClassPath: .,lib/lib.jar,lib/notexisting.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
lib/,\
.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>projects</artifactId>
<groupId>mavenDependencies</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>p002</artifactId>
<packaging>eclipse-plugin</packaging>
<version>1.0.0</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
hManifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: P003 Plug-in
Bundle-SymbolicName: p003
Bundle-Version: 1.0.0
Bundle-ClassPath: .
Require-Bundle: p002
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>projects</artifactId>
<groupId>mavenDependencies</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>p003</artifactId>
<packaging>eclipse-plugin</packaging>
<version>1.0.0</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?><project>
<modelVersion>4.0.0</modelVersion>
<groupId>mavenDependencies</groupId>
<artifactId>projects</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>p001</module>
<module>p002</module>
<module>p003</module>
</modules>

</project>
Binary file not shown.
Loading

0 comments on commit 245676f

Please sign in to comment.