Updating buck plugin to track web resources

Also, renaming onosjar to onos.

Change-Id: Ia726772462b0bc997b92dbcb4bdc477ee7b590b0
diff --git a/tools/build/buck-plugin/BUCK b/tools/build/buck-plugin/BUCK
index 57959a4..49be7a9 100644
--- a/tools/build/buck-plugin/BUCK
+++ b/tools/build/buck-plugin/BUCK
@@ -31,7 +31,7 @@
 )
 
 java_binary(
-  name = 'onosjar',
+  name = 'onos',
   deps = [':lib'],
   visibility = ['PUBLIC'],
 )
\ No newline at end of file
diff --git a/tools/build/buck-plugin/buck-plugin-install b/tools/build/buck-plugin/buck-plugin-install
index 4d71039..9dc44d7 100755
--- a/tools/build/buck-plugin/buck-plugin-install
+++ b/tools/build/buck-plugin/buck-plugin-install
@@ -1,9 +1,11 @@
 #!/bin/bash
 
-PLUGINS=$ONOS_ROOT/bucklets/plugins
+PLUGINS=$ONOS_ROOT/bin/plugins
+
+set -e
 
 # Build it first
-pluginJar=$(NO_BUCKD=1 buck build //tools/build/buck-plugin:onosjar --no-cache --show-output | grep onosjar.jar | cut -d\  -f2)
+pluginJar=$(NO_BUCKD=1 buck build //tools/build/buck-plugin:onos --no-cache --show-output | grep onos.jar | cut -d\  -f2)
 
 # Then install it
 mkdir -p $PLUGINS
diff --git a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJar.java b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJar.java
index dde1902..6885c07 100644
--- a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJar.java
+++ b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJar.java
@@ -17,20 +17,21 @@
 
 import com.facebook.buck.jvm.java.CompileToJarStepFactory;
 import com.facebook.buck.jvm.java.DefaultJavaLibrary;
-import com.facebook.buck.jvm.java.HasClasspathEntries;
 import com.facebook.buck.jvm.java.HasMavenCoordinates;
 import com.facebook.buck.jvm.java.JavaLibrary;
 import com.facebook.buck.jvm.java.MavenPublishable;
 import com.facebook.buck.model.BuildTarget;
+import com.facebook.buck.model.Pair;
 import com.facebook.buck.rules.AddToRuleKey;
 import com.facebook.buck.rules.BuildRule;
 import com.facebook.buck.rules.BuildRuleParams;
 import com.facebook.buck.rules.SourcePath;
 import com.facebook.buck.rules.SourcePathResolver;
 import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSortedMap;
 import com.google.common.collect.ImmutableSortedSet;
 
 import java.nio.file.Path;
@@ -59,6 +60,9 @@
     @AddToRuleKey
     final Optional<String> apiDescription;
 
+    @AddToRuleKey
+    final Optional<ImmutableSortedMap<String, SourcePath>> includedResources;
+
     private final ImmutableSortedSet<HasMavenCoordinates> mavenDeps;
 
     public OnosJar(BuildRuleParams params,
@@ -82,7 +86,8 @@
                    Optional<String> apiTitle,
                    Optional<String> apiVersion,
                    Optional<String> apiPackage,
-                   Optional<String> apiDescription) {
+                   Optional<String> apiDescription,
+                   Optional<ImmutableSortedMap<String, SourcePath>> includedResources) {
         super(params, resolver, srcs, resources, generatedSourceFolder,
               proguardConfig, postprocessClassesCommands, exportedDeps,
               providedDeps, abiJar, trackClassUsage, additionalClasspathEntries,
@@ -93,6 +98,7 @@
         this.apiVersion = apiVersion;
         this.apiPackage = apiPackage;
         this.apiDescription = apiDescription;
+        this.includedResources = includedResources;
         this.mavenDeps = computeMavenDeps();
     }
 
diff --git a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJarDescription.java b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJarDescription.java
index 6d1ee7e..6ba61cf 100644
--- a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJarDescription.java
+++ b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJarDescription.java
@@ -34,6 +34,7 @@
 import com.facebook.buck.model.Flavor;
 import com.facebook.buck.model.Flavored;
 import com.facebook.buck.model.ImmutableFlavor;
+import com.facebook.buck.model.Pair;
 import com.facebook.buck.parser.NoSuchBuildTargetException;
 import com.facebook.buck.rules.BuildRule;
 import com.facebook.buck.rules.BuildRuleParams;
@@ -53,8 +54,12 @@
 import com.google.common.collect.ImmutableSortedMap;
 import com.google.common.collect.ImmutableSortedSet;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 
 import java.nio.file.Path;
+import java.util.List;
+import java.util.function.BinaryOperator;
+import java.util.stream.Collectors;
 
 import static com.facebook.buck.jvm.common.ResourceValidator.validateResources;
 
@@ -198,6 +203,19 @@
 
         ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps.get());
 
+        // Build the resources string
+        List<String> resourceMappings = Lists.newArrayList();
+
+        if (args.includeResources.isPresent()) {
+            args.includeResources.get().entrySet().forEach(p ->
+                resourceMappings.add(String.format("%s=%s", p.getKey(), p.getValue())));
+        }
+
+        if (args.apiTitle.isPresent()) {
+            resourceMappings.add("WEB-INF/classes/apidoc/swagger.json=swagger.json");
+        }
+
+        Optional<String> includedResourcesString = Optional.of(String.join(",", resourceMappings));
         final DefaultJavaLibrary defaultJavaLibrary;
         if (!flavors.contains(NON_OSGI_JAR)) {
             defaultJavaLibrary =
@@ -232,7 +250,7 @@
                                                            args.apiPackage, args.apiDescription, args.resources,
                                                            args.groupId, args.bundleName, args.bundleVersion,
                                                            args.bundleLicense, args.bundleDescription, args.importPackages,
-                                                           args.exportPackages, args.includeResources, args.dynamicimportPackages),
+                                                           args.exportPackages, includedResourcesString, args.dynamicimportPackages),
                                     args.resourcesRoot,
                                     args.manifestFile,
                                     args.mavenCoords,
@@ -242,7 +260,8 @@
                                     args.apiTitle,
                                     args.apiVersion,
                                     args.apiPackage,
-                                    args.apiDescription));
+                                    args.apiDescription,
+                                    args.includeResources));
         } else {
             defaultJavaLibrary =
                     resolver.addToIndex(
@@ -309,7 +328,7 @@
 
         public Optional<String> importPackages;
         public Optional<String> exportPackages;
-        public Optional<String> includeResources;
+        public Optional<ImmutableSortedMap<String, SourcePath>> includeResources;
         public Optional<String> dynamicimportPackages;
     }
 }
\ No newline at end of file