Skip to content

Commit

Permalink
Break workspace-builds into their own example
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Oct 12, 2024
1 parent 43d61cb commit 437e086
Show file tree
Hide file tree
Showing 18 changed files with 627 additions and 159 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
flags:
- ""
- --config=jdk11
- --noenable_bzlmod

# The type of runner that the job will run on
runs-on: ${{ matrix.runs-on }}
Expand All @@ -46,3 +45,6 @@ jobs:

- name: bazel test //... (Java ${{ matrix.java-major-version }})
run: bazelisk --bazelrc=.github/workflows/ci.bazelrc test ${{ matrix.flags }} //...

- name: example workspace-based build
run: cd examples/workspace-based && bazel run gazelle && bazel test //java/test/com/github/bazelcontrib/rulesjvm/example:PassingTest
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ bazel-*
.idea
.ijwb
.eclipse
*~
/comparative-tests/.gradle/
/MODULE.bazel.lock
/examples/workspace-based/java/src/com/github/bazelcontrib/rulesjvm/example/BUILD.bazel
/examples/workspace-based/java/test/com/github/bazelcontrib/rulesjvm/example/BUILD.bazel
144 changes: 87 additions & 57 deletions README.md

Large diffs are not rendered by default.

94 changes: 0 additions & 94 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,95 +1 @@
workspace(name = "contrib_rules_jvm")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_python",
sha256 = "497ca47374f48c8b067d786b512ac10a276211810f4a580178ee9b9ad139323a",
strip_prefix = "rules_python-0.16.1",
url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.16.1.tar.gz",
)

load("//:repositories.bzl", "contrib_rules_jvm_deps", "contrib_rules_jvm_gazelle_deps")

contrib_rules_jvm_deps()

contrib_rules_jvm_gazelle_deps()

load("@apple_rules_lint//lint:repositories.bzl", "lint_deps")

lint_deps()

load("@apple_rules_lint//lint:setup.bzl", "lint_setup")

lint_setup({
"java-checkstyle": "//java:checkstyle-default-config",
"java-pmd": "//java:pmd-config",
"java-spotbugs": "//java:spotbugs-default-config",
})

load("//:setup.bzl", "contrib_rules_jvm_setup")

# gazelle:repository_macro third_party/go/repositories.bzl%go_deps
contrib_rules_jvm_setup()

load("//:gazelle_setup.bzl", "contrib_rules_jvm_gazelle_setup")

contrib_rules_jvm_gazelle_setup()

load("@io_grpc_grpc_java//:repositories.bzl", "IO_GRPC_GRPC_JAVA_ARTIFACTS")
load("@rules_jvm_external//:defs.bzl", "maven_install")
load("//third_party:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")

# These are used for our own tests.
maven_install(
name = "contrib_rules_jvm_tests",
artifacts = [
# These can be versioned independently of the versions in `repositories.bzl`
# so long as the version numbers are higher.
"org.junit.jupiter:junit-jupiter-engine:5.8.2",
"org.junit.jupiter:junit-jupiter-api:5.8.2",
"org.junit.jupiter:junit-jupiter-params:5.8.2",
"org.junit.platform:junit-platform-launcher:1.8.2",
"org.junit.platform:junit-platform-reporting:1.8.2",
"org.junit.platform:junit-platform-suite:1.8.2",
"org.junit.platform:junit-platform-suite-api:1.8.2",
"org.junit.platform:junit-platform-suite-engine:1.8.2",
"org.junit.vintage:junit-vintage-engine:5.8.2",
"org.mockito:mockito-core:4.8.1",
],
fail_if_repin_required = True,
fetch_sources = True,
maven_install_json = "@//:contrib_rules_jvm_tests_install.json",
repositories = [
"https://repo1.maven.org/maven2",
],
)

load("@contrib_rules_jvm_tests//:defs.bzl", maven_pmi = "pinned_maven_install")

maven_pmi()

load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")

stardoc_repositories()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.21.5")

gazelle_dependencies()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()

load("@googleapis//:repository_rules.bzl", "switched_rules_by_language")

switched_rules_by_language(
name = "com_google_googleapis_imports",
)
1 change: 0 additions & 1 deletion WORKSPACE.bzlmod

This file was deleted.

11 changes: 11 additions & 0 deletions examples/workspace-based/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
common --noenable_bzlmod

build --java_runtime_version=remotejdk_17
build --java_language_version=17
build --tool_java_runtime_version=remotejdk_17
build --tool_java_language_version=17

# Make sure we get something helpful when tests fail
test --verbose_failures
test --test_output=errors

1 change: 1 addition & 0 deletions examples/workspace-based/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.4.0
24 changes: 24 additions & 0 deletions examples/workspace-based/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")

# We can choose where the `maven_install` lock file is read from
#
# gazelle:java_maven_install_file java/maven_install.json

# And this maps common types to the ones from `contrib_rules_jvm`. By
# doing this we can use the integration with `apple_rules_lint`.
#
# gazelle:map_kind java_binary java_binary @contrib_rules_jvm//java:defs.bzl
# gazelle:map_kind java_library java_library @contrib_rules_jvm//java:defs.bzl
# gazelle:map_kind java_test java_test @contrib_rules_jvm//java:defs.bzl

gazelle(
name = "gazelle",
gazelle = ":gazelle_bin",
)

gazelle_binary(
name = "gazelle_bin",
languages = DEFAULT_LANGUAGES + [
"@contrib_rules_jvm//java/gazelle",
],
)
19 changes: 19 additions & 0 deletions examples/workspace-based/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Example Workspace-based project

This is a small example of using the `gazelle` java plugin with a
simple source repository.

## Steps to use

1. Run `bazel test //...` and notice that there's almost no tests.
1. `bazel run gazelle` and find the new `BUILD.bazel` files that are created
1. `bazel test //...`

The final run of `bazel test` will fail, as one of the example tests
is written so it won't pass. Try fixing it!

This example also shows how you can make use of the linters that are
supplied with `contrib_rules_jvm`. There are directives in the
top-level `BUILD.bazel` that tell the gazelle plugin to use the macros
from `contrib_rules_jvm` and not just `rules_java`. The configuration
of the linters can be found in the `WORKSPACE` file.
88 changes: 88 additions & 0 deletions examples/workspace-based/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
workspace(name = "contrib_rules_jvm_workspace_example")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
name = "contrib_rules_jvm",
path = "../..",
)

http_archive(
name = "apple_rules_lint",
sha256 = "483ea03d73d5fb33275d029da8d36811243fc32dfa4dc73a43acbb6f4b1af621",
strip_prefix = "apple_rules_lint-0.4.0",
url = "https://github.com/apple/apple_rules_lint/releases/download/0.4.0/apple_rules_lint-0.4.0.tar.gz",
)

load("@apple_rules_lint//lint:repositories.bzl", "lint_deps")

lint_deps()

load("@apple_rules_lint//lint:setup.bzl", "lint_setup")

lint_setup({
"java-checkstyle": "@contrib_rules_jvm//java:checkstyle-default-config",
"java-pmd": "@contrib_rules_jvm//java:pmd-config",
"java-spotbugs": "@contrib_rules_jvm//java:spotbugs-default-config",
})

load("@contrib_rules_jvm//:repositories.bzl", "contrib_rules_jvm_deps", "contrib_rules_jvm_gazelle_deps")

contrib_rules_jvm_deps()

load("@contrib_rules_jvm//:setup.bzl", "contrib_rules_jvm_setup")

contrib_rules_jvm_setup()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"com.google.guava:guava:33.3.1-jre",
"org.junit.jupiter:junit-jupiter-engine:5.8.2",
"org.junit.jupiter:junit-jupiter-api:5.8.2",
"org.junit.platform:junit-platform-launcher:1.8.2",
"org.junit.platform:junit-platform-reporting:1.8.2",
],
maven_install_json = "//java:maven_install.json",
repositories = [
"https://repo1.maven.org/maven2",
],
)

load("@maven//:defs.bzl", "pinned_maven_install")

pinned_maven_install()

http_archive(
name = "io_bazel_rules_go",
integrity = "sha256-M6zErg9wUC20uJPJ/B3Xqb+ZjCPn/yxFF3QdQEmpdvg=",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
],
)

http_archive(
name = "bazel_gazelle",
integrity = "sha256-12v3pg/YsFBEQJDfooN6Tq+YKeEWVhjuNdzspcvfWNU=",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

contrib_rules_jvm_gazelle_deps()

load("@contrib_rules_jvm//:gazelle_setup.bzl", "contrib_rules_jvm_gazelle_setup")

contrib_rules_jvm_gazelle_setup()

go_rules_dependencies()

go_register_toolchains(version = "1.23.2")

gazelle_dependencies()
Empty file.
Loading

0 comments on commit 437e086

Please sign in to comment.