Skip to content

Commit af52834

Browse files
authored
Adding Scala and multiple POM files support. (#217)
## What is the goal of this PR? Fixing four issues: 1. I'm also using Scala here, which is almost identical in deployment to Java. However, using [scala_library](https://github.com/bazelbuild/rules_scala/blob/master/docs/scala_library.md) with `assemble_maven` doesn't work. 2. The POM file name is hard-coded (`pom.xml`). Because we use both Scala and Java in the same BUILD file, the file was getting overwrite. #215 3. Targets that have only the `files` attribute failed to run because the provider return `srcjar` when there's isn't any defined. #214 4. `target_coordinates` is hard-coded to `[0]`, and it's not working on all cases. There's another thing to consider here it seems. When using exports, I had problem with: target_string = target[JavaLibInfo].target_coordinates.to_list()[-1] The first element in the array was the export, and not the root target itself. It seems like [-1] worked better on couple of scenario I tested it again, but it's probably better to write a logic to pick the right target instead of guessing. ## What are the changes implemented in this PR? - `pom.xml` will now be named based on the target name. So if my `assemble_maven` target name is `hello_world`, the pom is called `hello_world_pom.xml`. - Side effect of the above is that now `deploy_maven` requires a target (it was optional). The reason for that is because `pom.xml` was hard-coded into `deploy_maven`. Now the unique pom file name is being grabbed from the Target directly. - I changed the if condition that extracts jars and srcjars. First I'm checking the target has `files` attribute in it. If it does, I'm checking that there's files in `files`, and lastly I check the first file has JAR file in it (like you previously did). I also modified the way srcjars are being picked. I'm looking for `-src.jar` ending exclusively (the previous code was picking the first file in the list, and it wasn't the right srcjar using `scala_library` targets). - Srcjar is not available on all jars. In case there is no srcjar, the Provider no longer return it (it used to fail here). - When using exports, the first item in the `target_coordinates` array is not the root target (`target_string = target[JavaLibInfo].target_coordinates.to_list()[0]`. It seems like [-1] worked better on couple of scenario I tested it again (as the root target was always the last in the list), but it's probably better to write a logic to pick the right target instead of guessing. ## Notes I tested it on my Bazel project. I didn't find any tests to run the code changes on, so it's probably a good idea to try to run it on your Bazel projects to make sure it didn't break anything I wasn't aware of. Thanks.
1 parent 2b1cace commit af52834

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

maven/templates/rules.bzl

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ def _parse_maven_coordinates(coordinate_string):
164164

165165
def _generate_pom_xml(ctx, maven_coordinates):
166166
# Final 'pom.xml' is generated in 2 steps
167-
preprocessed_template = ctx.actions.declare_file("_pom.xml")
167+
preprocessed_template = ctx.actions.declare_file("_{}_pom.xml".format(ctx.attr.name))
168168

169-
pom_file = ctx.actions.declare_file("pom.xml")
169+
pom_file = ctx.actions.declare_file("{}_pom.xml".format(ctx.attr.name))
170170

171171
maven_pom_deps = ctx.attr.target[MavenPomInfo].maven_pom_deps
172172
deps_coordinates = depset(maven_pom_deps).to_list()
@@ -259,18 +259,23 @@ def _generate_pom_xml(ctx, maven_coordinates):
259259

260260
def _assemble_maven_impl(ctx):
261261
target = ctx.attr.target
262-
target_string = target[JavaLibInfo].target_coordinates.to_list()[0]
262+
target_string = target[JavaLibInfo].target_coordinates.to_list()[-1]
263263

264264
maven_coordinates = _parse_maven_coordinates(target_string)
265265

266266
pom_file = _generate_pom_xml(ctx, maven_coordinates)
267267

268268
# there is also .source_jar which produces '.srcjar'
269-
if hasattr(target, "java"):
270-
jar = target[JavaInfo].outputs.jars[0].class_jar
271-
srcjar = target[JavaInfo].outputs.jars[0].source_jar
272-
elif hasattr(target, "files"):
273-
jar = target.files.to_list()[0]
269+
srcjar = None
270+
271+
if hasattr(target, "files") and target.files.to_list() and target.files.to_list()[0].extension == 'jar':
272+
all_jars = target[JavaInfo].outputs.jars
273+
jar = all_jars[0].class_jar
274+
275+
for output in all_jars:
276+
if output.source_jar.basename.endswith('-src.jar'):
277+
srcjar = output.source_jar
278+
break
274279
else:
275280
fail("Could not find JAR file to deploy in {}".format(target))
276281

@@ -283,10 +288,16 @@ def _assemble_maven_impl(ctx):
283288
executable = ctx.executable._assemble_script,
284289
)
285290

286-
return [
287-
DefaultInfo(files = depset([output_jar, pom_file, srcjar])),
288-
MavenDeploymentInfo(jar = output_jar, pom = pom_file, srcjar = srcjar)
289-
]
291+
if srcjar == None:
292+
return [
293+
DefaultInfo(files = depset([output_jar, pom_file])),
294+
MavenDeploymentInfo(jar = output_jar, pom = pom_file)
295+
]
296+
else:
297+
return [
298+
DefaultInfo(files = depset([output_jar, pom_file, srcjar])),
299+
MavenDeploymentInfo(jar = output_jar, pom = pom_file, srcjar = srcjar)
300+
]
290301

291302
assemble_maven = rule(
292303
attrs = {
@@ -366,7 +377,7 @@ def _deploy_maven_impl(ctx):
366377

367378
lib_jar_link = "lib.jar"
368379
src_jar_link = "lib.srcjar"
369-
pom_xml_link = "pom.xml"
380+
pom_xml_link = ctx.attr.target[MavenDeploymentInfo].pom.basename
370381

371382
ctx.actions.expand_template(
372383
template = ctx.file._deployment_script,
@@ -399,6 +410,7 @@ _default_deployment_properties = None if 'deployment_properties_placeholder' in
399410
deploy_maven = rule(
400411
attrs = {
401412
"target": attr.label(
413+
mandatory = True,
402414
providers = [MavenDeploymentInfo],
403415
doc = "assemble_maven target to deploy"
404416
),

0 commit comments

Comments
 (0)