blob: 0f80bd5b9e09ddf03c069305cd958eb50bec4a5b [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;
24import com.facebook.buck.rules.AddToRuleKey;
25import com.facebook.buck.rules.BuildRule;
26import com.facebook.buck.rules.BuildRuleParams;
27import com.facebook.buck.rules.SourcePath;
28import com.facebook.buck.rules.SourcePathResolver;
29import com.google.common.base.Optional;
30import com.google.common.collect.ImmutableList;
31import com.google.common.collect.ImmutableSet;
Brian O'Connor79b70672016-10-20 13:44:52 -070032import com.google.common.collect.ImmutableSortedMap;
Brian O'Connore8468b52016-07-25 13:42:36 -070033import com.google.common.collect.ImmutableSortedSet;
34
35import java.nio.file.Path;
36import java.util.Set;
37import java.util.regex.Pattern;
38
39/**
40 * Implementation of a build rule that generates a onosjar.json file for a set
41 * of Java sources.
42 */
Brian O'Connoree674952016-09-13 16:31:45 -070043public class OnosJar extends DefaultJavaLibrary
44 implements MavenPublishable{
Brian O'Connore8468b52016-07-25 13:42:36 -070045
46 @AddToRuleKey
47 final Optional<String> webContext;
48
49 @AddToRuleKey
50 final Optional<String> apiTitle;
51
52 @AddToRuleKey
53 final Optional<String> apiVersion;
54
55 @AddToRuleKey
56 final Optional<String> apiPackage;
57
58 @AddToRuleKey
59 final Optional<String> apiDescription;
60
Brian O'Connor79b70672016-10-20 13:44:52 -070061 @AddToRuleKey
62 final Optional<ImmutableSortedMap<String, SourcePath>> includedResources;
63
Brian O'Connoree674952016-09-13 16:31:45 -070064 private final ImmutableSortedSet<HasMavenCoordinates> mavenDeps;
65
Brian O'Connore8468b52016-07-25 13:42:36 -070066 public OnosJar(BuildRuleParams params,
67 SourcePathResolver resolver,
68 Set<? extends SourcePath> srcs,
69 Set<? extends SourcePath> resources,
70 Optional<Path> generatedSourceFolder,
71 Optional<SourcePath> proguardConfig,
72 ImmutableList<String> postprocessClassesCommands,
73 ImmutableSortedSet<BuildRule> exportedDeps,
74 ImmutableSortedSet<BuildRule> providedDeps,
75 SourcePath abiJar, boolean trackClassUsage,
76 ImmutableSet<Path> additionalClasspathEntries,
77 CompileToJarStepFactory compileStepFactory,
78 Optional<Path> resourcesRoot,
Brian O'Connor69e37d92016-10-12 15:05:09 -070079 Optional<SourcePath> manifestFile,
Brian O'Connore8468b52016-07-25 13:42:36 -070080 Optional<String> mavenCoords,
81 ImmutableSortedSet<BuildTarget> tests,
82 ImmutableSet<Pattern> classesToRemoveFromJar,
83 Optional<String> webContext,
84 Optional<String> apiTitle,
85 Optional<String> apiVersion,
86 Optional<String> apiPackage,
Brian O'Connor79b70672016-10-20 13:44:52 -070087 Optional<String> apiDescription,
88 Optional<ImmutableSortedMap<String, SourcePath>> includedResources) {
Brian O'Connore8468b52016-07-25 13:42:36 -070089 super(params, resolver, srcs, resources, generatedSourceFolder,
90 proguardConfig, postprocessClassesCommands, exportedDeps,
91 providedDeps, abiJar, trackClassUsage, additionalClasspathEntries,
Brian O'Connor69e37d92016-10-12 15:05:09 -070092 compileStepFactory, resourcesRoot, manifestFile, mavenCoords,
93 tests, classesToRemoveFromJar);
Brian O'Connore8468b52016-07-25 13:42:36 -070094 this.webContext = webContext;
95 this.apiTitle = apiTitle;
96 this.apiVersion = apiVersion;
97 this.apiPackage = apiPackage;
98 this.apiDescription = apiDescription;
Brian O'Connor79b70672016-10-20 13:44:52 -070099 this.includedResources = includedResources;
Brian O'Connoree674952016-09-13 16:31:45 -0700100 this.mavenDeps = computeMavenDeps();
101 }
102
103 private ImmutableSortedSet<HasMavenCoordinates> computeMavenDeps() {
104 ImmutableSortedSet.Builder<HasMavenCoordinates> mavenDeps = ImmutableSortedSet.naturalOrder();
105 for (JavaLibrary javaLibrary : getTransitiveClasspathDeps()) {
106 if (this.equals(javaLibrary)) {
107 // no need to include ourself
108 continue;
109 } else if (HasMavenCoordinates.MAVEN_COORDS_PRESENT_PREDICATE.apply(javaLibrary)) {
110 mavenDeps.add(javaLibrary);
111 //FIXME BOC do we always want to exclude all of a maven jar's dependencies? probably.
112 mavenDeps.addAll(javaLibrary.getTransitiveClasspathDeps());
113 }
114 }
115 return mavenDeps.build();
116 }
117
118 @Override
119 public Iterable<HasMavenCoordinates> getMavenDeps() {
120 return mavenDeps;
121 }
122
123 @Override
124 public Iterable<BuildRule> getPackagedDependencies() {
125 //FIXME this is not supported at the moment
126 return ImmutableList.of();
Brian O'Connore8468b52016-07-25 13:42:36 -0700127 }
Brian O'Connor69e37d92016-10-12 15:05:09 -0700128
129 @Override
130 public Optional<Path> getPomTemplate() {
131 //FIXME we should consider supporting this
132 return Optional.absent();
133 }
Brian O'Connore8468b52016-07-25 13:42:36 -0700134}