Author: alien11689
Date: Wed Dec 28 08:01:27 2016
New Revision: 1776239

URL: http://svn.apache.org/viewvc?rev=1776239&view=rev
Log:
[ARIES-1644] Find packages to scan in BMP when scanPaths is missing

Added:
    
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/PackageFinder.java
Modified:
    
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java

Modified: 
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java?rev=1776239&r1=1776238&r2=1776239&view=diff
==============================================================================
--- 
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java
 (original)
+++ 
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java
 Wed Dec 28 08:01:27 2016
@@ -41,6 +41,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -48,14 +49,14 @@ import java.util.Set;
 /**
  * Generates blueprint from CDI annotations
  */
-@Mojo(name="blueprint-generate", 
requiresDependencyResolution=ResolutionScope.COMPILE,
-    defaultPhase=LifecyclePhase.PROCESS_CLASSES, inheritByDefault=false)
+@Mojo(name = "blueprint-generate", requiresDependencyResolution = 
ResolutionScope.COMPILE,
+    defaultPhase = LifecyclePhase.PROCESS_CLASSES, inheritByDefault = false)
 public class GenerateMojo extends AbstractMojo {
 
-    @Parameter(defaultValue="${project}", required=true)
+    @Parameter(defaultValue = "${project}", required = true)
     protected MavenProject project;
 
-    @Parameter(required=true)
+    @Parameter
     protected List<String> scanPaths;
 
     /**
@@ -70,14 +71,14 @@ public class GenerateMojo extends Abstra
     /**
      * Name of file to generate
      */
-    @Parameter(defaultValue="autowire.xml")
+    @Parameter(defaultValue = "autowire.xml")
     protected String generatedFileName;
 
     /**
      * Base directory to generate into
      * (relative to ${project.build.directory}/generated-sources/blueprint).
      */
-    @Parameter(defaultValue="OSGI-INF/blueprint/")
+    @Parameter(defaultValue = "OSGI-INF/blueprint/")
     private String generatedDir;
 
     /**
@@ -97,8 +98,19 @@ public class GenerateMojo extends Abstra
 
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
-        if (scanPaths.size() == 0 || scanPaths.iterator().next() == null) {
-            throw new MojoExecutionException("Configuration scanPaths must be 
set");
+        List<String> toScan = scanPaths;
+        if (scanPaths == null || scanPaths.size() == 0 || 
scanPaths.iterator().next() == null) {
+            getLog().info("Scan paths not specified - searching for packages");
+            Set<String> packages = 
PackageFinder.findPackagesInSources(project.getCompileSourceRoots());
+            if (packages.contains(null)) {
+                throw new MojoExecutionException("Found file without package");
+            }
+            toScan = new ArrayList<>(packages);
+            Collections.sort(toScan);
+
+        }
+        for (String aPackage : toScan) {
+            getLog().info("Package " + aPackage + " will be scan");
         }
         if (!buildContext.hasDelta(new 
File(project.getCompileSourceRoots().iterator().next()))) {
             return;
@@ -107,7 +119,7 @@ public class GenerateMojo extends Abstra
         try {
             ClassFinder finder = createProjectScopeFinder();
 
-            Set<Class<?>> classes = FilteredClassFinder.findClasses(finder, 
scanPaths);
+            Set<Class<?>> classes = FilteredClassFinder.findClasses(finder, 
toScan);
 
             BlueprintConfiguration blueprintConfiguration = new 
BlueprintConfigurationImpl(namespaces, defaultActivation, customParameters);
 

Added: 
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/PackageFinder.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/PackageFinder.java?rev=1776239&view=auto
==============================================================================
--- 
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/PackageFinder.java
 (added)
+++ 
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/PackageFinder.java
 Wed Dec 28 08:01:27 2016
@@ -0,0 +1,74 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.aries.blueprint.plugin;
+
+import java.io.File;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.Stack;
+
+class PackageFinder {
+    static Set<String> findPackagesInSources(List<String> compileSourceRoots) {
+        Set<String> packages = new HashSet<>();
+        for (String src : compileSourceRoots) {
+            File root = new File(src);
+            if (root.exists()) {
+                packages.addAll(findPackageRoots(root));
+            }
+        }
+        return packages;
+    }
+
+    private static Set<String> findPackageRoots(File file) {
+        Set<String> packages = new HashSet<>();
+        Stack<SearchFile> stack = new Stack<>();
+        stack.add(new SearchFile(null, file));
+        while (!stack.isEmpty()) {
+            SearchFile cur = stack.pop();
+            File[] files = cur.f.listFiles();
+            boolean foundFile = false;
+            for (File child : files) {
+                if (child.isFile()) {
+                    packages.add(cur.prefix);
+                    foundFile = true;
+                }
+            }
+            if (foundFile) {
+                continue;
+            }
+            for (File child : files) {
+                if (child.isDirectory()) {
+                    stack.add(new SearchFile(cur.prefix != null ? cur.prefix + 
"." + child.getName() : child.getName(), child));
+                }
+            }
+        }
+        return packages;
+    }
+
+    static class SearchFile {
+        String prefix;
+        File f;
+
+        public SearchFile(String prefix, File f) {
+            this.prefix = prefix;
+            this.f = f;
+        }
+    }
+}


Reply via email to