You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ca...@apache.org on 2007/02/15 02:53:00 UTC

svn commit: r507772 - in /maven/sandbox/plugins/maven-bundle-plugin/src: main/java/org/apache/felix/tools/maven2/bundleplugin/ test/java/org/apache/felix/tools/maven2/bundleplugin/

Author: carlos
Date: Wed Feb 14 17:52:59 2007
New Revision: 507772

URL: http://svn.apache.org/viewvc?view=rev&rev=507772
Log:
Honor each project dependency tree, not just current one as it breaks when exclusions are used anywhere in the tree
Added some error checks 

Added:
    maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/AbstractBundlePluginTest.java   (with props)
    maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPluginTest.java   (with props)
Modified:
    maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPlugin.java
    maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java
    maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundlePluginTest.java

Modified: maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPlugin.java
URL: http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPlugin.java?view=diff&rev=507772&r1=507771&r2=507772
==============================================================================
--- maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPlugin.java (original)
+++ maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPlugin.java Wed Feb 14 17:52:59 2007
@@ -19,12 +19,17 @@
 package org.apache.felix.tools.maven2.bundleplugin;
 
 import java.io.File;
+import java.io.FilenameFilter;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.jar.Manifest;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
@@ -57,6 +62,8 @@
     extends ManifestPlugin
 {
 
+    private static final Pattern SNAPSHOT_VERSION_PATTERN = Pattern.compile( "[0-9]{8}_[0-9]{6}_[0-9]+" );
+
     /**
      * The Maven Project.
      *
@@ -116,10 +123,13 @@
      */
     private MavenProjectBuilder mavenProjectBuilder;
 
+    private Set artifactsProcessed;
+
     public void execute()
         throws MojoExecutionException
     {
-        bundle( project );
+        artifactsProcessed = new HashSet();
+        bundleAll( project );
     }
 
     /**
@@ -128,9 +138,16 @@
      * @param project
      * @throws MojoExecutionException
      */
-    private void bundle( MavenProject project )
+    private void bundleAll( MavenProject project )
         throws MojoExecutionException
     {
+
+        if ( artifactsProcessed.contains( project.getArtifact() ) )
+        {
+            getLog().info( "Ignoring project already processed " + project.getArtifact() );
+            return;
+        }
+
         DependencyTree dependencyTree;
 
         try
@@ -148,7 +165,7 @@
         for ( Iterator it = dependencyTree.inverseIterator(); it.hasNext(); )
         {
             DependencyNode node = (DependencyNode) it.next();
-            if (!it.hasNext())
+            if ( !it.hasNext() )
             {
                 /* this is the root, current project */
                 break;
@@ -156,31 +173,41 @@
             Artifact artifact = resolveArtifact( node.getArtifact() );
             if ( ( artifact.getScope() == Artifact.SCOPE_COMPILE ) || ( artifact.getScope() == Artifact.SCOPE_RUNTIME ) )
             {
-                bundle( artifact );
+                MavenProject childProject;
+                try
+                {
+                    childProject = mavenProjectBuilder.buildFromRepository( artifact, remoteRepositories,
+                                                                            localRepository, true );
+                }
+                catch ( ProjectBuildingException e )
+                {
+                    throw new MojoExecutionException( "Unable to build project object for artifact " + artifact, e );
+                }
+                childProject.setArtifact( artifact );
+                bundleAll( childProject );
             }
         }
+
+        if ( this.project != project )
+        {
+            bundle( project );
+            artifactsProcessed.add( project.getArtifact() );
+        }
     }
 
     /**
-     * Bundle one artifact only
+     * Bundle one project only without building its childre
      * 
-     * @param artifact
+     * @param project
      * @throws MojoExecutionException
      */
-    private void bundle( Artifact artifact )
+    void bundle( MavenProject project )
         throws MojoExecutionException
     {
+        Artifact artifact = project.getArtifact();
         getLog().info( "Bundling " + artifact );
-
-        MavenProject project;
-        try
-        {
-            project = mavenProjectBuilder.buildFromRepository( artifact, remoteRepositories, localRepository, true );
-        }
-        catch ( ProjectBuildingException e )
-        {
-            throw new MojoExecutionException( "Unable to build project object for artifact " + artifact, e );
-        }
+        getLog().info( "  Artifacts:" + project.getArtifacts() );
+        getLog().info( "  Dependency Artifacts:" + project.getDependencyArtifacts() );
 
         try
         {
@@ -188,10 +215,17 @@
             instructions.put( Analyzer.EXPORT_PACKAGE, "*" );
 
             project.setFile( getFile( artifact ) );
+            File outputFile = getOutputFile( artifact );
+
+            if ( project.getFile().equals( outputFile ) )
+            {
+                throw new IllegalStateException( "Trying to read and write to the same file, try cleaning: "
+                    + outputFile );
+            }
+
             Manifest manifest = getManifest( project, getClasspath( project ) );
             Jar osgiJar = new Jar( project.getArtifactId(), project.getFile() );
             osgiJar.setManifest( manifest );
-            File outputFile = getOutputFile( artifact );
             outputFile.getParentFile().mkdirs();
             osgiJar.write( outputFile );
         }
@@ -213,10 +247,14 @@
         return getBundleName( project.getArtifact() );
     }
 
+    private String getBundleNameFirstPart( Artifact artifact )
+    {
+        return artifact.getGroupId() + "." + artifact.getArtifactId();
+    }
+
     private String getBundleName( Artifact artifact )
     {
-        return artifact.getGroupId() + "." + artifact.getArtifactId() + "_"
-            + convertVersionToOsgi( artifact.getVersion() ) + ".jar";
+        return getBundleNameFirstPart( artifact ) + "_" + convertVersionToOsgi( artifact.getVersion() ) + ".jar";
     }
 
     /**
@@ -224,15 +262,80 @@
      * 
      * @param artifact
      */
-    protected File getFile( Artifact artifact )
+    protected File getFile( final Artifact artifact )
     {
+        File bundle = null;
+
         /* if bundle was already built use it instead of jar from repo */
-        File bundle = getOutputFile( artifact );
-        if ( bundle.exists() )
+        File outputFile = getOutputFile( artifact );
+        if ( outputFile.exists() )
+        {
+            bundle = outputFile;
+        }
+
+        /*
+         * Find snapshots in output folder, eg. 2.1-SNAPSHOT will match 2.1.0.20070207_193904_2
+         * TODO there has to be another way to do this using Maven libs 
+         */
+        if ( ( bundle == null ) && artifact.isSnapshot() )
+        {
+            final File buildDirectory = new File( getBuildDirectory() );
+            File[] files = buildDirectory.listFiles( new FilenameFilter()
+            {
+                public boolean accept( File dir, String name )
+                {
+                    if ( dir.equals( buildDirectory ) && snapshotMatch( artifact, name ) )
+                    {
+                        return true;
+                    }
+                    return false;
+                }
+            } );
+            if ( files.length > 1 )
+            {
+                throw new RuntimeException( "More than one previously built bundle matches for artifact " + artifact
+                    + " : " + Arrays.asList( files ) );
+            }
+            if ( files.length == 1 )
+            {
+                bundle = files[0];
+            }
+        }
+
+        if ( bundle != null )
         {
+            getLog().info( "Using previously built OSGi bundle for " + artifact + " in " + bundle );
             return bundle;
         }
         return super.getFile( artifact );
+    }
+
+    /**
+     * Check that the bundleName provided correspond to the artifact provided.
+     * Used to determine when the bundle name is a timestamped snapshot and the artifact is a snapshot not timestamped.
+     * 
+     * @param artifact artifact with snapshot version
+     * @param bundleName bundle file name 
+     * @return if both represent the same artifact and version, forgetting about the snapshot timestamp
+     */
+    boolean snapshotMatch( Artifact artifact, String bundleName )
+    {
+        String artifactBundleName = getBundleName( artifact );
+        int i = artifactBundleName.indexOf( "SNAPSHOT" );
+        if ( i < 0 )
+        {
+            return false;
+        }
+        artifactBundleName = artifactBundleName.substring( 0, i );
+
+        if ( bundleName.startsWith( artifactBundleName ) )
+        {
+            /* it's the same artifact groupId and artifactId */
+            String timestamp = bundleName.substring( artifactBundleName.length(), bundleName.lastIndexOf( ".jar" ) );
+            Matcher m = SNAPSHOT_VERSION_PATTERN.matcher( timestamp );
+            return m.matches();
+        }
+        return false;
     }
 
     protected File getOutputFile( Artifact artifact )

Modified: maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java
URL: http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java?view=diff&rev=507772&r1=507771&r2=507772
==============================================================================
--- maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java (original)
+++ maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java Wed Feb 14 17:52:59 2007
@@ -230,13 +230,17 @@
     list.add(new Jar(".", outputDirectory));
   }
  
-  Set artifacts = project.getArtifacts();
+  Set artifacts = project.getDependencyArtifacts();
   for (Iterator it = artifacts.iterator(); it.hasNext();) {
    Artifact artifact = (Artifact) it.next();
    if (Artifact.SCOPE_COMPILE.equals(artifact.getScope()) 
      || Artifact.SCOPE_SYSTEM.equals(artifact.getScope()) 
      || Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) {
-    Jar jar = new Jar(artifact.getArtifactId(), getFile(artifact));
+    File file = getFile(artifact);
+    if (file == null) {
+        throw new RuntimeException("File is not available for artifact " + artifact + " in project " + project.getArtifact());
+    }
+    Jar jar = new Jar(artifact.getArtifactId(), file);
     list.add(jar);
    }
   }
@@ -364,6 +368,10 @@
   return buildDirectory;
  }
 
+ void setBuildDirectory(String buildirectory) {
+  this.buildDirectory = buildirectory;    
+ }
+
  /**
   * Get a list of packages inside a Jar
   * 
@@ -417,5 +425,13 @@
      properties.put("project.build.outputdirectory", outputDirectory );
      
      return properties;
+ }
+ 
+ void setBasedir(File basedir){
+     this.baseDir = basedir;
+ }
+
+ void setOutputDirectory(File outputDirectory){
+     this.outputDirectory = outputDirectory;
  }
 }

Added: maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/AbstractBundlePluginTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/AbstractBundlePluginTest.java?view=auto&rev=507772
==============================================================================
--- maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/AbstractBundlePluginTest.java (added)
+++ maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/AbstractBundlePluginTest.java Wed Feb 14 17:52:59 2007
@@ -0,0 +1,52 @@
+package org.apache.felix.tools.maven2.bundleplugin;
+
+/*
+ * 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 org.apache.maven.plugin.testing.stubs.ArtifactStub;
+import org.codehaus.plexus.PlexusTestCase;
+
+/**
+ * Common methods for bundle plugin testing
+ * 
+ * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
+ * @version $Id$
+ */
+public abstract class AbstractBundlePluginTest
+    extends PlexusTestCase
+{
+
+    protected ArtifactStub getArtifactStub()
+    {
+        ArtifactStub artifact = new ArtifactStub();
+        artifact.setGroupId( "group" );
+        artifact.setArtifactId( "artifact" );
+        artifact.setVersion( "1.0" );
+        return artifact;
+    }
+
+    protected File getTestBundle()
+    {
+        String osgiBundleFileName = "org.apache.maven.maven-model_2.1.0.SNAPSHOT.jar";
+        return getTestFile( getBasedir(), "src/test/resources/" + osgiBundleFileName );
+    }
+
+}
\ No newline at end of file

Propchange: maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/AbstractBundlePluginTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/AbstractBundlePluginTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPluginTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPluginTest.java?view=auto&rev=507772
==============================================================================
--- maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPluginTest.java (added)
+++ maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPluginTest.java Wed Feb 14 17:52:59 2007
@@ -0,0 +1,102 @@
+package org.apache.felix.tools.maven2.bundleplugin;
+
+/*
+ * 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.Collections;
+
+import org.apache.maven.plugin.testing.stubs.ArtifactStub;
+import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+
+/**
+ * Test for {@link BundleAllPlugin}
+ * 
+ * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
+ * @version $Id$
+ */
+public class BundleAllPluginTest
+    extends AbstractBundlePluginTest
+{
+
+    private BundleAllPlugin plugin;
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+        init();
+    }
+
+    private void init()
+    {
+        plugin = new BundleAllPlugin();
+        File basedir = new File( getBasedir() );
+        plugin.setBasedir( basedir );
+        File buildDirectory = new File( basedir, "target" );
+        plugin.setBuildDirectory( buildDirectory.getPath() );
+        File outputDirectory = new File( buildDirectory, "classes" );
+        plugin.setOutputDirectory( outputDirectory );
+    }
+
+    public void testSnapshotMatch()
+    {
+        ArtifactStub artifact = getArtifactStub();
+        String bundleName;
+
+        artifact.setVersion( "2.1-SNAPSHOT" );
+        bundleName = "group.artifact_2.1.0.20070207_193904_2.jar";
+
+        assertTrue( plugin.snapshotMatch( artifact, bundleName ) );
+
+        artifact.setVersion( "2-SNAPSHOT" );
+        assertFalse( plugin.snapshotMatch( artifact, bundleName ) );
+
+        artifact.setArtifactId( "artifactx" );
+        artifact.setVersion( "2.1-SNAPSHOT" );
+        assertFalse( plugin.snapshotMatch( artifact, bundleName ) );
+    }
+
+//    public void testRewriting()
+//        throws Exception
+//    {
+//
+//        MavenProjectStub project = new MavenProjectStub();
+//        project.setArtifact( getArtifactStub() );
+//        project.getArtifact().setFile( getTestBundle() );
+//        project.setDependencyArtifacts( Collections.EMPTY_SET );
+//        project.setVersion( project.getArtifact().getVersion() );
+//
+//        File output = new File( plugin.getBuildDirectory(), plugin.getBundleName( project ) );
+//        boolean delete = output.delete();
+//
+//        plugin.bundle( project );
+//
+//        init();
+//        try
+//        {
+//            plugin.bundle( project );
+//            fail();
+//        }
+//        catch ( RuntimeException e )
+//        {
+//            // expected
+//        }
+//    }
+}

Propchange: maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPluginTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundleAllPluginTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundlePluginTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundlePluginTest.java?view=diff&rev=507772&r1=507771&r2=507772
==============================================================================
--- maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundlePluginTest.java (original)
+++ maven/sandbox/plugins/maven-bundle-plugin/src/test/java/org/apache/felix/tools/maven2/bundleplugin/BundlePluginTest.java Wed Feb 14 17:52:59 2007
@@ -25,12 +25,10 @@
 
 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
 import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.PlexusTestCase;
 import org.codehaus.plexus.archiver.ArchiverException;
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 
 import aQute.lib.osgi.Analyzer;
-import aQute.lib.osgi.Builder;
 import aQute.lib.osgi.Jar;
 
 /**
@@ -40,10 +38,17 @@
  * @version $Id$
  */
 public class BundlePluginTest
-    extends PlexusTestCase
+    extends AbstractBundlePluginTest
 {
 
-    BundlePlugin plugin = new BundlePlugin();
+    private BundlePlugin plugin;
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+        plugin = new BundlePlugin();
+    }
 
     public void testConvertVersionToOsgi()
     {
@@ -89,8 +94,7 @@
     public void testReadExportedModules()
         throws Exception
     {
-        String osgiBundleFileName = "org.apache.maven.maven-model_2.1.0.SNAPSHOT.jar";
-        File osgiBundleFile = new File( getBasedir(), "src/test/resources/" + osgiBundleFileName );
+        File osgiBundleFile = getTestBundle();
 
         assertTrue( osgiBundleFile.exists() );