diff --git a/WORKSPACE b/WORKSPACE index d3047a39d..603b6727a 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -57,6 +57,10 @@ kt_register_toolchains() # dependencies. So, we omit them to keep the WORKSPACE file simpler. # https://skydoc.bazel.build/docs/getting_started_stardoc.html +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("//:repositories.bzl", "rules_jvm_external_deps") rules_jvm_external_deps() diff --git a/examples/android_instrumentation_test/WORKSPACE b/examples/android_instrumentation_test/WORKSPACE index 075db85cc..cf67bd0e2 100644 --- a/examples/android_instrumentation_test/WORKSPACE +++ b/examples/android_instrumentation_test/WORKSPACE @@ -14,6 +14,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( diff --git a/examples/android_kotlin_app/WORKSPACE b/examples/android_kotlin_app/WORKSPACE index 05405fb68..3a2e859ba 100644 --- a/examples/android_kotlin_app/WORKSPACE +++ b/examples/android_kotlin_app/WORKSPACE @@ -29,6 +29,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( diff --git a/examples/android_local_test/WORKSPACE b/examples/android_local_test/WORKSPACE index fe521a6f2..e303bcf25 100644 --- a/examples/android_local_test/WORKSPACE +++ b/examples/android_local_test/WORKSPACE @@ -21,6 +21,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( diff --git a/examples/java-export/WORKSPACE b/examples/java-export/WORKSPACE index 8c62ca523..593046af3 100644 --- a/examples/java-export/WORKSPACE +++ b/examples/java-export/WORKSPACE @@ -5,6 +5,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps") rules_jvm_external_deps() diff --git a/examples/kt_jvm_export/WORKSPACE b/examples/kt_jvm_export/WORKSPACE index 3c0eac7d8..af3845428 100644 --- a/examples/kt_jvm_export/WORKSPACE +++ b/examples/kt_jvm_export/WORKSPACE @@ -3,6 +3,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps") rules_jvm_external_deps() diff --git a/examples/pom_file_generation/WORKSPACE b/examples/pom_file_generation/WORKSPACE index 9f563b192..f38faa6a9 100644 --- a/examples/pom_file_generation/WORKSPACE +++ b/examples/pom_file_generation/WORKSPACE @@ -5,6 +5,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( diff --git a/examples/protobuf-java/WORKSPACE b/examples/protobuf-java/WORKSPACE index a662f0848..700b5acfc 100644 --- a/examples/protobuf-java/WORKSPACE +++ b/examples/protobuf-java/WORKSPACE @@ -47,6 +47,10 @@ local_repository( path = "../..", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( diff --git a/examples/scala_akka/WORKSPACE b/examples/scala_akka/WORKSPACE index a4f00c46d..8429f4719 100644 --- a/examples/scala_akka/WORKSPACE +++ b/examples/scala_akka/WORKSPACE @@ -3,6 +3,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( diff --git a/examples/simple/WORKSPACE b/examples/simple/WORKSPACE index 2b2d86692..b5e45c30e 100644 --- a/examples/simple/WORKSPACE +++ b/examples/simple/WORKSPACE @@ -8,6 +8,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( diff --git a/examples/spring_boot/WORKSPACE b/examples/spring_boot/WORKSPACE index ae9408ff0..3ecda55e3 100644 --- a/examples/spring_boot/WORKSPACE +++ b/examples/spring_boot/WORKSPACE @@ -3,6 +3,10 @@ local_repository( path = "../../", ) +load("//:set_license_classifier.bzl", "use_default_license_classifier") + +use_default_license_classifier() + load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( diff --git a/license_demo/BUILD b/license_demo/BUILD new file mode 100644 index 000000000..bebc5ba0d --- /dev/null +++ b/license_demo/BUILD @@ -0,0 +1,9 @@ + + +java_library( + name = "my_lib", + srcs = ["MyLib.java"], + deps = [ + "@maven//:junit_junit", + ], +) diff --git a/license_demo/MyLib.java b/license_demo/MyLib.java new file mode 100644 index 000000000..b6da0b499 --- /dev/null +++ b/license_demo/MyLib.java @@ -0,0 +1,10 @@ +/** + * + */ +final class MyLib { + + public MyLib() { + } + + int foo() { return 1; } +} diff --git a/license_demo/WORKSPACE b/license_demo/WORKSPACE new file mode 100644 index 000000000..e1afc07ca --- /dev/null +++ b/license_demo/WORKSPACE @@ -0,0 +1,62 @@ +# Copyright 2022 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +workspace(name = "demo") + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "rules_license", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.1/rules_license-0.0.1.tar.gz", + ], + sha256 = "4865059254da674e3d18ab242e21c17f7e3e8c6b1f1421fffa4c5070f82e98b5", +) + +# You must expose your own license declarations as a repository so the rules +# generated in @maven can point to them. Otherwise you would have to generate +# the declarations in @maven. +local_repository( + name = "my_compliance", + path = "compliance", +) + + +# Point to parent for testing. You would use http_archive in real life. +local_repository( + name = "rules_jvm_external", + path = "..", +) + + +load("@rules_jvm_external//:set_license_classifier.bzl", "set_license_classifier") +set_license_classifier(path="compliance") + +# You should never have to call this. rules_jvm_external_setup does it for you. +#load("@rules_jvm_external//:set_license_classifier.bzl", "use_default_license_classifier") +#use_default_license_classifier() + +load("@rules_jvm_external//:defs.bzl", "maven_install") + +maven_install( + artifacts = [ + "junit:junit:4.12", + "com.google.guava:guava:28.0-jre", + ], + fetch_sources = True, + repositories = [ + "http://uk.maven.org/maven2", + "https://jcenter.bintray.com/", + ], +) diff --git a/license_demo/compliance/BUILD b/license_demo/compliance/BUILD new file mode 100644 index 000000000..5d9c03ba7 --- /dev/null +++ b/license_demo/compliance/BUILD @@ -0,0 +1,16 @@ +# Organization local compliance rules go here + +load("@rules_license//rules:license.bzl", "license") + +package( + default_applicable_licenses = [":license"], + default_visibility = ["//visibility:public"], +) + +license( + name = "apache", + license_kinds = [ + "@rules_license//licenses/spdx:Apache-2.0" + ], + license_text = "LICENSE_apache.txt", +) diff --git a/license_demo/compliance/LICENSE_apache.txt b/license_demo/compliance/LICENSE_apache.txt new file mode 100644 index 000000000..7fb3a0270 --- /dev/null +++ b/license_demo/compliance/LICENSE_apache.txt @@ -0,0 +1 @@ +apache license diff --git a/license_demo/compliance/WORKSPACE b/license_demo/compliance/WORKSPACE new file mode 100644 index 000000000..e69de29bb diff --git a/license_demo/compliance/license_classifier.bzl b/license_demo/compliance/license_classifier.bzl new file mode 100644 index 000000000..c5bf1e5ff --- /dev/null +++ b/license_demo/compliance/license_classifier.bzl @@ -0,0 +1,9 @@ +"""Users will provider their own. + +This is a respository rule. +""" + + +def lookup_license(url=None, sha256=None, maven_id=None): + print("==================================== yes") + return "@my_compliance//:apache" diff --git a/private/dependency_tree_parser.bzl b/private/dependency_tree_parser.bzl index 57846ccb5..937310ea1 100644 --- a/private/dependency_tree_parser.bzl +++ b/private/dependency_tree_parser.bzl @@ -17,6 +17,7 @@ This file contains parsing functions to turn a JSON-like dependency tree into target declarations (jvm_import) for the final @maven//:BUILD file. """ +load("@rules_jvm_license_classifier//:license_classifier.bzl", "lookup_license") load("//private:coursier_utilities.bzl", "escape", "get_classifier", "get_packaging", "strip_packaging_and_classifier", "strip_packaging_and_classifier_and_version") JETIFY_INCLUDE_LIST_JETIFY_ALL = ["*"] @@ -293,6 +294,12 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar target_import_string.append("\tvisibility = [%s]," % (",".join(["\"%s\"" % v for v in default_visibilities]))) alias_visibility = "\tvisibility = [%s],\n" % (",".join(["\"%s\"" % v for v in default_visibilities])) + + # Splice in the license + lic = lookup_license(maven_id = simple_coord) + if lic: + target_import_string.append("\tapplicable_licenses = [\"%s\"]," % lic) + # 9. Finish the java_import rule. # # java_import( diff --git a/repositories.bzl b/repositories.bzl index 553e851ac..2d535d0a2 100644 --- a/repositories.bzl +++ b/repositories.bzl @@ -1,5 +1,4 @@ load("//:defs.bzl", "maven_install") -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") _DEFAULT_REPOSITORIES = [ "https://repo1.maven.org/maven2", diff --git a/set_license_classifier.bzl b/set_license_classifier.bzl new file mode 100644 index 000000000..3785867ee --- /dev/null +++ b/set_license_classifier.bzl @@ -0,0 +1,52 @@ +# Copyright 2022 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def _ensure_rules_license(): + if not native.existing_rule("rules_license"): + http_archive( + name = "rules_license", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.1/rules_license-0.0.1.tar.gz", + ], + sha256 = "4865059254da674e3d18ab242e21c17f7e3e8c6b1f1421fffa4c5070f82e98b5", + ) + + +def set_default_license_classifier_impl(rctx): + rctx.file("license_classifier.bzl", content = """ +def lookup_license(url=None, sha256=None, maven_id=None): + return None +""") + rctx.file("BUILD", content = "") + +_set_default_license_classifier = repository_rule( + implementation=set_default_license_classifier_impl, +) + +def use_default_license_classifier(): + _ensure_rules_license() + if not native.existing_rule("rules_jvm_license_classifier"): + _set_default_license_classifier( + name = "rules_jvm_license_classifier", + ) + +def set_license_classifier(path): + _ensure_rules_license() + if native.existing_rule("rules_jvm_license_classifier"): + fail("You are trying to set the rules_jvm_external license classifier a second time.") + native.local_repository( + name = "rules_jvm_license_classifier", + path = path)