Skip to content

Commit c1e040b

Browse files
committed
Propagate testonly attribute to generated plugins
"Generate java_plugin targets for annotation processors" (#1102) brought a much-appreciated improvement by synthesizing a frequently repeated pattern. However, it doesn't work for annotation processor-related artifacts whose `testonly` attribute is set to `True`: > ERROR: /path/to/external/maven/BUILD:12345:12: in java_plugin rule > @@maven//:group_artifact__java_plugin__package_Class: non-test target > '@@maven//:group_artifact__java_plugin__package_Class' depends on > testonly target '@@maven//:group_artifact' and doesn't have testonly > attribute set The present change aims at covering the use case by taking a [Java Microbenchmark Harness](https://github.com/openjdk/jmh)-based target as a practical example.
1 parent e3bc919 commit c1e040b

File tree

6 files changed

+197
-4
lines changed

6 files changed

+197
-4
lines changed

MODULE.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,22 @@ dev_maven.install(
288288
],
289289
)
290290

291+
dev_maven.artifact(
292+
name = "service_indexing_testing",
293+
testonly = True,
294+
artifact = "jmh-core",
295+
group = "org.openjdk.jmh",
296+
version = "1.36",
297+
)
298+
299+
dev_maven.artifact(
300+
name = "service_indexing_testing",
301+
testonly = True,
302+
artifact = "jmh-generator-annprocess",
303+
group = "org.openjdk.jmh",
304+
version = "1.36",
305+
)
306+
291307
dev_maven.install(
292308
name = "service_indexing_testing",
293309
artifacts = [

WORKSPACE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,18 @@ maven_install(
475475
"com.google.auto.value:auto-value:1.10.4",
476476
"com.google.auto.value:auto-value-annotations:1.10.4",
477477
"org.projectlombok:lombok:1.18.22",
478+
maven.artifact(
479+
testonly = True,
480+
artifact = "jmh-core",
481+
group = "org.openjdk.jmh",
482+
version = "1.36",
483+
),
484+
maven.artifact(
485+
testonly = True,
486+
artifact = "jmh-generator-annprocess",
487+
group = "org.openjdk.jmh",
488+
version = "1.36",
489+
),
478490
],
479491
maven_install_json = "//tests/custom_maven_install:service_indexing_testing.json",
480492
repositories = [

private/dependency_tree_parser.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,13 @@ genrule(
322322
for annotation_processor in artifact.get("annotation_processors", []):
323323
to_return.append(
324324
"""java_plugin(
325-
\tname = "{name}",
325+
\tname = "{name}",{testonly}
326326
\tdeps = [":{jar_target}"],
327327
\tgenerates_api = True,
328328
processor_class = "{processor_class}",
329329
{alias_visibility})""".format(
330330
name = "{}__java_plugin__{}".format(target_label, escape(annotation_processor)),
331+
testonly = "\n\ttestonly = True," if testonly_artifacts.get(simple_coord) else "",
331332
jar_target = target_label,
332333
processor_class = annotation_processor,
333334
alias_visibility = alias_visibility,

tests/custom_maven_install/service_indexing_testing.json

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
3-
"__INPUT_ARTIFACTS_HASH": 775593107,
4-
"__RESOLVED_ARTIFACTS_HASH": -1860184594,
3+
"__INPUT_ARTIFACTS_HASH": 1872954798,
4+
"__RESOLVED_ARTIFACTS_HASH": -138133658,
55
"artifacts": {
66
"com.google.auto.value:auto-value": {
77
"shasums": {
@@ -15,14 +15,46 @@
1515
},
1616
"version": "1.10.4"
1717
},
18+
"net.sf.jopt-simple:jopt-simple": {
19+
"shasums": {
20+
"jar": "df26cc58f235f477db07f753ba5a3ab243ebe5789d9f89ecf68dd62ea9a66c28"
21+
},
22+
"version": "5.0.4"
23+
},
24+
"org.apache.commons:commons-math3": {
25+
"shasums": {
26+
"jar": "6268a9a0ea3e769fc493a21446664c0ef668e48c93d126791f6f3f757978fee2"
27+
},
28+
"version": "3.2"
29+
},
30+
"org.openjdk.jmh:jmh-core": {
31+
"shasums": {
32+
"jar": "f90974e37d0da8886b5c05e6e3e7e20556900d747c5a41c1023b47c3301ea73c"
33+
},
34+
"version": "1.36"
35+
},
36+
"org.openjdk.jmh:jmh-generator-annprocess": {
37+
"shasums": {
38+
"jar": "c2a88cf8be1eb0870732a7b2e669972efc7f33a145998f568096137f16b20d79"
39+
},
40+
"version": "1.36"
41+
},
1842
"org.projectlombok:lombok": {
1943
"shasums": {
2044
"jar": "ecef1581411d7a82cc04281667ee0bac5d7c0a5aae74cfc38430396c91c31831"
2145
},
2246
"version": "1.18.22"
2347
}
2448
},
25-
"dependencies": {},
49+
"dependencies": {
50+
"org.openjdk.jmh:jmh-core": [
51+
"net.sf.jopt-simple:jopt-simple",
52+
"org.apache.commons:commons-math3"
53+
],
54+
"org.openjdk.jmh:jmh-generator-annprocess": [
55+
"org.openjdk.jmh:jmh-core"
56+
]
57+
},
2658
"packages": {
2759
"com.google.auto.value:auto-value": [
2860
"autovalue.shaded.com.google.auto.common",
@@ -107,6 +139,93 @@
107139
"com.google.auto.value.extension.serializable",
108140
"com.google.auto.value.extension.toprettystring"
109141
],
142+
"net.sf.jopt-simple:jopt-simple": [
143+
"joptsimple",
144+
"joptsimple.internal",
145+
"joptsimple.util"
146+
],
147+
"org.apache.commons:commons-math3": [
148+
"org.apache.commons.math3",
149+
"org.apache.commons.math3.analysis",
150+
"org.apache.commons.math3.analysis.differentiation",
151+
"org.apache.commons.math3.analysis.function",
152+
"org.apache.commons.math3.analysis.integration",
153+
"org.apache.commons.math3.analysis.integration.gauss",
154+
"org.apache.commons.math3.analysis.interpolation",
155+
"org.apache.commons.math3.analysis.polynomials",
156+
"org.apache.commons.math3.analysis.solvers",
157+
"org.apache.commons.math3.complex",
158+
"org.apache.commons.math3.dfp",
159+
"org.apache.commons.math3.distribution",
160+
"org.apache.commons.math3.distribution.fitting",
161+
"org.apache.commons.math3.exception",
162+
"org.apache.commons.math3.exception.util",
163+
"org.apache.commons.math3.filter",
164+
"org.apache.commons.math3.fitting",
165+
"org.apache.commons.math3.fraction",
166+
"org.apache.commons.math3.genetics",
167+
"org.apache.commons.math3.geometry",
168+
"org.apache.commons.math3.geometry.euclidean.oned",
169+
"org.apache.commons.math3.geometry.euclidean.threed",
170+
"org.apache.commons.math3.geometry.euclidean.twod",
171+
"org.apache.commons.math3.geometry.partitioning",
172+
"org.apache.commons.math3.geometry.partitioning.utilities",
173+
"org.apache.commons.math3.linear",
174+
"org.apache.commons.math3.ml.clustering",
175+
"org.apache.commons.math3.ml.distance",
176+
"org.apache.commons.math3.ode",
177+
"org.apache.commons.math3.ode.events",
178+
"org.apache.commons.math3.ode.nonstiff",
179+
"org.apache.commons.math3.ode.sampling",
180+
"org.apache.commons.math3.optim",
181+
"org.apache.commons.math3.optim.linear",
182+
"org.apache.commons.math3.optim.nonlinear.scalar",
183+
"org.apache.commons.math3.optim.nonlinear.scalar.gradient",
184+
"org.apache.commons.math3.optim.nonlinear.scalar.noderiv",
185+
"org.apache.commons.math3.optim.nonlinear.vector",
186+
"org.apache.commons.math3.optim.nonlinear.vector.jacobian",
187+
"org.apache.commons.math3.optim.univariate",
188+
"org.apache.commons.math3.optimization",
189+
"org.apache.commons.math3.optimization.direct",
190+
"org.apache.commons.math3.optimization.fitting",
191+
"org.apache.commons.math3.optimization.general",
192+
"org.apache.commons.math3.optimization.linear",
193+
"org.apache.commons.math3.optimization.univariate",
194+
"org.apache.commons.math3.primes",
195+
"org.apache.commons.math3.random",
196+
"org.apache.commons.math3.special",
197+
"org.apache.commons.math3.stat",
198+
"org.apache.commons.math3.stat.clustering",
199+
"org.apache.commons.math3.stat.correlation",
200+
"org.apache.commons.math3.stat.descriptive",
201+
"org.apache.commons.math3.stat.descriptive.moment",
202+
"org.apache.commons.math3.stat.descriptive.rank",
203+
"org.apache.commons.math3.stat.descriptive.summary",
204+
"org.apache.commons.math3.stat.inference",
205+
"org.apache.commons.math3.stat.ranking",
206+
"org.apache.commons.math3.stat.regression",
207+
"org.apache.commons.math3.transform",
208+
"org.apache.commons.math3.util"
209+
],
210+
"org.openjdk.jmh:jmh-core": [
211+
"org.openjdk.jmh",
212+
"org.openjdk.jmh.annotations",
213+
"org.openjdk.jmh.generators.core",
214+
"org.openjdk.jmh.infra",
215+
"org.openjdk.jmh.profile",
216+
"org.openjdk.jmh.results",
217+
"org.openjdk.jmh.results.format",
218+
"org.openjdk.jmh.runner",
219+
"org.openjdk.jmh.runner.format",
220+
"org.openjdk.jmh.runner.link",
221+
"org.openjdk.jmh.runner.options",
222+
"org.openjdk.jmh.util",
223+
"org.openjdk.jmh.util.lines"
224+
],
225+
"org.openjdk.jmh:jmh-generator-annprocess": [
226+
"org.openjdk.jmh.generators",
227+
"org.openjdk.jmh.generators.annotations"
228+
],
110229
"org.projectlombok:lombok": [
111230
"lombok",
112231
"lombok.delombok.ant",
@@ -126,6 +245,10 @@
126245
"https://repo1.maven.org/maven2/": [
127246
"com.google.auto.value:auto-value",
128247
"com.google.auto.value:auto-value-annotations",
248+
"net.sf.jopt-simple:jopt-simple",
249+
"org.apache.commons:commons-math3",
250+
"org.openjdk.jmh:jmh-core",
251+
"org.openjdk.jmh:jmh-generator-annprocess",
129252
"org.projectlombok:lombok"
130253
]
131254
},
@@ -154,6 +277,11 @@
154277
"com.google.auto.value.processor.AutoValueProcessor"
155278
]
156279
},
280+
"org.openjdk.jmh:jmh-generator-annprocess": {
281+
"javax.annotation.processing.Processor": [
282+
"org.openjdk.jmh.generators.BenchmarkProcessor"
283+
]
284+
},
157285
"org.projectlombok:lombok": {
158286
"javax.annotation.processing.Processor": [
159287
"lombok.launch.AnnotationProcessorHider$AnnotationProcessor",

tests/integration/plugin_targets/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,24 @@ java_library(
1818
deps = [":ClassUsingAnnotationProcessor"],
1919
)
2020

21+
java_binary(
22+
name = "ClassUsingTestOnlyAnnotationProcessor",
23+
testonly = True, # mandatory since dependencies are themselves `testonly`
24+
srcs = ["ClassUsingTestOnlyAnnotationProcessor.java"],
25+
main_class = "org.openjdk.jmh.Main",
26+
plugins = [java_plugin_artifact(
27+
"org.openjdk.jmh:jmh.generator.annprocess",
28+
"org.openjdk.jmh.generators.BenchmarkProcessor",
29+
"service_indexing_testing",
30+
)],
31+
deps = [artifact("org.openjdk.jmh:jmh-core", "service_indexing_testing")],
32+
)
33+
2134
build_test(
2235
name = "build_test",
2336
targets = [
2437
":ClassUsingAnnotationProcessor",
2538
":ClassUsingClassUsingAnnotationProcessor",
39+
":ClassUsingTestOnlyAnnotationProcessor",
2640
],
2741
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tests.integration.plugin_targets;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.Fork;
5+
import org.openjdk.jmh.annotations.Measurement;
6+
import org.openjdk.jmh.annotations.Warmup;
7+
import org.openjdk.jmh.infra.Blackhole;
8+
9+
@Fork(1)
10+
@Warmup(iterations = 2, time = 1)
11+
@Measurement(iterations = 5, time = 1)
12+
public class ClassUsingTestOnlyAnnotationProcessor {
13+
@Benchmark
14+
public void stringBuilder(final Blackhole blackhole) {
15+
blackhole.consume(new StringBuilder().append("foo").append("bar").toString());
16+
}
17+
18+
@Benchmark
19+
public void stringJoin(final Blackhole blackhole) {
20+
blackhole.consume(String.join("", "foo", "bar"));
21+
}
22+
}

0 commit comments

Comments
 (0)