You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2012/05/22 08:05:20 UTC

svn commit: r1341310 - /felix/sandbox/cziegeler/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java

Author: cziegeler
Date: Tue May 22 06:05:20 2012
New Revision: 1341310

URL: http://svn.apache.org/viewvc?rev=1341310&view=rev
Log:
New scr plugin

Modified:
    felix/sandbox/cziegeler/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java

Modified: felix/sandbox/cziegeler/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java?rev=1341310&r1=1341309&r2=1341310&view=diff
==============================================================================
--- felix/sandbox/cziegeler/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java (original)
+++ felix/sandbox/cziegeler/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java Tue May 22 06:05:20 2012
@@ -45,9 +45,9 @@ import org.apache.maven.project.MavenPro
 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
  * @threadSafe
@@ -58,7 +58,7 @@ public class SCRDescriptorMojo extends A
 
     /**
      * The groupID of the SCR Annotation library
-     *
+     * 
      * @see #SCR_ANN_MIN_VERSION
      * @see #checkAnnotationArtifact(Artifact)
      */
@@ -66,7 +66,7 @@ public class SCRDescriptorMojo extends A
 
     /**
      * The artifactID of the SCR Annotation library.
-     *
+     * 
      * @see #SCR_ANN_MIN_VERSION
      * @see #checkAnnotationArtifact(Artifact)
      */
@@ -75,10 +75,11 @@ public class SCRDescriptorMojo extends A
     /**
      * The minimum SCR Annotation library version supported by this plugin. See
      * FELIX-2680 for full details.
-     *
+     * 
      * @see #checkAnnotationArtifact(Artifact)
      */
-    private static final ArtifactVersion SCR_ANN_MIN_VERSION = new DefaultArtifactVersion("1.6.0");
+    private static final ArtifactVersion SCR_ANN_MIN_VERSION = new DefaultArtifactVersion(
+            "1.6.0");
 
     /**
      * @parameter expression="${project.build.directory}/scr-plugin-generated"
@@ -89,7 +90,7 @@ public class SCRDescriptorMojo extends A
 
     /**
      * The Maven project.
-     *
+     * 
      * @parameter expression="${project}"
      * @required
      * @readonly
@@ -98,91 +99,81 @@ 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;
 
     /**
      * Name of the generated meta type file.
-     *
+     * 
      * @parameter default-value="metatype.xml"
      */
     private String metaTypeName;
 
     /**
      * 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.
-     *
+     * 
      * @parameter alias="excludes"
      */
     private String sourceExcludes;
 
     /**
      * Predefined properties.
-     *
+     * 
      * @parameter
      */
     private Map<String, String> properties = new HashMap<String, String>();
 
     /**
      * Allows to define additional implementations of the interface
-     * {@link org.apache.felix.scrplugin.AnnotationProcessor}
-     * that provide mappings from custom annotations to descriptions.
-     *
+     * {@link org.apache.felix.scrplugin.AnnotationProcessor} that provide
+     * mappings from custom annotations to descriptions.
+     * 
      * @parameter
      */
     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() );
+        final org.apache.felix.scrplugin.Log scrLog = new MavenLog(getLog());
         // create the class loader
-        final ClassLoader classLoader = new URLClassLoader( getClassPath(), this.getClass().getClassLoader() );
+        final ClassLoader classLoader = new URLClassLoader(getClassPath(), this
+                .getClass().getClassLoader());
 
         // create project
-        final MavenProjectScanner scanner = new MavenProjectScanner(this.project, this.sourceIncludes, this.sourceExcludes, scrLog);
+        final MavenProjectScanner scanner = new MavenProjectScanner(
+                this.project, this.sourceIncludes, this.sourceExcludes, scrLog);
 
         final Project project = new Project();
         project.setClassLoader(classLoader);
@@ -191,57 +182,61 @@ public class SCRDescriptorMojo extends A
 
         // create options
         final Options options = new Options();
-        options.setGenerateAccessors( generateAccessors );
-        options.setStrictMode( strictMode );
-        options.setProperties( properties );
-        options.setSpecVersion( SpecVersion.fromName(specVersion) );
+        options.setGenerateAccessors(generateAccessors);
+        options.setStrictMode(strictMode);
+        options.setProperties(properties);
+        options.setSpecVersion(SpecVersion.fromName(specVersion));
         options.setAnnotationProcessors(annotationProcessors);
         try {
 
-            final SCRDescriptorGenerator generator = new SCRDescriptorGenerator( scrLog );
+            final SCRDescriptorGenerator generator = new SCRDescriptorGenerator(
+                    scrLog);
 
             // setup from plugin configuration
-            generator.setOutputDirectory( outputDirectory );
+            generator.setOutputDirectory(outputDirectory);
             generator.setOptions(options);
             generator.setProject(project);
-            generator.setFinalName( finalName );
-            generator.setMetaTypeName( metaTypeName );
+            generator.setFinalName(finalName);
+            generator.setMetaTypeName(metaTypeName);
 
-            if ( generator.execute() ) {
+            if (generator.execute()) {
                 setServiceComponentHeader();
                 addResources();
             }
 
-        }
-        catch ( final SCRDescriptorException sde ) {
-            throw new MojoExecutionException( sde.getMessage(), sde.getCause() );
-        } catch ( SCRDescriptorFailureException sdfe ) {
-            throw ( MojoFailureException ) new MojoFailureException( sdfe.getMessage() ).initCause( sdfe );
+        } 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 ( final IOException ioe ) {
-            throw new MojoFailureException( "Unable to add target directory to classloader.");
+            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()]);
     }
 
     /**
@@ -251,61 +246,64 @@ public class SCRDescriptorMojo extends A
      * lower then the build fails because as of Maven SCR Plugin 1.6.0 the old
      * 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}
+     */
+    private void assertMinScrAnnotationArtifactVersion(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)
-            {
+            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" );
+    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 );
+            svcComp = (svcComp == null) ? svcPath
+                    : svcComp.contains(svcPath) ? svcComp : svcComp + ", "
+                            + svcPath;
+            project.getProperties().setProperty("Service-Component", svcComp);
         }
     }
 
-
-    private void addResources()
-    {
+    private void addResources() {
         // 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);
         }
     }
 }