felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1349775 - in /felix/trunk/scrplugin/maven-scr-plugin: ./ src/main/java/org/apache/felix/scrplugin/mojo/
Date Wed, 13 Jun 2012 12:06:54 GMT
Author: cziegeler
Date: Wed Jun 13 12:06:53 2012
New Revision: 1349775

URL: http://svn.apache.org/viewvc?rev=1349775&view=rev
Log:
FELIX-3550 : Reimplement the SCR Generator

Added:
    felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenProjectScanner.java
  (with props)
Removed:
    felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenJavaClassDescriptorManager.java
Modified:
    felix/trunk/scrplugin/maven-scr-plugin/NOTICE
    felix/trunk/scrplugin/maven-scr-plugin/pom.xml
    felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenLog.java
    felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java

Modified: felix/trunk/scrplugin/maven-scr-plugin/NOTICE
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/maven-scr-plugin/NOTICE?rev=1349775&r1=1349774&r2=1349775&view=diff
==============================================================================
--- felix/trunk/scrplugin/maven-scr-plugin/NOTICE (original)
+++ felix/trunk/scrplugin/maven-scr-plugin/NOTICE Wed Jun 13 12:06:53 2012
@@ -1,5 +1,5 @@
 Apache Felix SCR Maven Plugin
-Copyright 2007-2011 The Apache Software Foundation
+Copyright 2007-2012 The Apache Software Foundation
 
 This product includes software developed at
 The Apache Software Foundation (http://www.apache.org/).

Modified: felix/trunk/scrplugin/maven-scr-plugin/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/maven-scr-plugin/pom.xml?rev=1349775&r1=1349774&r2=1349775&view=diff
==============================================================================
--- felix/trunk/scrplugin/maven-scr-plugin/pom.xml (original)
+++ felix/trunk/scrplugin/maven-scr-plugin/pom.xml Wed Jun 13 12:06:53 2012
@@ -29,7 +29,7 @@
     <groupId>org.apache.felix</groupId>
     <artifactId>maven-scr-plugin</artifactId>
 
-    <version>1.7.5-SNAPSHOT</version>
+    <version>1.8.0-SNAPSHOT</version>
     <packaging>maven-plugin</packaging>
 
     <name>Maven SCR Plugin</name>
@@ -57,9 +57,20 @@
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.scr.generator</artifactId>
-            <version>1.1.5-SNAPSHOT</version>
+            <version>1.2.0-SNAPSHOT</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <version>1.7.0-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>asm</groupId>
+            <artifactId>asm-all</artifactId>
+            <version>3.3.1</version>
+        </dependency>
     </dependencies>
     
     <build>

Modified: felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenLog.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenLog.java?rev=1349775&r1=1349774&r2=1349775&view=diff
==============================================================================
--- felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenLog.java
(original)
+++ felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenLog.java
Wed Jun 13 12:06:53 2012
@@ -18,144 +18,154 @@
  */
 package org.apache.felix.scrplugin.mojo;
 
-
 import org.apache.felix.scrplugin.Log;
 
-
 /**
  * The <code>MavenLog</code> class implements the {@link Log} interface using
  * the Maven logger created on instantiation.
  */
-public class MavenLog implements Log
-{
+public class MavenLog implements Log {
 
     private final org.apache.maven.plugin.logging.Log mavenLog;
 
-
-    MavenLog( org.apache.maven.plugin.logging.Log mavenLog )
-    {
+    MavenLog(final org.apache.maven.plugin.logging.Log mavenLog) {
         this.mavenLog = mavenLog;
     }
 
-
-    public void debug( String content, Throwable error )
-    {
-        mavenLog.debug( content, error );
+    /**
+     * @see org.apache.felix.scrplugin.Log#debug(java.lang.String,
+     *      java.lang.Throwable)
+     */
+    public void debug(final String content, final Throwable error) {
+        mavenLog.debug(content, error);
     }
 
-
-    public void debug( String content )
-    {
-        mavenLog.debug( content );
+    /**
+     * @see org.apache.felix.scrplugin.Log#debug(java.lang.String)
+     */
+    public void debug(final String content) {
+        mavenLog.debug(content);
     }
 
-
-    public void debug( Throwable error )
-    {
-        mavenLog.debug( error );
+    /**
+     * @see org.apache.felix.scrplugin.Log#debug(java.lang.Throwable)
+     */
+    public void debug(final Throwable error) {
+        mavenLog.debug(error);
     }
 
-
-    public void error( String content, Throwable error )
-    {
-        mavenLog.error( content, error );
+    /**
+     * @see org.apache.felix.scrplugin.Log#error(java.lang.String, java.lang.Throwable)
+     */
+    public void error(final String content, final Throwable error) {
+        mavenLog.error(content, error);
     }
 
-
-    public void error( String content, String location, int lineNumber )
-    {
-        if ( isErrorEnabled() )
-        {
-            final String message = formatMessage( content, location, lineNumber );
-            mavenLog.error( message );
-        }
+    /**
+     * @see org.apache.felix.scrplugin.Log#error(java.lang.String, java.lang.String, int)
+     */
+    public void error(final String content,
+            final String location,
+            final int lineNumber) {
+        final String message = formatMessage(content, location, lineNumber);
+        mavenLog.error(message);
     }
 
-
-    public void error( String content )
-    {
-        mavenLog.error( content );
+    /**
+     * @see org.apache.felix.scrplugin.Log#error(java.lang.String)
+     */
+    public void error(final String content) {
+        mavenLog.error(content);
     }
 
-
-    public void error( Throwable error )
-    {
-        mavenLog.error( error );
+    /**
+     * @see org.apache.felix.scrplugin.Log#error(java.lang.Throwable)
+     */
+    public void error(final Throwable error) {
+        mavenLog.error(error);
     }
 
-
-    public void info( String content, Throwable error )
-    {
-        mavenLog.info( content, error );
+    /**
+     * @see org.apache.felix.scrplugin.Log#info(java.lang.String, java.lang.Throwable)
+     */
+    public void info(final String content, final Throwable error) {
+        mavenLog.info(content, error);
     }
 
-
-    public void info( String content )
-    {
-        mavenLog.info( content );
+    /**
+     * @see org.apache.felix.scrplugin.Log#info(java.lang.String)
+     */
+    public void info(final String content) {
+        mavenLog.info(content);
     }
 
-
-    public void info( Throwable error )
-    {
-        mavenLog.info( error );
+    /**
+     * @see org.apache.felix.scrplugin.Log#info(java.lang.Throwable)
+     */
+    public void info(final Throwable error) {
+        mavenLog.info(error);
     }
 
-
-    public boolean isDebugEnabled()
-    {
+    /**
+     * @see org.apache.felix.scrplugin.Log#isDebugEnabled()
+     */
+    public boolean isDebugEnabled() {
         return mavenLog.isDebugEnabled();
     }
 
-
-    public boolean isErrorEnabled()
-    {
+    /**
+     * @see org.apache.felix.scrplugin.Log#isErrorEnabled()
+     */
+    public boolean isErrorEnabled() {
         return mavenLog.isErrorEnabled();
     }
 
-
-    public boolean isInfoEnabled()
-    {
+    /**
+     * @see org.apache.felix.scrplugin.Log#isInfoEnabled()
+     */
+    public boolean isInfoEnabled() {
         return mavenLog.isInfoEnabled();
     }
 
-
-    public boolean isWarnEnabled()
-    {
+    /**
+     * @see org.apache.felix.scrplugin.Log#isWarnEnabled()
+     */
+    public boolean isWarnEnabled() {
         return mavenLog.isWarnEnabled();
     }
 
-
-    public void warn( String content, Throwable error )
-    {
-        mavenLog.warn( content, error );
-    }
-
-
-    public void warn( String content, String location, int lineNumber )
-    {
-        if ( isWarnEnabled() )
-        {
-            final String message = formatMessage( content, location, lineNumber );
-            mavenLog.warn( message );
-        }
-    }
-
-
-    public void warn( String content )
-    {
-        mavenLog.warn( content );
+    /**
+     * @see org.apache.felix.scrplugin.Log#warn(java.lang.String, java.lang.Throwable)
+     */
+    public void warn(final String content, final Throwable error) {
+        mavenLog.warn(content, error);
+    }
+
+    /**
+     * @see org.apache.felix.scrplugin.Log#warn(java.lang.String, java.lang.String, int)
+     */
+    public void warn(final String content, final String location,
+            final int lineNumber) {
+        final String message = formatMessage(content, location, lineNumber);
+        mavenLog.warn(message);
+    }
+
+    /**
+     * @see org.apache.felix.scrplugin.Log#warn(java.lang.String)
+     */
+    public void warn(final String content) {
+        mavenLog.warn(content);
+    }
+
+    /**
+     * @see org.apache.felix.scrplugin.Log#warn(java.lang.Throwable)
+     */
+    public void warn(final Throwable error) {
+        mavenLog.warn(error);
     }
 
-
-    public void warn( Throwable error )
-    {
-        mavenLog.warn( error );
-    }
-
-
-    private String formatMessage( String content, String location, int lineNumber )
-    {
+    private String formatMessage(final String content, final String location,
+            final int lineNumber) {
         return content + " at " + location + ":" + lineNumber;
     }
 }

Added: felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenProjectScanner.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenProjectScanner.java?rev=1349775&view=auto
==============================================================================
--- felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenProjectScanner.java
(added)
+++ felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenProjectScanner.java
Wed Jun 13 12:06:53 2012
@@ -0,0 +1,163 @@
+/*
+ * 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.
+ */
+package org.apache.felix.scrplugin.mojo;
+
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.felix.scrplugin.Log;
+import org.apache.felix.scrplugin.helper.StringUtils;
+import org.apache.felix.scrplugin.scanner.Source;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.DirectoryScanner;
+
+
+public class MavenProjectScanner {
+
+    private final MavenProject project;
+
+    private final String includeString;
+
+    private final String excludeString;
+
+    private final Log log;
+
+    public MavenProjectScanner( final MavenProject project,
+            final String includeString,
+            final String excludeString,
+            final Log log) {
+        this.project = project;
+        this.includeString = includeString;
+        this.excludeString = excludeString;
+        this.log = log;
+    }
+
+    /**
+     * Return all sources.
+     */
+    public Collection<Source> getSources() {
+        final ArrayList<Source> files = new ArrayList<Source>();
+
+        @SuppressWarnings("unchecked")
+        final Iterator<String> i = project.getCompileSourceRoots().iterator();
+
+        // FELIX-509: check for excludes
+        String[] includes = new String[] { "**/*.java" };
+        if ( includeString != null ) {
+        	includes = StringUtils.split( includeString, "," );
+        }
+
+        final String[] excludes;
+        if ( excludeString != null ) {
+            excludes = StringUtils.split( excludeString, "," );
+        } else {
+            excludes = null;
+        }
+
+        while ( i.hasNext() ) {
+            final String tree = i.next();
+            final File directory = new File( tree );
+            if ( !directory.exists() ) {
+                log.warn("Source tree does not exist. Ignoring " + tree);
+                continue;
+            }
+            log.debug( "Scanning source tree " + tree );
+            final DirectoryScanner scanner = new DirectoryScanner();
+            scanner.setBasedir( directory );
+
+            if ( excludes != null && excludes.length > 0 ) {
+                scanner.setExcludes( excludes );
+            }
+            scanner.addDefaultExcludes();
+            scanner.setIncludes( includes );
+
+            scanner.scan();
+
+            for ( final String fileName : scanner.getIncludedFiles() ) {
+                files.add( new Source() {
+
+                    public File getFile() {
+                        return new File(directory, fileName);
+                    }
+
+                    public String getClassName() {
+                        // remove ".java"
+                        String name = fileName.substring(0, fileName.length() - 5);
+                        return name.replace(File.separatorChar, '/').replace('/', '.');
+                    }
+                });
+            }
+        }
+
+        return files;
+    }
+
+    /**
+     * Return all dependencies
+     */
+    public List<File> getDependencies() {
+        final ArrayList<File> dependencies = new ArrayList<File>();
+
+        @SuppressWarnings("unchecked")
+        final Iterator<Artifact> it = project.getArtifacts().iterator();
+        while ( it.hasNext() ) {
+            final Artifact declared = it.next();
+            this.log.debug( "Checking artifact " + declared );
+            if ( this.isJavaArtifact( declared ) ) {
+                if ( Artifact.SCOPE_COMPILE.equals( declared.getScope() )
+                    || Artifact.SCOPE_RUNTIME.equals( declared.getScope() )
+                    || Artifact.SCOPE_PROVIDED.equals( declared.getScope() )
+                    || Artifact.SCOPE_SYSTEM.equals( declared.getScope() ) ) {
+                    this.log.debug( "Resolving artifact " + declared );
+                    if ( declared.getFile() != null ) {
+                        dependencies.add( declared.getFile() );
+                    } else {
+                        this.log.debug( "Unable to resolve artifact " + declared );
+                    }
+                } else {
+                    this.log.debug( "Artifact " + declared + " has not scope compile or runtime,
but "
+                        + declared.getScope() );
+                }
+            } else {
+                this.log.debug( "Artifact " + declared + " is not a java artifact, type is
" + declared.getType() );
+            }
+        }
+
+        return dependencies;
+    }
+
+
+    /**
+     * Check if the artifact is a java artifact (jar or bundle)
+     */
+    private boolean isJavaArtifact( Artifact artifact ) {
+        if ( "jar".equals( artifact.getType() ) ) {
+            return true;
+        }
+        if ( "bundle".equals( artifact.getType() ) ) {
+            return true;
+        }
+        return false;
+    }
+}

Propchange: felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenProjectScanner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenProjectScanner.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/MavenProjectScanner.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java?rev=1349775&r1=1349774&r2=1349775&view=diff
==============================================================================
--- felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java
(original)
+++ felix/trunk/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java
Wed Jun 13 12:06:53 2012
@@ -22,20 +22,34 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.*;
-
-import org.apache.felix.scrplugin.*;
+import java.util.ArrayList;
+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.StringTokenizer;
+
+import org.apache.felix.scrplugin.Options;
+import org.apache.felix.scrplugin.Project;
+import org.apache.felix.scrplugin.Result;
+import org.apache.felix.scrplugin.SCRDescriptorException;
+import org.apache.felix.scrplugin.SCRDescriptorFailureException;
+import org.apache.felix.scrplugin.SCRDescriptorGenerator;
+import org.apache.felix.scrplugin.description.SpecVersion;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.apache.maven.model.Resource;
-import org.apache.maven.plugin.*;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.StringUtils;
 
 /**
- * The <code>SCRDescriptorMojo</code>
- * generates a service descriptor file based on annotations found in the sources.
+ * The <code>SCRDescriptorMojo</code> generates a service descriptor file based
+ * on annotations found in the sources.
  *
  * @goal scr
  * @phase process-classes
@@ -67,7 +81,8 @@ public class SCRDescriptorMojo extends A
      *
      * @see #checkAnnotationArtifact(Artifact)
      */
-    private static final ArtifactVersion SCR_ANN_MIN_VERSION = new DefaultArtifactVersion("1.5.1");
+    private static final ArtifactVersion SCR_ANN_MIN_VERSION = new DefaultArtifactVersion(
+            "1.6.9");
 
     /**
      * @parameter expression="${project.build.directory}/scr-plugin-generated"
@@ -88,7 +103,8 @@ public class SCRDescriptorMojo extends A
     /**
      * Name of the generated descriptor.
      *
-     * @parameter expression="${scr.descriptor.name}" default-value="serviceComponents.xml"
+     * @parameter expression="${scr.descriptor.name}"
+     *            default-value="serviceComponents.xml"
      */
     private String finalName;
 
@@ -101,38 +117,25 @@ public class SCRDescriptorMojo extends A
 
     /**
      * This flag controls the generation of the bind/unbind methods.
+     *
      * @parameter default-value="true"
      */
     private boolean generateAccessors;
 
     /**
-     * This flag controls whether the javadoc source code will be scanned for
-     * tags.
-     * @parameter default-value="true"
-     */
-    protected boolean parseJavadoc;
-
-    /**
-     * This flag controls whether the annotations in the sources will be
-     * processed.
-     * @parameter default-value="true"
-     */
-    protected boolean processAnnotations;
-
-    /**
      * In strict mode the plugin even fails on warnings.
+     *
      * @parameter default-value="false"
      */
     protected boolean strictMode;
 
-    
     /**
      * The comma separated list of tokens to include when processing sources.
-     * 
+     *
      * @parameter alias="includes"
      */
     private String sourceIncludes;
-    
+
     /**
      * The comma separated list of tokens to exclude when processing sources.
      *
@@ -149,87 +152,97 @@ public class SCRDescriptorMojo extends A
 
     /**
      * Allows to define additional implementations of the interface
-     * {@link org.apache.felix.scrplugin.tags.annotation.AnnotationTagProvider}
-     * that provide mappings from custom annotations to
-     * {@link org.apache.felix.scrplugin.tags.JavaTag} implementations.
-     * List of full qualified class file names.
+     * {@link org.apache.felix.scrplugin.AnnotationProcessor} that provide
+     * mappings from custom annotations to descriptions.
      *
      * @parameter
      */
-    private String[] annotationTagProviders = {};
+    private String[] annotationProcessors = {};
 
     /**
-     * The version of the DS spec this plugin generates a descriptor for.
-     * By default the version is detected by the used tags.
+     * The version of the DS spec this plugin generates a descriptor for. By
+     * default the version is detected by the used tags.
+     *
      * @parameter
      */
     private String specVersion;
 
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        // create the log for the generator
+        final org.apache.felix.scrplugin.Log scrLog = new MavenLog(getLog());
+        // create the class loader
+        final ClassLoader classLoader = new URLClassLoader(getClassPath(), this
+                .getClass().getClassLoader());
+
+        // create project
+        final MavenProjectScanner scanner = new MavenProjectScanner(
+                this.project, this.sourceIncludes, this.sourceExcludes, scrLog);
+
+        final Project project = new Project();
+        project.setClassLoader(classLoader);
+        project.setDependencies(scanner.getDependencies());
+        project.setSources(scanner.getSources());
+        project.setClassesDirectory(this.project.getBuild().getOutputDirectory());
+
+        // create options
+        final Options options = new Options();
+        options.setGenerateAccessors(generateAccessors);
+        options.setStrictMode(strictMode);
+        options.setProperties(properties);
+        options.setSpecVersion(SpecVersion.fromName(specVersion));
+        if ( specVersion != null && options.getSpecVersion() == null ) {
+            throw new MojoExecutionException("Unknown spec version specified: " + specVersion);
+        }
+        options.setAnnotationProcessors(annotationProcessors);
+        try {
 
-    public void execute() throws MojoExecutionException, MojoFailureException
-    {
-        try
-        {
-            final org.apache.felix.scrplugin.Log scrLog = new MavenLog( getLog() );
-
-            final ClassLoader classLoader = new URLClassLoader( getClassPath(), this.getClass().getClassLoader()
);
-            final JavaClassDescriptorManager jManager = new MavenJavaClassDescriptorManager(
project, scrLog,
-                classLoader, this.annotationTagProviders, this.sourceIncludes, this.sourceExcludes,
this.parseJavadoc,
-                this.processAnnotations );
-
-            final SCRDescriptorGenerator generator = new SCRDescriptorGenerator( scrLog );
+            final SCRDescriptorGenerator generator = new SCRDescriptorGenerator(
+                    scrLog);
 
             // setup from plugin configuration
-            generator.setOutputDirectory( outputDirectory );
-            generator.setDescriptorManager( jManager );
-            generator.setFinalName( finalName );
-            generator.setMetaTypeName( metaTypeName );
-            generator.setGenerateAccessors( generateAccessors );
-            generator.setStrictMode( strictMode );
-            generator.setProperties( properties );
-            generator.setSpecVersion( specVersion );
-
-            if ( generator.execute() )
-            {
-                setServiceComponentHeader();
-                addResources();
-            }
-        }
-        catch ( SCRDescriptorException sde )
-        {
-            throw new MojoExecutionException( sde.getMessage(), sde.getCause() );
-        }
-        catch ( SCRDescriptorFailureException sdfe )
-        {
-            throw ( MojoFailureException ) new MojoFailureException( sdfe.getMessage() ).initCause(
sdfe );
+            generator.setOutputDirectory(outputDirectory);
+            generator.setOptions(options);
+            generator.setProject(project);
+            generator.setFinalName(finalName);
+            generator.setMetaTypeName(metaTypeName);
+
+            final Result result = generator.execute();
+            this.setServiceComponentHeader(result.getScrFiles());
+            this.updateProjectResources();
+
+        } catch (final SCRDescriptorException sde) {
+            throw new MojoExecutionException(sde.getMessage(), sde.getCause());
+        } catch (SCRDescriptorFailureException sdfe) {
+            throw (MojoFailureException) new MojoFailureException(
+                    sdfe.getMessage()).initCause(sdfe);
         }
     }
 
-    private URL[] getClassPath() throws MojoFailureException{
+    private URL[] getClassPath() throws MojoFailureException {
         @SuppressWarnings("unchecked")
         List<Artifact> artifacts = this.project.getCompileArtifacts();
         ArrayList<URL> path = new ArrayList<URL>();
 
-        try
-        {
-            path.add(new File( this.project.getBuild().getOutputDirectory() ).toURI().toURL());
-        }
-        catch ( IOException ioe )
-        {
-            throw new MojoFailureException( "Unable to add target directory to classloader.");
+        try {
+            path.add(new File(this.project.getBuild().getOutputDirectory())
+                    .toURI().toURL());
+        } catch (final IOException ioe) {
+            throw new MojoFailureException(
+                    "Unable to add target directory to classloader.");
         }
 
-        for (Iterator<Artifact> ai=artifacts.iterator(); ai.hasNext(); ) {
+        for (Iterator<Artifact> ai = artifacts.iterator(); ai.hasNext();) {
             Artifact a = ai.next();
             assertMinScrAnnotationArtifactVersion(a);
             try {
                 path.add(a.getFile().toURI().toURL());
             } catch (IOException ioe) {
-                throw new MojoFailureException("Unable to get compile class loader.");
+                throw new MojoFailureException(
+                        "Unable to get compile class loader.");
             }
         }
 
-        return path.toArray( new URL[path.size()] );
+        return path.toArray(new URL[path.size()]);
     }
 
     /**
@@ -240,61 +253,83 @@ public class SCRDescriptorMojo extends A
      * SCR Annotation libraries do not produce descriptors any more. If the
      * artifact is not this method silently returns.
      *
-     * @param a The artifact to check and assert
+     * @param a
+     *            The artifact to check and assert
      * @see #SCR_ANN_ARTIFACTID
      * @see #SCR_ANN_GROUPID
      * @see #SCR_ANN_MIN_VERSION
-     * @throws MojoFailureException If the artifact refers to the SCR Annotation
-     *             library with a version less than {@link #SCR_ANN_MIN_VERSION}
-     */
-    private void assertMinScrAnnotationArtifactVersion(Artifact a) throws MojoFailureException
-    {
-        if (SCR_ANN_ARTIFACTID.equals(a.getArtifactId()) && SCR_ANN_GROUPID.equals(a.getGroupId()))
-        {
+     * @throws MojoFailureException
+     *             If the artifact refers to the SCR Annotation library with a
+     *             version less than {@link #SCR_ANN_MIN_VERSION}
+     */
+    @SuppressWarnings("unchecked")
+    private void assertMinScrAnnotationArtifactVersion(final Artifact a)
+            throws MojoFailureException {
+        if (SCR_ANN_ARTIFACTID.equals(a.getArtifactId())
+                && SCR_ANN_GROUPID.equals(a.getGroupId())) {
             // assert minimal version number
-            ArtifactVersion aVersion = new DefaultArtifactVersion(a.getBaseVersion());
-            if (SCR_ANN_MIN_VERSION.compareTo(aVersion) > 0)
-            {
+            final ArtifactVersion aVersion = new DefaultArtifactVersion(a.getBaseVersion());
+            if (SCR_ANN_MIN_VERSION.compareTo(aVersion) > 0) {
                 getLog().error("Project depends on " + a);
-                getLog().error("Minimum required version is " + SCR_ANN_MIN_VERSION);
-                throw new MojoFailureException("Please use org.apache.felix:org.apache.felix.scr.annotations
version "
-                    + SCR_ANN_MIN_VERSION + " or newer.");
+                getLog().error(
+                        "Minimum required version is " + SCR_ANN_MIN_VERSION);
+                throw new MojoFailureException(
+                        "Please use org.apache.felix:org.apache.felix.scr.annotations version
"
+                                + SCR_ANN_MIN_VERSION + " or newer.");
             }
         }
     }
 
-    private void setServiceComponentHeader()
-    {
-        final File descriptorFile = StringUtils.isEmpty( this.finalName ) ? null : new File(
new File(
-            this.outputDirectory, "OSGI-INF" ), this.finalName );
-        if ( descriptorFile.exists() )
-        {
-            String svcComp = project.getProperties().getProperty( "Service-Component" );
-            final String svcPath = "OSGI-INF/" + finalName;
-            svcComp = ( svcComp == null ) ? svcPath :
-                svcComp.contains(svcPath) ? svcComp : svcComp + ", " + svcPath;
-            project.getProperties().setProperty( "Service-Component", svcComp );
+    /**
+     * Set the service component header based on the scr files.
+     */
+    private void setServiceComponentHeader(final List<String> files) {
+        if ( files != null && files.size() > 0 ) {
+            final String svcHeader = project.getProperties().getProperty("Service-Component");
+            final Set<String> xmlFiles = new HashSet<String>();
+            if ( svcHeader != null ) {
+                final StringTokenizer st = new StringTokenizer(svcHeader, ",");
+                while ( st.hasMoreTokens() ) {
+                    final String token = st.nextToken();
+                    xmlFiles.add(token.trim());
+                }
+            }
+
+            for(final String path : files) {
+                xmlFiles.add(path);
+            }
+            final StringBuilder sb = new StringBuilder();
+            boolean first = true;
+            for(final String entry : xmlFiles) {
+                if ( !first ) {
+                    sb.append(", ");
+                } else {
+                    first = false;
+                }
+                sb.append(entry);
+            }
+            project.getProperties().setProperty("Service-Component", sb.toString());
         }
     }
 
-
-    private void addResources()
-    {
+    /**
+     * Update the Maven project resources.
+     */
+    private void updateProjectResources() {
         // now add the descriptor directory to the maven resources
         final String ourRsrcPath = this.outputDirectory.getAbsolutePath();
         boolean found = false;
         @SuppressWarnings("unchecked")
-        final Iterator<Resource> rsrcIterator = this.project.getResources().iterator();
-        while ( !found && rsrcIterator.hasNext() )
-        {
+        final Iterator<Resource> rsrcIterator = this.project.getResources()
+                .iterator();
+        while (!found && rsrcIterator.hasNext()) {
             final Resource rsrc = rsrcIterator.next();
-            found = rsrc.getDirectory().equals( ourRsrcPath );
+            found = rsrc.getDirectory().equals(ourRsrcPath);
         }
-        if ( !found )
-        {
+        if (!found) {
             final Resource resource = new Resource();
-            resource.setDirectory( this.outputDirectory.getAbsolutePath() );
-            this.project.addResource( resource );
+            resource.setDirectory(this.outputDirectory.getAbsolutePath());
+            this.project.addResource(resource);
         }
     }
 }



Mime
View raw message