Brian O'Connor
Committed by Gerrit Code Review

Enhancing OnosJar to build OSGi jars and wars if required.

Also, simplifying onos.bucklet by using the rule

Change-Id: If89633db2d83cbfc56a8e70d2bea665ffaf186ff
......@@ -114,6 +114,7 @@ def osgi_jar(
api_description = NONE,
resources = NONE,
resources_root = None,
tests = None,
**kwargs
):
......@@ -134,76 +135,42 @@ def osgi_jar(
resources_root = RESOURCES_ROOT
if api_title != NONE:
r = 'WEB-INF/classes/apidoc/swagger.json=bin/swagger.json'
r = 'WEB-INF/classes/apidoc/swagger.json=swagger.json'
include_resources = include_resources + ',' + r if include_resources != NONE else r
bare_jar_name = name + '-jar'
osgi_jar_name = name + '-osgi'
mvn_coords = group_id + ':' + name + ':' + version
onos_jar(
name = bare_jar_name,
srcs = srcs,
name = name,
srcs = srcs + glob(['src/main/webapp/**']),
deps = deps,
visibility = [], #intentially, not visible
visibility = visibility,
resources = resources,
resources_root = resources_root,
bundle_name = name,
group_id = group_id,
bundle_version = version,
bundle_license = license,
bundle_description = description,
import_packages = import_packages,
export_packages = export_packages,
include_resources = include_resources,
dynamicimport_packages = dynamicimport_packages,
web_context = web_context,
api_title = api_title,
api_version = api_version,
api_package = api_package,
api_description = api_description,
tests = tests,
maven_coords = mvn_coords,
**kwargs
)
cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""'
args = ( '$(location :%s)' % bare_jar_name, #input jar
'$OUT', #output jar
cp, #classpath
name, #bundle name
group_id, #group id
version, #version
license, #license url
"'%s'" % import_packages, #packages to import
"'%s'" % export_packages, #packages to export
include_resources, #custom includes to classpath
web_context, #web context (REST API only)
"'%s'" % dynamicimport_packages, #DynamicImport-Package
description, #description
)
#TODO stage_jar is a horrendous hack
stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
bash = stage_jar + wrap_jar
if debug:
bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
print bash
# FIXME: make sure that /swagger.json gets filtered
genrule(
name = osgi_jar_name,
bash = bash,
out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
srcs = glob(['src/main/webapp/**']),
visibility = [], #intentially, not visible
)
# TODO we really should shade the jar with maven flavor
prebuilt_jar(
name = name,
maven_coords = mvn_coords,
binary_jar = ':' + osgi_jar_name,
visibility = visibility,
)
### Checkstyle
checkstyle(
name = name + '-checkstyle-files',
srcs = srcs,
jar_target = ':'+ bare_jar_name,
jar_target = ':'+ name,
)
java_doc(
......@@ -219,7 +186,7 @@ def osgi_jar(
# TODO add project config for intellij
# project_config(
# src_target = ':' + bare_jar_name,
# src_target = ':' + name,
# src_roots = [ 'src/main/java' ],
# test_target = ':' + name + '-tests',
# test_roots = [ 'src/test/java' ],
......@@ -247,6 +214,8 @@ def osgi_jar(
def osgi_jar_with_tests(
name = None,
deps = [],
group_id = ONOS_GROUP_ID,
version = ONOS_VERSION,
test_srcs = None,
test_deps = [ '//lib:TEST' ],
test_resources = None,
......@@ -260,7 +229,10 @@ def osgi_jar_with_tests(
osgi_jar(name = name,
deps = deps,
group_id = group_id,
version = version,
visibility = visibility,
tests = [':' + name + '-tests'],
**kwargs)
if test_resources and not test_resources_root:
......@@ -275,16 +247,18 @@ def osgi_jar_with_tests(
if test_srcs is None:
test_srcs = glob([TEST + '/*.java'])
mvn_coords = group_id + ':' + name + ':jar:tests:' + version
java_test(
name = name + '-tests',
srcs = test_srcs,
deps = deps +
test_deps +
[':' + name + '-jar'],
source_under_test = [':' + name + '-jar'],
[':' + name + '#non-osgi'],
resources = test_resources,
resources_root = test_resources_root,
visibility = visibility
maven_coords = mvn_coords,
visibility = visibility,
)
checkstyle(
......@@ -292,5 +266,3 @@ def osgi_jar_with_tests(
srcs = test_srcs,
jar_target = ':' + name + '-tests',
)
#FIXME need to run checkstyle on test sources
......
# Comment the remote_jar out for local buck development
remote_jar (
name = 'buck-api',
out = 'buck.jar',
url = 'https://github.com/bocon13/buck/releases/download/v2016.07.29.01-wip/buck.jar',
sha1 = 'f89324cb869b74fdcd4db9972233065a93d890a2',
url = 'https://github.com/bocon13/buck/releases/download/v2016.09.13.01/buck.jar',
sha1 = 'd5415b4326e5316675532a8cd48001a8456e8c9e',
visibility = [],
)
# Uncomment the prebuilt_jar and copy buck-api to lib/buck.jar for local buck development
# prebuilt_jar (
# name = 'buck-api',
# binary_jar = 'lib/buck.jar'
# )
COMPILE = [
'//lib:qdox',
#'//lib:jackson-core',
#'//lib:jackson-databind',
#'//lib:jackson-annotations',
]
RUNTIME = [
'//lib:guava',
'//lib:bndlib',
'//lib:org.apache.felix.scr.bnd'
]
java_library(
......
......@@ -17,6 +17,10 @@ package org.onosproject.onosjar;
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.rules.AddToRuleKey;
import com.facebook.buck.rules.BuildRule;
......@@ -24,6 +28,7 @@ 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.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
......@@ -36,7 +41,8 @@ import java.util.regex.Pattern;
* Implementation of a build rule that generates a onosjar.json file for a set
* of Java sources.
*/
public class OnosJar extends DefaultJavaLibrary {
public class OnosJar extends DefaultJavaLibrary
implements MavenPublishable{
@AddToRuleKey
final Optional<String> webContext;
......@@ -53,6 +59,8 @@ public class OnosJar extends DefaultJavaLibrary {
@AddToRuleKey
final Optional<String> apiDescription;
private final ImmutableSortedSet<HasMavenCoordinates> mavenDeps;
public OnosJar(BuildRuleParams params,
SourcePathResolver resolver,
Set<? extends SourcePath> srcs,
......@@ -84,5 +92,32 @@ public class OnosJar extends DefaultJavaLibrary {
this.apiVersion = apiVersion;
this.apiPackage = apiPackage;
this.apiDescription = apiDescription;
this.mavenDeps = computeMavenDeps();
}
private ImmutableSortedSet<HasMavenCoordinates> computeMavenDeps() {
ImmutableSortedSet.Builder<HasMavenCoordinates> mavenDeps = ImmutableSortedSet.naturalOrder();
for (JavaLibrary javaLibrary : getTransitiveClasspathDeps()) {
if (this.equals(javaLibrary)) {
// no need to include ourself
continue;
} else if (HasMavenCoordinates.MAVEN_COORDS_PRESENT_PREDICATE.apply(javaLibrary)) {
mavenDeps.add(javaLibrary);
//FIXME BOC do we always want to exclude all of a maven jar's dependencies? probably.
mavenDeps.addAll(javaLibrary.getTransitiveClasspathDeps());
}
}
return mavenDeps.build();
}
@Override
public Iterable<HasMavenCoordinates> getMavenDeps() {
return mavenDeps;
}
@Override
public Iterable<BuildRule> getPackagedDependencies() {
//FIXME this is not supported at the moment
return ImmutableList.of();
}
}
......
......@@ -28,12 +28,14 @@ import com.facebook.buck.rules.BuildableContext;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.step.Step;
import com.facebook.buck.step.fs.CopyStep;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
......@@ -49,6 +51,15 @@ public class OnosJarStepFactory extends JavacToJarStepFactory {
private final String apiPackage;
private final String apiDescription;
private final Optional<ImmutableSortedSet<SourcePath>> resources;
private final String groupId;
private final String bundleName;
private final String bundleVersion;
private final String bundleLicense;
private final String bundleDescription;
private final String importPackages;
private final String exportPackages;
private final String includeResources;
private final String dynamicimportPackages;
public OnosJarStepFactory(JavacOptions javacOptions,
JavacOptionsAmender amender,
......@@ -57,8 +68,26 @@ public class OnosJarStepFactory extends JavacToJarStepFactory {
Optional<String> apiVersion,
Optional<String> apiPackage,
Optional<String> apiDescription,
Optional<ImmutableSortedSet<SourcePath>> resources) {
Optional<ImmutableSortedSet<SourcePath>> resources,
Optional<String> groupId,
Optional<String> bundleName,
Optional<String> bundleVersion,
Optional<String> bundleLicense,
Optional<String> bundleDescription,
Optional<String> importPackages,
Optional<String> exportPackages,
Optional<String> includeResources,
Optional<String> dynamicimportPackages) {
super(javacOptions, amender);
this.bundleDescription = processParameter(bundleDescription);
this.importPackages = processParameter(importPackages);
this.exportPackages = processParameter(exportPackages);
this.includeResources = processParameter(includeResources);
this.dynamicimportPackages = processParameter(dynamicimportPackages);
this.groupId = processParameter(groupId);
this.bundleName = processParameter(bundleName);
this.bundleVersion = processParameter(bundleVersion);
this.bundleLicense = processParameter(bundleLicense);
this.webContext = processParameter(webContext);
this.apiTitle = processParameter(apiTitle);
this.apiVersion = processParameter(apiVersion);
......@@ -114,6 +143,11 @@ public class OnosJarStepFactory extends JavacToJarStepFactory {
apiPackage, apiDescription);
sourceFilePathBuilder.add(swaggerStep.apiRegistratorPath());
steps.add(swaggerStep);
// steps.addAll(sourceFilePaths.stream()
// .filter(sp -> sp.startsWith("src/main/webapp/"))
// .map(sp -> CopyStep.forFile(filesystem, sp, outputDirectory))
// .iterator());
}
createCompileStep(context,
......@@ -132,6 +166,7 @@ public class OnosJarStepFactory extends JavacToJarStepFactory {
// post compilation steps
// FIXME BOC: add mechanism to inject new Steps
//context.additionalStepFactory(JavaStep.class);
......@@ -143,6 +178,28 @@ public class OnosJarStepFactory extends JavacToJarStepFactory {
manifestFile.orNull(),
true,
blacklistBuilder.build()));
OSGiWrapper osgiStep = new OSGiWrapper(
outputJar, //input jar
outputJar, //Paths.get(outputJar.toString() + ".jar"), //output jar
invokingRule.getBasePath(), // sources dir
outputDirectory, // classes dir
declaredClasspathEntries, // classpath
bundleName, // bundle name
groupId, // groupId
bundleVersion, // bundle version
bundleLicense, // bundle license
importPackages, // import packages
exportPackages, // export packages
includeResources, // include resources
webContext, // web context
dynamicimportPackages, // dynamic import packages
bundleDescription // bundle description
);
steps.add(osgiStep);
//steps.add(CopyStep.forFile(filesystem, Paths.get(outputJar.toString() + ".jar"), outputJar));
}
private ImmutableSortedSet<Path> findSwaggerModelDefs(SourcePathResolver resolver,
......
......@@ -102,10 +102,12 @@ public class SwaggerGenerator {
}
if (srcs != null) {
srcs.forEach(src -> {
try {
builder.addSource(src);
} catch (IOException e) {
throw new RuntimeException(e);
if (src.toString().endsWith(".java")) {
try {
builder.addSource(src);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
......