[FELIX-3565] Embed-Transitive leaks transitive dependencies of excluded artifacts
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1690153 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/pom.xml b/bundleplugin/pom.xml
index b205442..555e1d8 100644
--- a/bundleplugin/pom.xml
+++ b/bundleplugin/pom.xml
@@ -142,7 +142,7 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
- <version>2.0.7</version>
+ <version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AbstractDependencyFilter.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AbstractDependencyFilter.java
index 80ed259..8b668dd 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AbstractDependencyFilter.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AbstractDependencyFilter.java
@@ -22,15 +22,26 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
+import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.regex.Pattern;
import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
+import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.plugin.MojoExecutionException;
import aQute.bnd.header.Attrs;
import aQute.bnd.header.OSGiHeader;
import aQute.bnd.osgi.Instruction;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter;
+import org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter;
+import org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor;
+import org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor;
+import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+import org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor;
/**
@@ -43,17 +54,22 @@
private static final Pattern MISSING_KEY_PATTERN = Pattern.compile( "(^|,)\\p{Blank}*(!)?\\p{Blank}*([a-zA-Z]+=)" );
/**
+ * Dependency Graph.
+ */
+ private final DependencyNode m_dependencyGraph;
+ /**
* Dependency artifacts.
*/
private final Collection<Artifact> m_dependencyArtifacts;
- public AbstractDependencyFilter( Collection<Artifact> dependencyArtifacts )
+ public AbstractDependencyFilter( DependencyNode dependencyGraph, Collection<Artifact> dependencyArtifacts )
{
+ m_dependencyGraph = dependencyGraph;
m_dependencyArtifacts = dependencyArtifacts;
}
- private static abstract class DependencyFilter
+ private static abstract class DependencyFilter implements ArtifactFilter
{
private final Instruction m_instruction;
private final String m_defaultValue;
@@ -71,21 +87,7 @@
m_defaultValue = defaultValue;
}
-
- public void filter( Collection<Artifact> dependencies )
- {
- for ( Iterator<Artifact> i = dependencies.iterator(); i.hasNext(); )
- {
- if ( false == matches( i.next() ) )
- {
- i.remove();
- }
- }
- }
-
-
- abstract boolean matches( Artifact dependency );
-
+ public abstract boolean include( Artifact dependency );
boolean matches( String text )
{
@@ -104,6 +106,26 @@
}
}
+ private static class TrimmingDependencyNodeFilter implements DependencyNodeFilter
+ {
+ private DependencyNodeFilter dependencyNodeFilter;
+
+ public TrimmingDependencyNodeFilter( DependencyNodeFilter dependencyNodeFilter )
+ {
+ this.dependencyNodeFilter = dependencyNodeFilter;
+ }
+
+ public boolean accept( DependencyNode node )
+ {
+ boolean accepted = dependencyNodeFilter.accept( node );
+ if( !accepted )
+ {
+ List<DependencyNode> children = node.getChildren();
+ children.clear();
+ }
+ return accepted;
+ }
+ }
protected final void processInstructions( String header ) throws MojoExecutionException
{
@@ -111,7 +133,6 @@
Collection<Artifact> availableDependencies = new LinkedHashSet<Artifact>( m_dependencyArtifacts );
- DependencyFilter filter;
for ( Iterator<Map.Entry<String,Attrs>> clauseIterator = instructions.entrySet().iterator(); clauseIterator.hasNext(); )
{
String inline = "false";
@@ -128,22 +149,24 @@
primaryKey = primaryKey.substring( 1 );
}
+ final AndArtifactFilter andArtifactFilter = new AndArtifactFilter();
if ( !"*".equals( primaryKey ) )
{
- filter = new DependencyFilter( primaryKey )
+ ArtifactFilter filter = new DependencyFilter( primaryKey )
{
@Override
- boolean matches( Artifact dependency )
+ public boolean include( Artifact dependency )
{
return super.matches( dependency.getArtifactId() );
}
};
// FILTER ON MAIN CLAUSE
- filter.filter( filteredDependencies );
+ andArtifactFilter.add(filter);
}
for ( Iterator<Map.Entry<String,String>> attrIterator = clause.getValue().entrySet().iterator(); attrIterator.hasNext(); )
{
+ final ArtifactFilter filter;
// ATTRIBUTE: KEY --> REGEXP
Map.Entry<String,String> attr = attrIterator.next();
if ( "groupId".equals( attr.getKey() ) )
@@ -151,7 +174,7 @@
filter = new DependencyFilter( attr.getValue() )
{
@Override
- boolean matches( Artifact dependency )
+ public boolean include( Artifact dependency )
{
return super.matches( dependency.getGroupId() );
}
@@ -162,7 +185,7 @@
filter = new DependencyFilter( attr.getValue() )
{
@Override
- boolean matches( Artifact dependency )
+ public boolean include( Artifact dependency )
{
return super.matches( dependency.getArtifactId() );
}
@@ -173,7 +196,7 @@
filter = new DependencyFilter( attr.getValue() )
{
@Override
- boolean matches( Artifact dependency )
+ public boolean include( Artifact dependency )
{
try
{
@@ -192,7 +215,7 @@
filter = new DependencyFilter( attr.getValue(), "compile" )
{
@Override
- boolean matches( Artifact dependency )
+ public boolean include( Artifact dependency )
{
return super.matches( dependency.getScope() );
}
@@ -203,7 +226,7 @@
filter = new DependencyFilter( attr.getValue(), "jar" )
{
@Override
- boolean matches( Artifact dependency )
+ public boolean include( Artifact dependency )
{
return super.matches( dependency.getType() );
}
@@ -214,7 +237,7 @@
filter = new DependencyFilter( attr.getValue() )
{
@Override
- boolean matches( Artifact dependency )
+ public boolean include( Artifact dependency )
{
return super.matches( dependency.getClassifier() );
}
@@ -225,7 +248,7 @@
filter = new DependencyFilter( attr.getValue(), "false" )
{
@Override
- boolean matches( Artifact dependency )
+ public boolean include( Artifact dependency )
{
return super.matches( "" + dependency.isOptional() );
}
@@ -242,9 +265,11 @@
}
// FILTER ON EACH ATTRIBUTE
- filter.filter( filteredDependencies );
+ andArtifactFilter.add( filter );
}
+ filteredDependencies( andArtifactFilter, filteredDependencies );
+
if ( isNegative )
{
// negative clauses reduce the set of available artifacts
@@ -265,4 +290,43 @@
protected abstract void processDependencies( Collection<Artifact> dependencies, String inline );
+
+ private void filteredDependencies( final ArtifactFilter artifactFilter, Collection<Artifact> filteredDependencies )
+ {
+ CollectingDependencyNodeVisitor collectingDependencyNodeVisitor = new CollectingDependencyNodeVisitor();
+ final Artifact rootArtifact = m_dependencyGraph.getArtifact();
+ ArtifactFilter filter = new ArtifactFilter()
+ {
+
+
+ public boolean include( Artifact artifact )
+ {
+ return artifact.equals( rootArtifact ) || artifactFilter.include( artifact );
+ }
+
+
+ };
+ DependencyNodeFilter dependencyNodeFilter = new ArtifactDependencyNodeFilter( filter );
+ dependencyNodeFilter = new TrimmingDependencyNodeFilter( dependencyNodeFilter );
+ DependencyNodeVisitor dependencyNodeVisitor =
+ new FilteringDependencyNodeVisitor( collectingDependencyNodeVisitor, dependencyNodeFilter );
+ dependencyNodeVisitor = new BuildingDependencyNodeVisitor( dependencyNodeVisitor );
+ m_dependencyGraph.accept( dependencyNodeVisitor );
+ List<DependencyNode> dependencyNodes = collectingDependencyNodeVisitor.getNodes();
+ Set<String> ids = new LinkedHashSet<String>( dependencyNodes.size() );
+ for( DependencyNode dependencyNode : dependencyNodes ) {
+ Artifact artifact = dependencyNode.getArtifact();
+ String id = artifact.getId();
+ ids.add(id);
+ }
+ for (Iterator<Artifact> iterator = filteredDependencies.iterator(); iterator.hasNext();)
+ {
+ Artifact artifact = iterator.next();
+ String id = artifact.getId();
+ if (!ids.contains(id))
+ {
+ iterator.remove();
+ }
+ }
+ }
}
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AntPlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AntPlugin.java
index c3c1caa..0e5c7d6 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AntPlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/AntPlugin.java
@@ -29,6 +29,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
@@ -49,7 +50,7 @@
@Override
- protected void execute( MavenProject currentProject, Map<String, String> originalInstructions, Properties properties,
+ protected void execute( MavenProject currentProject, DependencyNode dependencyGraph, Map<String, String> originalInstructions, Properties properties,
Jar[] classpath ) throws MojoExecutionException
{
final String artifactId = getProject().getArtifactId();
@@ -58,7 +59,8 @@
try
{
// assemble bundle as usual, but don't save it - this way we have all the instructions we need
- Builder builder = buildOSGiBundle( currentProject, originalInstructions, properties, classpath );
+ Builder builder = buildOSGiBundle( currentProject,
+ dependencyGraph, originalInstructions, properties, classpath );
Properties bndProperties = builder.getProperties();
// cleanup and remove all non-strings from the builder properties
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
index 2aed620..5d095d8 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
@@ -329,7 +329,7 @@
instructions.put( Analyzer.IMPORT_PACKAGE, wrapImportPackage );
project.getArtifact().setFile( getFile( artifact ) );
- File outputFile = getOutputFile( artifact );
+ File outputFile = getOutputFile(artifact);
if ( project.getArtifact().getFile().equals( outputFile ) )
{
@@ -342,7 +342,8 @@
// + " to the same file, try cleaning: " + outputFile );
}
- Analyzer analyzer = getAnalyzer( project, instructions, new Properties(), getClasspath( project ) );
+ org.apache.maven.shared.dependency.graph.DependencyNode dependencyGraph = buildDependencyGraph( project );
+ Analyzer analyzer = getAnalyzer( project, dependencyGraph, instructions, new Properties(), getClasspath( project, dependencyGraph ) );
Jar osgiJar = new Jar( project.getArtifactId(), project.getArtifact().getFile() );
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
index 2b37fc8..20f4b35 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
@@ -64,6 +64,9 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.osgi.DefaultMaven2OsgiConverter;
import org.apache.maven.shared.osgi.Maven2OsgiConverter;
import org.codehaus.plexus.archiver.UnArchiver;
@@ -160,6 +163,9 @@
@Component
private ArtifactHandlerManager m_artifactHandlerManager;
+ @Component
+ private DependencyGraphBuilder m_dependencyGraphBuilder;
+
/**
* Project types which this plugin supports.
*/
@@ -233,6 +239,19 @@
return project;
}
+ protected DependencyNode buildDependencyGraph( MavenProject mavenProject ) throws MojoExecutionException
+ {
+ DependencyNode dependencyGraph;
+ try
+ {
+ dependencyGraph = m_dependencyGraphBuilder.buildDependencyGraph( mavenProject, null );
+ }
+ catch ( DependencyGraphBuilderException e )
+ {
+ throw new MojoExecutionException( e.getMessage(), e );
+ }
+ return dependencyGraph;
+ }
/**
* @see org.apache.maven.plugin.AbstractMojo#execute()
@@ -250,16 +269,16 @@
return;
}
- execute( getProject(), instructions, properties );
+ execute( getProject(), buildDependencyGraph(getProject()), instructions, properties );
}
- protected void execute( MavenProject currentProject, Map<String, String> originalInstructions, Properties properties )
+ protected void execute( MavenProject currentProject, DependencyNode dependencyGraph, Map<String, String> originalInstructions, Properties properties )
throws MojoExecutionException
{
try
{
- execute( currentProject, originalInstructions, properties, getClasspath( currentProject ) );
+ execute( currentProject, dependencyGraph, originalInstructions, properties, getClasspath( currentProject, dependencyGraph ) );
}
catch ( IOException e )
{
@@ -336,13 +355,13 @@
}
- protected void execute( MavenProject currentProject, Map<String, String> originalInstructions, Properties properties,
+ protected void execute( MavenProject currentProject, DependencyNode dependencyGraph, Map<String, String> originalInstructions, Properties properties,
Jar[] classpath ) throws MojoExecutionException
{
try
{
File jarFile = new File( getBuildDirectory(), getBundleName( currentProject ) );
- Builder builder = buildOSGiBundle( currentProject, originalInstructions, properties, classpath );
+ Builder builder = buildOSGiBundle( currentProject, dependencyGraph, originalInstructions, properties, classpath );
boolean hasErrors = reportErrors( "Bundle " + currentProject.getArtifact(), builder );
if ( hasErrors )
{
@@ -572,7 +591,7 @@
}
- protected void addMavenInstructions( MavenProject currentProject, Builder builder ) throws Exception
+ protected void addMavenInstructions( MavenProject currentProject, DependencyNode dependencyGraph, Builder builder ) throws Exception
{
if ( currentProject.getBasedir() != null )
{
@@ -587,8 +606,8 @@
}
// update BND instructions to embed selected Maven dependencies
- Collection<Artifact> embeddableArtifacts = getEmbeddableArtifacts( currentProject, builder );
- new DependencyEmbedder( getLog(), embeddableArtifacts ).processHeaders( builder );
+ Collection<Artifact> embeddableArtifacts = getEmbeddableArtifacts( currentProject, dependencyGraph, builder );
+ new DependencyEmbedder( getLog(), dependencyGraph, embeddableArtifacts ).processHeaders( builder );
if ( dumpInstructions != null || getLog().isDebugEnabled() )
{
@@ -616,16 +635,16 @@
}
- protected Builder buildOSGiBundle( MavenProject currentProject, Map<String, String> originalInstructions, Properties properties,
+ protected Builder buildOSGiBundle( MavenProject currentProject, DependencyNode dependencyGraph, Map<String, String> originalInstructions, Properties properties,
Jar[] classpath ) throws Exception
{
Builder builder = getOSGiBuilder( currentProject, originalInstructions, properties, classpath );
- addMavenInstructions( currentProject, builder );
+ addMavenInstructions( currentProject, dependencyGraph, builder );
builder.build();
- mergeMavenManifest( currentProject, builder );
+ mergeMavenManifest( currentProject, dependencyGraph, builder );
return builder;
}
@@ -736,7 +755,7 @@
}
- protected void mergeMavenManifest( MavenProject currentProject, Builder builder ) throws Exception
+ protected void mergeMavenManifest( MavenProject currentProject, DependencyNode dependencyGraph, Builder builder ) throws Exception
{
Jar jar = builder.getJar();
@@ -831,7 +850,7 @@
String importPackages = bundleManifest.getMainAttributes().getValue( "Import-Package" );
if ( importPackages != null )
{
- Set optionalPackages = getOptionalPackages( currentProject );
+ Set optionalPackages = getOptionalPackages( currentProject, dependencyGraph );
Map<String, ? extends Map<String, String>> values = new Analyzer().parseHeader( importPackages );
for ( Map.Entry<String, ? extends Map<String, String>> entry : values.entrySet() )
@@ -868,19 +887,16 @@
}
- protected Set<String> getOptionalPackages( MavenProject currentProject ) throws IOException, MojoExecutionException
+ protected Set<String> getOptionalPackages( MavenProject currentProject, DependencyNode dependencyGraph ) throws IOException, MojoExecutionException
{
ArrayList<Artifact> inscope = new ArrayList<Artifact>();
- final Collection<Artifact> artifacts = getSelectedDependencies( currentProject.getArtifacts() );
+ final Collection<Artifact> artifacts = getSelectedDependencies( dependencyGraph, currentProject.getArtifacts() );
for ( Iterator<Artifact> it = artifacts.iterator(); it.hasNext(); )
{
Artifact artifact = it.next();
if ( artifact.getArtifactHandler().isAddedToClasspath() )
{
- if ( !Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
- {
- inscope.add( artifact );
- }
+ inscope.add( artifact );
}
}
@@ -1077,7 +1093,7 @@
}
- protected Jar[] getClasspath( MavenProject currentProject ) throws IOException, MojoExecutionException
+ protected Jar[] getClasspath( MavenProject currentProject, DependencyNode dependencyGraph ) throws IOException, MojoExecutionException
{
List<Jar> list = new ArrayList<Jar>();
@@ -1086,25 +1102,22 @@
list.add( new Jar( ".", getOutputDirectory() ) );
}
- final Collection<Artifact> artifacts = getSelectedDependencies( currentProject.getArtifacts() );
+ final Collection<Artifact> artifacts = getSelectedDependencies( dependencyGraph, currentProject.getArtifacts() );
for ( Iterator<Artifact> it = artifacts.iterator(); it.hasNext(); )
{
Artifact artifact = it.next();
if ( artifact.getArtifactHandler().isAddedToClasspath() )
{
- if ( !Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
+ File file = getFile( artifact );
+ if ( file == null )
{
- File file = getFile( artifact );
- if ( file == null )
- {
- getLog().warn(
- "File is not available for artifact " + artifact + " in project "
- + currentProject.getArtifact() );
- continue;
- }
- Jar jar = new Jar( artifact.getArtifactId(), file );
- list.add( jar );
+ getLog().warn(
+ "File is not available for artifact " + artifact + " in project "
+ + currentProject.getArtifact() );
+ continue;
}
+ Jar jar = new Jar( artifact.getArtifactId(), file );
+ list.add( jar );
}
}
Jar[] cp = new Jar[list.size()];
@@ -1113,7 +1126,7 @@
}
- private Collection<Artifact> getSelectedDependencies( Collection<Artifact> artifacts ) throws MojoExecutionException
+ private Collection<Artifact> getSelectedDependencies( DependencyNode dependencyGraph, Collection<Artifact> artifacts ) throws MojoExecutionException
{
if ( null == excludeDependencies || excludeDependencies.length() == 0 )
{
@@ -1125,7 +1138,7 @@
}
Collection<Artifact> selectedDependencies = new LinkedHashSet<Artifact>( artifacts );
- DependencyExcluder excluder = new DependencyExcluder( artifacts );
+ DependencyExcluder excluder = new DependencyExcluder( dependencyGraph, artifacts );
excluder.processHeaders( excludeDependencies );
selectedDependencies.removeAll( excluder.getExcludedArtifacts() );
@@ -1539,7 +1552,7 @@
}
- protected Collection<Artifact> getEmbeddableArtifacts( MavenProject currentProject, Analyzer analyzer )
+ protected Collection<Artifact> getEmbeddableArtifacts( MavenProject currentProject, DependencyNode dependencyGraph, Analyzer analyzer )
throws MojoExecutionException
{
final Collection<Artifact> artifacts;
@@ -1556,7 +1569,7 @@
artifacts = currentProject.getDependencyArtifacts();
}
- return getSelectedDependencies( artifacts );
+ return getSelectedDependencies( dependencyGraph, artifacts );
}
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/DependencyEmbedder.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/DependencyEmbedder.java
index c83d3a2..97e91d1 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/DependencyEmbedder.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/DependencyEmbedder.java
@@ -27,6 +27,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.codehaus.plexus.util.StringUtils;
import aQute.bnd.osgi.Analyzer;
@@ -64,9 +65,9 @@
private final Collection<Artifact> m_embeddedArtifacts;
- public DependencyEmbedder( Log log, Collection<Artifact> dependencyArtifacts )
+ public DependencyEmbedder( Log log, DependencyNode dependencyGraph, Collection<Artifact> dependencyArtifacts )
{
- super( dependencyArtifacts );
+ super( dependencyGraph, dependencyArtifacts );
m_inlinedPaths = new LinkedHashSet<String>();
m_embeddedArtifacts = new LinkedHashSet<Artifact>();
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/DependencyExcluder.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/DependencyExcluder.java
index 1bcc8b1..145e26e 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/DependencyExcluder.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/DependencyExcluder.java
@@ -24,6 +24,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
/**
@@ -39,9 +40,9 @@
private final Collection<Artifact> m_excludedArtifacts;
- public DependencyExcluder( Collection<Artifact> dependencyArtifacts )
+ public DependencyExcluder( DependencyNode dependencyGraph, Collection<Artifact> dependencyArtifacts )
{
- super( dependencyArtifacts );
+ super( dependencyGraph, dependencyArtifacts );
m_excludedArtifacts = new HashSet<Artifact>();
}
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/InstructionsPlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/InstructionsPlugin.java
index 4341a3b..b0c05ca 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/InstructionsPlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/InstructionsPlugin.java
@@ -32,6 +32,7 @@
import org.apache.maven.project.MavenProject;
import aQute.bnd.osgi.Jar;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
/**
@@ -41,7 +42,7 @@
public class InstructionsPlugin extends BundlePlugin
{
@Override
- protected void execute( MavenProject project, Map<String, String> instructions, Properties properties, Jar[] classpath )
+ protected void execute( MavenProject project, DependencyNode dependencyGraph, Map<String, String> instructions, Properties properties, Jar[] classpath )
throws MojoExecutionException
{
if ( dumpInstructions == null )
@@ -51,7 +52,7 @@
try
{
- addMavenInstructions( project, getOSGiBuilder( project, instructions, properties, classpath ) );
+ addMavenInstructions( project, dependencyGraph, getOSGiBuilder(project, instructions, properties, classpath) );
}
catch ( FileNotFoundException e )
{
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
index e474fad..10e3f9c 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
@@ -43,6 +43,7 @@
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Resource;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
/**
@@ -61,13 +62,13 @@
@Override
- protected void execute( MavenProject project, Map<String, String> instructions, Properties properties, Jar[] classpath )
+ protected void execute( MavenProject project, DependencyNode dependencyGraph, Map<String, String> instructions, Properties properties, Jar[] classpath )
throws MojoExecutionException
{
Manifest manifest;
try
{
- manifest = getManifest( project, instructions, properties, classpath );
+ manifest = getManifest( project, dependencyGraph, instructions, properties, classpath );
}
catch ( FileNotFoundException e )
{
@@ -102,17 +103,17 @@
}
- public Manifest getManifest( MavenProject project, Jar[] classpath ) throws IOException, MojoFailureException,
+ public Manifest getManifest( MavenProject project, DependencyNode dependencyGraph, Jar[] classpath ) throws IOException, MojoFailureException,
MojoExecutionException, Exception
{
- return getManifest( project, new LinkedHashMap<String, String>(), new Properties(), classpath );
+ return getManifest( project, dependencyGraph, new LinkedHashMap<String, String>(), new Properties(), classpath );
}
- public Manifest getManifest( MavenProject project, Map<String, String> instructions, Properties properties, Jar[] classpath )
+ public Manifest getManifest( MavenProject project, DependencyNode dependencyGraph, Map<String, String> instructions, Properties properties, Jar[] classpath )
throws IOException, MojoFailureException, MojoExecutionException, Exception
{
- Analyzer analyzer = getAnalyzer( project, instructions, properties, classpath );
+ Analyzer analyzer = getAnalyzer( project, dependencyGraph, instructions, properties, classpath );
boolean hasErrors = reportErrors( "Manifest " + project.getArtifact(), analyzer );
if ( hasErrors )
{
@@ -150,19 +151,19 @@
}
- protected Analyzer getAnalyzer( MavenProject project, Jar[] classpath ) throws IOException, MojoExecutionException,
+ protected Analyzer getAnalyzer( MavenProject project, DependencyNode dependencyGraph, Jar[] classpath ) throws IOException, MojoExecutionException,
Exception
{
- return getAnalyzer( project, new LinkedHashMap<String, String>(), new Properties(), classpath );
+ return getAnalyzer( project, dependencyGraph, new LinkedHashMap<String, String>(), new Properties(), classpath );
}
- protected Analyzer getAnalyzer( MavenProject project, Map<String, String> instructions, Properties properties, Jar[] classpath )
+ protected Analyzer getAnalyzer( MavenProject project, DependencyNode dependencyGraph, Map<String, String> instructions, Properties properties, Jar[] classpath )
throws IOException, MojoExecutionException, Exception
{
if ( rebuildBundle && supportedProjectTypes.contains( project.getArtifact().getType() ) )
{
- return buildOSGiBundle( project, instructions, properties, classpath );
+ return buildOSGiBundle( project, dependencyGraph, instructions, properties, classpath );
}
File file = getOutputDirectory();
@@ -201,7 +202,7 @@
analyzer.setProperty( Analyzer.EXPORT_PACKAGE, export );
}
- addMavenInstructions( project, analyzer );
+ addMavenInstructions( project, dependencyGraph, analyzer );
// if we spot Embed-Dependency and the bundle is "target/classes", assume we need to rebuild
if ( analyzer.getProperty( DependencyEmbedder.EMBED_DEPENDENCY ) != null && isOutputDirectory )
@@ -214,7 +215,7 @@
analyzer.getJar().setManifest( analyzer.calcManifest() );
}
- mergeMavenManifest( project, analyzer );
+ mergeMavenManifest( project, dependencyGraph, analyzer );
return analyzer;
}
diff --git a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/AbstractBundlePluginTest.java b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/AbstractBundlePluginTest.java
index 5afd05e..2f43aa6 100644
--- a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/AbstractBundlePluginTest.java
+++ b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/AbstractBundlePluginTest.java
@@ -22,8 +22,20 @@
import java.io.File;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.DefaultArtifactRepository;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.stubs.ArtifactStub;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.apache.maven.project.DefaultProjectBuilderConfiguration;
+import org.apache.maven.project.ProjectBuilderConfiguration;
import org.codehaus.plexus.PlexusTestCase;
@@ -33,15 +45,28 @@
* @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
* @version $Id$
*/
-public abstract class AbstractBundlePluginTest extends PlexusTestCase
+public abstract class AbstractBundlePluginTest extends AbstractMojoTestCase
{
protected MavenProjectStub getMavenProjectStub()
{
MavenProjectStub project = new MavenProjectStub();
- project.setGroupId( "group" );
- project.setArtifactId( "project" );
+ project.setGroupId("group");
+ project.setArtifactId("project");
project.setVersion( "1.2.3.4" );
+
+ VersionRange versionRange = VersionRange.createFromVersion( project.getVersion() );
+ ArtifactHandler artifactHandler = new DefaultArtifactHandler("pom");
+ Artifact artifact =
+ new DefaultArtifact( project.getGroupId(), project.getArtifactId(),
+ versionRange, null, "pom", null, artifactHandler );
+ artifact.setResolved( true );
+ project.setArtifact( artifact );
+ ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration();
+ ArtifactRepositoryLayout layout = new LegacyRepositoryLayout();
+ ArtifactRepository artifactRepository = new DefaultArtifactRepository( "scratch", new File( getBasedir(), "target" + File.separatorChar + "scratch" ).toURI().toString(), layout );
+ projectBuilderConfiguration.setLocalRepository( artifactRepository );
+ project.setProjectBuilderConfiguration( projectBuilderConfiguration );
return project;
}
diff --git a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BlueprintComponentTest.java b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BlueprintComponentTest.java
index c92ed66..9bbbc72 100644
--- a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BlueprintComponentTest.java
+++ b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BlueprintComponentTest.java
@@ -36,14 +36,24 @@
import org.apache.felix.utils.manifest.Clause;
import org.apache.felix.utils.manifest.Parser;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Resource;
+import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.apache.maven.project.DefaultProjectBuilderConfiguration;
+import org.apache.maven.project.ProjectBuilderConfiguration;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.osgi.framework.Constants;
import aQute.bnd.osgi.Builder;
-public class BlueprintComponentTest extends TestCase
+public class BlueprintComponentTest extends AbstractMojoTestCase
{
public void testBlueprintServices() throws Exception
@@ -83,18 +93,28 @@
return new File( "target/tmp/basedir" );
}
};
- project.setGroupId( "group" );
+ project.setGroupId("group");
project.setArtifactId( "artifact" );
project.setVersion( "1.1.0.0" );
+ VersionRange versionRange = VersionRange.createFromVersion(project.getVersion());
+ ArtifactHandler artifactHandler = new DefaultArtifactHandler( "jar" );
+ Artifact artifact = new DefaultArtifact(project.getGroupId(),project.getArtifactId(),versionRange, null, "jar", null, artifactHandler);
+ project.setArtifact(artifact);
+
+ ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration();
+ projectBuilderConfiguration.setLocalRepository(null);
+ project.setProjectBuilderConfiguration(projectBuilderConfiguration);
+
Resource r = new Resource();
r.setDirectory( new File( "src/test/resources" ).getAbsoluteFile().getCanonicalPath() );
- r.setIncludes( Arrays.asList( "**/*.*" ) );
- project.addResource( r );
- project.addCompileSourceRoot( new File( "src/test/resources" ).getAbsoluteFile().getCanonicalPath() );
+ r.setIncludes(Arrays.asList("**/*.*"));
+ project.addResource(r);
+ project.addCompileSourceRoot(new File("src/test/resources").getAbsoluteFile().getCanonicalPath());
ManifestPlugin plugin = new ManifestPlugin();
plugin.setBuildDirectory( "target/tmp/basedir/target" );
- plugin.setOutputDirectory( new File( "target/tmp/basedir/target/classes" ) );
+ plugin.setOutputDirectory(new File("target/tmp/basedir/target/classes"));
+ setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default"));
Map instructions = new HashMap();
instructions.put( "service_mode", mode );
@@ -107,7 +127,8 @@
instructions.put( "Import-Service", "org.osgi.service.cm.ConfigurationAdmin;availability:=optional" );
Properties props = new Properties();
- Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
+ DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
+ Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) );
Manifest manifest = builder.getJar().getManifest();
String expSvc = manifest.getMainAttributes().getValue( Constants.EXPORT_SERVICE );
diff --git a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundleAllPluginTest.java b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundleAllPluginTest.java
index c3dab0c..b197e18 100644
--- a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundleAllPluginTest.java
+++ b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundleAllPluginTest.java
@@ -24,9 +24,15 @@
import java.util.Collections;
import java.util.Map;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.testing.stubs.ArtifactStub;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
/**
@@ -48,7 +54,7 @@
}
- private void init()
+ private void init() throws Exception
{
plugin = new BundleAllPlugin();
File baseDirectory = new File( getBasedir() );
@@ -56,6 +62,7 @@
plugin.setBuildDirectory( buildDirectory.getPath() );
File outputDirectory = new File( buildDirectory, "test-classes" );
plugin.setOutputDirectory( outputDirectory );
+ setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default"));
}
@@ -86,12 +93,11 @@
testFile.delete();
}
- ArtifactStub artifact = new ArtifactStub();
- artifact.setGroupId( "group" );
- artifact.setArtifactId( "artifact" );
- artifact.setVersion( "1.0.0.0" );
+ VersionRange versionRange = VersionRange.createFromVersion("1.0.0.0");
+ ArtifactHandler artifactHandler = new DefaultArtifactHandler( "jar" );
+ Artifact artifact = new DefaultArtifact("group","artifact",versionRange, null, "jar", null, artifactHandler);
- MavenProject project = new MavenProjectStub();
+ MavenProject project = getMavenProjectStub();
project.setGroupId( artifact.getGroupId() );
project.setArtifactId( artifact.getArtifactId() );
project.setVersion( artifact.getVersion() );
diff --git a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
index 53c4c10..7d7803e 100644
--- a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
+++ b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
@@ -34,6 +34,8 @@
import org.apache.maven.model.Organization;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.osgi.framework.Constants;
import aQute.bnd.osgi.Analyzer;
@@ -58,7 +60,8 @@
super.setUp();
plugin = new BundlePlugin();
plugin.setBuildDirectory( "." );
- plugin.setOutputDirectory( new File( getBasedir(), "target" + File.separatorChar + "scratch" ) );
+ plugin.setOutputDirectory(new File(getBasedir(), "target" + File.separatorChar + "scratch"));
+ setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default"));
}
@@ -233,7 +236,8 @@
+ "*;classifier=;type=jar;scope=runtime" );
Properties props = new Properties();
- Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
+ DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
+ Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) );
Manifest manifest = builder.getJar().getManifest();
String bcp = manifest.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
@@ -262,7 +266,8 @@
instructions.put( DependencyEmbedder.EMBED_DEPENDENCY, "!type=jar, !artifactId=c" );
Properties props = new Properties();
- Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
+ DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
+ Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) );
Manifest manifest = builder.getJar().getManifest();
String bcp = manifest.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
@@ -291,7 +296,8 @@
instructions.put( DependencyEmbedder.EMBED_DEPENDENCY, "c;type=jar,c;type=sources" );
Properties props = new Properties();
- Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
+ DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
+ Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) );
Manifest manifest = builder.getJar().getManifest();
String bcp = manifest.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
@@ -320,7 +326,8 @@
instructions.put( DependencyEmbedder.EMBED_DEPENDENCY, "artifactId=a|b" );
Properties props = new Properties();
- Builder builder = plugin.buildOSGiBundle( project, instructions, props, plugin.getClasspath( project ) );
+ DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
+ Builder builder = plugin.buildOSGiBundle( project, dependencyGraph, instructions, props, plugin.getClasspath( project, dependencyGraph ) );
Manifest manifest = builder.getJar().getManifest();
String bcp = manifest.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
@@ -341,20 +348,22 @@
artifacts.addAll( artifactFactory.getClassifiedArtifacts() );
artifacts.addAll( artifactFactory.getScopedArtifacts() );
- artifacts.addAll( artifactFactory.getTypedArtifacts() );
+ artifacts.addAll(artifactFactory.getTypedArtifacts());
MavenProject project = getMavenProjectStub();
- project.setDependencyArtifacts( artifacts );
+ project.setDependencyArtifacts(artifacts);
Properties props = new Properties();
+ DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
+ Jar[] classpath = plugin.getClasspath(project, dependencyGraph);
Map instructions1 = new HashMap();
instructions1.put( DependencyEmbedder.EMBED_DEPENDENCY, "!scope=compile" );
- Builder builder1 = plugin.buildOSGiBundle( project, instructions1, props, plugin.getClasspath( project ) );
+ Builder builder1 = plugin.buildOSGiBundle( project, dependencyGraph, instructions1, props, classpath );
Manifest manifest1 = builder1.getJar().getManifest();
Map instructions2 = new HashMap();
instructions2.put( DependencyEmbedder.EMBED_DEPENDENCY, "scope=!compile" );
- Builder builder2 = plugin.buildOSGiBundle( project, instructions2, props, plugin.getClasspath( project ) );
+ Builder builder2 = plugin.buildOSGiBundle( project, dependencyGraph, instructions2, props, classpath );
Manifest manifest2 = builder2.getJar().getManifest();
String bcp1 = manifest1.getMainAttributes().getValue( Constants.BUNDLE_CLASSPATH );
@@ -387,7 +396,8 @@
props.put( "4", new HashMap( 2 ) );
props.put( "1, two, 3.0", new char[5] );
- Builder builder = plugin.getOSGiBuilder( project, new HashMap(), props, plugin.getClasspath( project ) );
+ DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
+ Builder builder = plugin.getOSGiBuilder( project, new HashMap(), props, plugin.getClasspath( project, dependencyGraph ) );
File file = new File( getBasedir(), "target" + File.separatorChar + "test.props" );
builder.getProperties().store( new FileOutputStream( file ), "TEST" );