blob: dde1902a64f851d4e230bf2c431bbeb0ce933c0b [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,
Brian O'Connor69e37d92016-10-12 15:05:09 -070077 Optional<SourcePath> manifestFile,
Brian O'Connore8468b52016-07-25 13:42:36 -070078 Optional<String> mavenCoords,
79 ImmutableSortedSet<BuildTarget> tests,
80 ImmutableSet<Pattern> classesToRemoveFromJar,
81 Optional<String> webContext,
82 Optional<String> apiTitle,
83 Optional<String> apiVersion,
84 Optional<String> apiPackage,
85 Optional<String> apiDescription) {
86 super(params, resolver, srcs, resources, generatedSourceFolder,
87 proguardConfig, postprocessClassesCommands, exportedDeps,
88 providedDeps, abiJar, trackClassUsage, additionalClasspathEntries,
Brian O'Connor69e37d92016-10-12 15:05:09 -070089 compileStepFactory, resourcesRoot, manifestFile, mavenCoords,
90 tests, classesToRemoveFromJar);
Brian O'Connore8468b52016-07-25 13:42:36 -070091 this.webContext = webContext;
92 this.apiTitle = apiTitle;
93 this.apiVersion = apiVersion;
94 this.apiPackage = apiPackage;
95 this.apiDescription = apiDescription;
Brian O'Connoree674952016-09-13 16:31:45 -070096 this.mavenDeps = computeMavenDeps();
97 }
98
99 private ImmutableSortedSet<HasMavenCoordinates> computeMavenDeps() {
100 ImmutableSortedSet.Builder<HasMavenCoordinates> mavenDeps = ImmutableSortedSet.naturalOrder();
101 for (JavaLibrary javaLibrary : getTransitiveClasspathDeps()) {
102 if (this.equals(javaLibrary)) {
103 // no need to include ourself
104 continue;
105 } else if (HasMavenCoordinates.MAVEN_COORDS_PRESENT_PREDICATE.apply(javaLibrary)) {
106 mavenDeps.add(javaLibrary);
107 //FIXME BOC do we always want to exclude all of a maven jar's dependencies? probably.
108 mavenDeps.addAll(javaLibrary.getTransitiveClasspathDeps());
109 }
110 }
111 return mavenDeps.build();
112 }
113
114 @Override
115 public Iterable<HasMavenCoordinates> getMavenDeps() {
116 return mavenDeps;
117 }
118
119 @Override
120 public Iterable<BuildRule> getPackagedDependencies() {
121 //FIXME this is not supported at the moment
122 return ImmutableList.of();
Brian O'Connore8468b52016-07-25 13:42:36 -0700123 }
Brian O'Connor69e37d92016-10-12 15:05:09 -0700124
125 @Override
126 public Optional<Path> getPomTemplate() {
127 //FIXME we should consider supporting this
128 return Optional.absent();
129 }
Brian O'Connore8468b52016-07-25 13:42:36 -0700130}