java_binary rule not adding main class to the manifest?

979 views
Skip to first unread message

Rohit Saboo

unread,
Feb 2, 2016, 9:13:42 PM2/2/16
to bazel-discuss
Hi all,

I tried creating a java_binary rule, but for some reason the value of the main-class attribute is not being added to the manifest file in the jar. This doesn't matter if I run the java program via the generated launcher script. However, it's an issue if I have to run via the jar file.

Is this a known issue, and is there a workaround for this in the interim?

Thanks,
Rohit

Alex Humesky

unread,
Feb 3, 2016, 5:19:14 PM2/3/16
to Rohit Saboo, bazel-discuss
You'll want to build the deploy jar for your binary, by adding "_deploy.jar" to your target name. That will create a jar with the binary's class files and the class files of the dependencies of that binary, and it should add the main class to the jar's manifest.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAF-%2BGwct0%2BJEp7R6%3D-ojhWLPet5vii9Oii7gW67bN-mi7TiS%3DA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Alex Humesky

unread,
Feb 3, 2016, 5:20:25 PM2/3/16
to Rohit Saboo, bazel-discuss
That is, if you have a java_binary(name = "Test", ...), you can do "bazel build mypackage:Test_deploy.jar"

Rohit Saboo

unread,
Feb 3, 2016, 6:47:59 PM2/3/16
to Alex Humesky, bazel-discuss
Ah okay. Is there a way to ask Bazel to generate the deploy target, too, by default, by perhaps explicitly adding some rule?

Rohit

Alex Humesky

unread,
Feb 3, 2016, 7:18:10 PM2/3/16
to Rohit Saboo, bazel-discuss
Hmm, I'm not sure I understand, could you elaborate? Normally if you're doing "bazel build mypackage:Test", you would just change that to "bazel build mypackage:Test_deploy.jar".

Rohit Saboo

unread,
Feb 3, 2016, 7:20:39 PM2/3/16
to Alex Humesky, bazel-discuss
When we say bazel build //path:..., the deploy package isn't normally built. Is there something that can be done to have it be built in those cases?

Rohit

Alex Humesky

unread,
Feb 3, 2016, 7:53:14 PM2/3/16
to Rohit Saboo, bazel-discuss
I'm not aware of a clean way to do this. I would usually just refer to exactly what I want to build, since I would eventually need to refer to them explicitly in bazel-bin anyway.

Can you elaborate on your general use case? For example, if you have many different java_binary rules you want to build the deploy jar from as a part of a release process, you can do something like

$ bazel query "kind(java_binary, java/...)"

which will give all the labels for all the java_binary rules under the java package. Then you can add "_deploy.jar" to those and build them explicitly. E.g.:

$ bazel query "kind(java_binary, java/...)" | sed 's/$/_deploy.jar/' | xargs bazel build
You'd still need to find them in bazel-bin (maybe you use the results of the query).

Rohit Saboo

unread,
Feb 3, 2016, 7:59:27 PM2/3/16
to Alex Humesky, bazel-discuss
Hi Alex,

Yep basically needed that for automated processes. This sample command works well.

Thanks,
Rohit

Rohit

Alex Humesky

unread,
Feb 3, 2016, 8:04:26 PM2/3/16
to Rohit Saboo, bazel-discuss
Great, glad that helps.

Thinking more about this, we could potentially add an output group in bazel for deploy jars, then you could do something like "bazel build //package/... --output_groups=+deploy_jars" (you can, for example, do "--output_groups=+_source_jars" to get all the source jars for java rules). You'd still have the problem of finding them in bazel-bin, so you'd probably end up doing a bazel query anyway. You can make a request for this at https://github.com/bazelbuild/bazel/issues if you would like.
Reply all
Reply to author
Forward
0 new messages