Enhanced onos-build-selective to react to any src file modifications and to also result in a cascaded build of app modules which require the modified modules.

Change-Id: I1c41ca2d11c936bfeb7307ce0e01b20d0141daa6
diff --git a/apps/grouphandler/pom.xml b/apps/grouphandler/pom.xml
deleted file mode 100644
index 17aa23c..0000000
--- a/apps/grouphandler/pom.xml
+++ /dev/null
@@ -1,72 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  ~ Copyright 2014 Open Networking Laboratory
-  ~
-  ~ Licensed 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.
-  -->
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-    <modelVersion>4.0.0</modelVersion>
-
-    <parent>
-        <groupId>org.onosproject</groupId>
-        <artifactId>onos-apps</artifactId>
-        <version>1.2.0-SNAPSHOT</version>
-        <relativePath>../pom.xml</relativePath>
-    </parent>
-
-    <artifactId>onos-app-grouphandler</artifactId>
-    <packaging>bundle</packaging>
-
-    <description>ONOS sample application using group service</description>
-
-    <dependencies>
-        <dependency>
-            <groupId>org.onosproject</groupId>
-            <artifactId>onos-api</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.onosproject</groupId>
-            <artifactId>onlab-osgi</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.onosproject</groupId>
-            <artifactId>onlab-nio</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.onosproject</groupId>
-            <artifactId>onlab-netty</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.karaf.shell</groupId>
-            <artifactId>org.apache.karaf.shell.console</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.onosproject</groupId>
-            <artifactId>onlab-misc</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.google.guava</groupId>
-            <artifactId>guava</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.core</artifactId>
-        </dependency>
-    </dependencies>
-</project>
diff --git a/tools/dev/bin/onos-build-selective b/tools/dev/bin/onos-build-selective
index 732a31d..576db7a 100755
--- a/tools/dev/bin/onos-build-selective
+++ b/tools/dev/bin/onos-build-selective
@@ -3,11 +3,36 @@
 # Selectively builds only those projects that contained modified Java files.
 # ----------------------------------------------------------------------------
 
-# TODO: figure out a more elegant way of ignoring disconnected projects
-projects=$(find $ONOS_ROOT -name '*.java' \
-    -not -path '.git/*' -and -not -path '*/archetypes/*' -and -not -path '*/grouphandler/*' -and -not -path '*/maven-plugin/*' \
+projects=$(find $ONOS_ROOT -type f -path '*/src/*' \
     -exec $ONOS_ROOT/tools/dev/bin/onos-build-selective-hook {} \; | \
-    sort -u | sed "s:$ONOS_ROOT::g" | tr '\n' ',' | \
-    sed 's:/,:,:g;s:,/:,:g;s:^/::g;s:,$::g')
+        grep -v -f $ONOS_ROOT/tools/dev/bin/onos-build-selective.exclude | \
+        sort -u | sed "s:$ONOS_ROOT::g" | tr '\n' ',' | \
+        sed 's:/,:,:g;s:,/:,:g;s:^/::g;s:,$::g')
 
-[ -n "$projects" ] && cd $ONOS_ROOT && mvn --projects $projects ${@:-clean install}
+if [ -n "$projects" ]; then
+    # Ascertain artifact IDs of the projects to be rebuilt
+    modulesERE=""
+    for pd in ${projects//,/ }; do
+        artifactId=$(grep -E "^    <artifactId>.*</artifactId>$" ${pd}/pom.xml | \
+                        sed 's/.[^>]*>//;s/<.*//')
+        modulesERE="$modulesERE|$artifactId"
+    done
+    modulesERE=${modulesERE#|*}
+
+    # Search through staged app.xml files for any apps that require one or
+    # more of the modified artifacts.
+    appProjects=$(find $ONOS_ROOT -type f -path '*/target/oar/app.xml' | \
+            xargs grep '<artifact>' | grep -E "/($modulesERE)/" | \
+            cut -d: -f1 | sed 's:/target/oar/.*::g' | \
+            sort -u | sed "s:$ONOS_ROOT::g" | tr '\n' ',' | \
+            sed 's:/,:,:g;s:,/:,:g;s:^/::g;s:,$::g')
+
+    # If we found any, append those app projects to the list of projects to
+    # be built.
+    [ -n "$appProjects" ] && projects=$projects,$appProjects
+
+    echo Building projects $projects
+    cd $ONOS_ROOT && mvn --projects $projects ${@:-clean install}
+
+    [ -n "$appProjects" ] && echo "App staging required for projects $appProjects"
+fi
diff --git a/tools/dev/bin/onos-build-selective-hook b/tools/dev/bin/onos-build-selective-hook
index c27d747..fbe7752 100755
--- a/tools/dev/bin/onos-build-selective-hook
+++ b/tools/dev/bin/onos-build-selective-hook
@@ -1,16 +1,10 @@
 #!/bin/bash
 # ----------------------------------------------------------------------------
-# Echoes project-level directory if a Java file within is newer than the
+# Echoes project-level directory if a source file within is newer than the
 # target directory.
 # ----------------------------------------------------------------------------
 
-javaFile=${1#*\/src\/*\/java/}
-basename=${1/*\//}
+[ ${1/*\//} = "package-info.java" ] && exit 0
 
-[ $basename = "package-info.java" ] && exit 0
-
-src=${1/$javaFile/}
-project=${src/src*/}
-target=$project/target
-
-[ $target -nt ${src}$javaFile ] || echo ${src/src*/}
+project=${1/src*/}
+[ ${project}target -nt $1 ] || echo ${project}
diff --git a/tools/dev/bin/onos-build-selective.exclude b/tools/dev/bin/onos-build-selective.exclude
new file mode 100644
index 0000000..cd36a91
--- /dev/null
+++ b/tools/dev/bin/onos-build-selective.exclude
@@ -0,0 +1,3 @@
+.*/archetypes/.*
+.*/maven-plugin/.*
+.*/build/conf/.*
\ No newline at end of file