blob: 6885c075a499a1c70dbe520955ab6e9c24761839 [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.HasMavenCoordinates;
21import com.facebook.buck.jvm.java.JavaLibrary;
22import com.facebook.buck.jvm.java.MavenPublishable;
Brian O'Connore8468b52016-07-25 13:42:36 -070023import com.facebook.buck.model.BuildTarget;
Brian O'Connor79b70672016-10-20 13:44:52 -070024import com.facebook.buck.model.Pair;
Brian O'Connore8468b52016-07-25 13:42:36 -070025import 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;
31import com.google.common.collect.ImmutableList;
Brian O'Connor79b70672016-10-20 13:44:52 -070032import com.google.common.collect.ImmutableMap;
Brian O'Connore8468b52016-07-25 13:42:36 -070033import com.google.common.collect.ImmutableSet;
Brian O'Connor79b70672016-10-20 13:44:52 -070034import com.google.common.collect.ImmutableSortedMap;
Brian O'Connore8468b52016-07-25 13:42:36 -070035import com.google.common.collect.ImmutableSortedSet;
36
37import java.nio.file.Path;
38import java.util.Set;
39import java.util.regex.Pattern;
40
41/**
42 * Implementation of a build rule that generates a onosjar.json file for a set
43 * of Java sources.
44 */
Brian O'Connoree674952016-09-13 16:31:45 -070045public class OnosJar extends DefaultJavaLibrary
46 implements MavenPublishable{
Brian O'Connore8468b52016-07-25 13:42:36 -070047
48 @AddToRuleKey
49 final Optional<String> webContext;
50
51 @AddToRuleKey
52 final Optional<String> apiTitle;
53
54 @AddToRuleKey
55 final Optional<String> apiVersion;
56
57 @AddToRuleKey
58 final Optional<String> apiPackage;
59
60 @AddToRuleKey
61 final Optional<String> apiDescription;
62
Brian O'Connor79b70672016-10-20 13:44:52 -070063 @AddToRuleKey
64 final Optional<ImmutableSortedMap<String, SourcePath>> includedResources;
65
Brian O'Connoree674952016-09-13 16:31:45 -070066 private final ImmutableSortedSet<HasMavenCoordinates> mavenDeps;
67
Brian O'Connore8468b52016-07-25 13:42:36 -070068 public OnosJar(BuildRuleParams params,
69 SourcePathResolver resolver,
70 Set<? extends SourcePath> srcs,
71 Set<? extends SourcePath> resources,
72 Optional<Path> generatedSourceFolder,
73 Optional<SourcePath> proguardConfig,
74 ImmutableList<String> postprocessClassesCommands,
75 ImmutableSortedSet<BuildRule> exportedDeps,
76 ImmutableSortedSet<BuildRule> providedDeps,
77 SourcePath abiJar, boolean trackClassUsage,
78 ImmutableSet<Path> additionalClasspathEntries,
79 CompileToJarStepFactory compileStepFactory,
80 Optional<Path> resourcesRoot,
Brian O'Connor69e37d92016-10-12 15:05:09 -070081 Optional<SourcePath> manifestFile,
Brian O'Connore8468b52016-07-25 13:42:36 -070082 Optional<String> mavenCoords,
83 ImmutableSortedSet<BuildTarget> tests,
84 ImmutableSet<Pattern> classesToRemoveFromJar,
85 Optional<String> webContext,
86 Optional<String> apiTitle,
87 Optional<String> apiVersion,
88 Optional<String> apiPackage,
Brian O'Connor79b70672016-10-20 13:44:52 -070089 Optional<String> apiDescription,
90 Optional<ImmutableSortedMap<String, SourcePath>> includedResources) {
Brian O'Connore8468b52016-07-25 13:42:36 -070091 super(params, resolver, srcs, resources, generatedSourceFolder,
92 proguardConfig, postprocessClassesCommands, exportedDeps,
93 providedDeps, abiJar, trackClassUsage, additionalClasspathEntries,
Brian O'Connor69e37d92016-10-12 15:05:09 -070094 compileStepFactory, resourcesRoot, manifestFile, mavenCoords,
95 tests, classesToRemoveFromJar);
Brian O'Connore8468b52016-07-25 13:42:36 -070096 this.webContext = webContext;
97 this.apiTitle = apiTitle;
98 this.apiVersion = apiVersion;
99 this.apiPackage = apiPackage;
100 this.apiDescription = apiDescription;
Brian O'Connor79b70672016-10-20 13:44:52 -0700101 this.includedResources = includedResources;
Brian O'Connoree674952016-09-13 16:31:45 -0700102 this.mavenDeps = computeMavenDeps();
103 }
104
105 private ImmutableSortedSet<HasMavenCoordinates> computeMavenDeps() {
106 ImmutableSortedSet.Builder<HasMavenCoordinates> mavenDeps = ImmutableSortedSet.naturalOrder();
107 for (JavaLibrary javaLibrary : getTransitiveClasspathDeps()) {
108 if (this.equals(javaLibrary)) {
109 // no need to include ourself
110 continue;
111 } else if (HasMavenCoordinates.MAVEN_COORDS_PRESENT_PREDICATE.apply(javaLibrary)) {
112 mavenDeps.add(javaLibrary);
113 //FIXME BOC do we always want to exclude all of a maven jar's dependencies? probably.
114 mavenDeps.addAll(javaLibrary.getTransitiveClasspathDeps());
115 }
116 }
117 return mavenDeps.build();
118 }
119
120 @Override
121 public Iterable<HasMavenCoordinates> getMavenDeps() {
122 return mavenDeps;
123 }
124
125 @Override
126 public Iterable<BuildRule> getPackagedDependencies() {
127 //FIXME this is not supported at the moment
128 return ImmutableList.of();
Brian O'Connore8468b52016-07-25 13:42:36 -0700129 }
Brian O'Connor69e37d92016-10-12 15:05:09 -0700130
131 @Override
132 public Optional<Path> getPomTemplate() {
133 //FIXME we should consider supporting this
134 return Optional.absent();
135 }
Brian O'Connore8468b52016-07-25 13:42:36 -0700136}