blob: 3c71968306ddaa881a22518f13413e2fa46a0e63 [file] [log] [blame]
Brian O'Connore8468b52016-07-25 13:42:36 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.onosjar;
17
18import com.facebook.buck.jvm.java.CompileToJarStepFactory;
19import com.facebook.buck.jvm.java.DefaultJavaLibrary;
Brian O'Connoree674952016-09-13 16:31:45 -070020import com.facebook.buck.jvm.java.HasClasspathEntries;
21import com.facebook.buck.jvm.java.HasMavenCoordinates;
22import com.facebook.buck.jvm.java.JavaLibrary;
23import com.facebook.buck.jvm.java.MavenPublishable;
Brian O'Connore8468b52016-07-25 13:42:36 -070024import com.facebook.buck.model.BuildTarget;
25import com.facebook.buck.rules.AddToRuleKey;
26import com.facebook.buck.rules.BuildRule;
27import com.facebook.buck.rules.BuildRuleParams;
28import com.facebook.buck.rules.SourcePath;
29import com.facebook.buck.rules.SourcePathResolver;
30import com.google.common.base.Optional;
Brian O'Connoree674952016-09-13 16:31:45 -070031import com.google.common.base.Preconditions;
Brian O'Connore8468b52016-07-25 13:42:36 -070032import com.google.common.collect.ImmutableList;
33import com.google.common.collect.ImmutableSet;
34import com.google.common.collect.ImmutableSortedSet;
35
36import java.nio.file.Path;
37import java.util.Set;
38import java.util.regex.Pattern;
39
40/**
41 * Implementation of a build rule that generates a onosjar.json file for a set
42 * of Java sources.
43 */
Brian O'Connoree674952016-09-13 16:31:45 -070044public class OnosJar extends DefaultJavaLibrary
45 implements MavenPublishable{
Brian O'Connore8468b52016-07-25 13:42:36 -070046
47 @AddToRuleKey
48 final Optional<String> webContext;
49
50 @AddToRuleKey
51 final Optional<String> apiTitle;
52
53 @AddToRuleKey
54 final Optional<String> apiVersion;
55
56 @AddToRuleKey
57 final Optional<String> apiPackage;
58
59 @AddToRuleKey
60 final Optional<String> apiDescription;
61
Brian O'Connoree674952016-09-13 16:31:45 -070062 private final ImmutableSortedSet<HasMavenCoordinates> mavenDeps;
63
Brian O'Connore8468b52016-07-25 13:42:36 -070064 public OnosJar(BuildRuleParams params,
65 SourcePathResolver resolver,
66 Set<? extends SourcePath> srcs,
67 Set<? extends SourcePath> resources,
68 Optional<Path> generatedSourceFolder,
69 Optional<SourcePath> proguardConfig,
70 ImmutableList<String> postprocessClassesCommands,
71 ImmutableSortedSet<BuildRule> exportedDeps,
72 ImmutableSortedSet<BuildRule> providedDeps,
73 SourcePath abiJar, boolean trackClassUsage,
74 ImmutableSet<Path> additionalClasspathEntries,
75 CompileToJarStepFactory compileStepFactory,
76 Optional<Path> resourcesRoot,
77 Optional<String> mavenCoords,
78 ImmutableSortedSet<BuildTarget> tests,
79 ImmutableSet<Pattern> classesToRemoveFromJar,
80 Optional<String> webContext,
81 Optional<String> apiTitle,
82 Optional<String> apiVersion,
83 Optional<String> apiPackage,
84 Optional<String> apiDescription) {
85 super(params, resolver, srcs, resources, generatedSourceFolder,
86 proguardConfig, postprocessClassesCommands, exportedDeps,
87 providedDeps, abiJar, trackClassUsage, additionalClasspathEntries,
88 compileStepFactory, resourcesRoot, mavenCoords, tests,
89 classesToRemoveFromJar);
90 this.webContext = webContext;
91 this.apiTitle = apiTitle;
92 this.apiVersion = apiVersion;
93 this.apiPackage = apiPackage;
94 this.apiDescription = apiDescription;
Brian O'Connoree674952016-09-13 16:31:45 -070095 this.mavenDeps = computeMavenDeps();
96 }
97
98 private ImmutableSortedSet<HasMavenCoordinates> computeMavenDeps() {
99 ImmutableSortedSet.Builder<HasMavenCoordinates> mavenDeps = ImmutableSortedSet.naturalOrder();
100 for (JavaLibrary javaLibrary : getTransitiveClasspathDeps()) {
101 if (this.equals(javaLibrary)) {
102 // no need to include ourself
103 continue;
104 } else if (HasMavenCoordinates.MAVEN_COORDS_PRESENT_PREDICATE.apply(javaLibrary)) {
105 mavenDeps.add(javaLibrary);
106 //FIXME BOC do we always want to exclude all of a maven jar's dependencies? probably.
107 mavenDeps.addAll(javaLibrary.getTransitiveClasspathDeps());
108 }
109 }
110 return mavenDeps.build();
111 }
112
113 @Override
114 public Iterable<HasMavenCoordinates> getMavenDeps() {
115 return mavenDeps;
116 }
117
118 @Override
119 public Iterable<BuildRule> getPackagedDependencies() {
120 //FIXME this is not supported at the moment
121 return ImmutableList.of();
Brian O'Connore8468b52016-07-25 13:42:36 -0700122 }
123}