Resource file location

2,564 views
Skip to first unread message

Erik Kuefler

unread,
Apr 3, 2015, 5:52:22 PM4/3/15
to bazel-...@googlegroups.com
The documentation for the resource attribute in java_library says that "the location of the resources inside of the jar file is determined using a heuristic, but it's often in a directory corresponding to the build package name." Is this heuristic documented anywhere and is there a way to control it? I'm depending on a resource "processors/src/main/resources/velocity.properties" from a root "AnnotationProcessors" package, and Bazel is placing the file at "AnnotationProcessors/processors/src/main/resources/velocity.properties". Ideally I'd like it to match existing Gradle behavior that treats "src/main/resources" as a root, so the properties file would appear at the root level in the jar.

Kristina Chodorow

unread,
Apr 6, 2015, 3:55:04 PM4/6/15
to Erik Kuefler, bazel-...@googlegroups.com
It'll be under the java/ directory if there is one (so if your directory structure was java/src/main/... you'd get the right path) or your build root (as you discovered).  There is no way to control this at the moment, although it's on our radar to add an option for it.  I've filed https://github.com/google/bazel/issues/97 to track.

On Fri, Apr 3, 2015 at 5:52 PM, Erik Kuefler <ekue...@gmail.com> wrote:
The documentation for the resource attribute in java_library says that "the location of the resources inside of the jar file is determined using a heuristic, but it's often in a directory corresponding to the build package name." Is this heuristic documented anywhere and is there a way to control it? I'm depending on a resource "processors/src/main/resources/velocity.properties" from a root "AnnotationProcessors" package, and Bazel is placing the file at "AnnotationProcessors/processors/src/main/resources/velocity.properties". Ideally I'd like it to match existing Gradle behavior that treats "src/main/resources" as a root, so the properties file would appear at the root level in the jar.

--
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 post to this group, send email to bazel-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/95de7428-0ece-4cd6-bfc4-a6cae596cfba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erik Kuefler

unread,
Apr 6, 2015, 5:28:50 PM4/6/15
to Kristina Chodorow, bazel-...@googlegroups.com
Thanks, that's helpful. I'll move my resource files under java/ for now. IMO it would be a good idea for the heuristic to identify "resources" as well as "java" as a root, since this is a very common layout in open-source projects: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Thomas Broyer

unread,
Apr 7, 2015, 5:29:20 AM4/7/15
to bazel-...@googlegroups.com, kcho...@google.com


On Monday, April 6, 2015 at 11:28:50 PM UTC+2, Erik Kuefler wrote:
Thanks, that's helpful. I'll move my resource files under java/ for now. IMO it would be a good idea for the heuristic to identify "resources" as well as "java" as a root, since this is a very common layout in open-source projects: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

If possible maybe not "resources" but more "src/*/resources": GWT for example has a com.google.gwt.resources package.

FWIW, Buck currently has a java.src_roots in .buckconfig which they acknowledge is a hack only for that use-case and they'd like to get rid of it eventually. So maybe they thought a bit about it and have some ideas?
Pants has a similar config, with a maven_layout shortcut, but paths are relative to the BUILD file they're declared in (so they can be declared anywhere in the tree and you can therefore have "exceptions"; not just globally as with Buck): https://pantsbuild.github.io/setup_repo.html#configure-code-layout-with-source_root-maven_layout

Kristina Chodorow

unread,
Apr 24, 2015, 2:08:52 PM4/24/15
to bazel-...@googlegroups.com, kcho...@google.com
Update: Bazel will now look for src/.../resources and use the path below that.

Ram Kumar Rengaswamy

unread,
Jun 22, 2015, 2:55:53 PM6/22/15
to bazel-...@googlegroups.com, kcho...@google.com
Does the old bazel way for placing resources in JAR still work ?
We have a JAVA source tree organized with java/ and javatests/ directories.
For some reason, Bazel is not picking up the list of files specified in the resources attribute of a java_library build rule.
I cannot find them in libfoo_files when I look inside bazel-bin directory.

Kristina Chodorow

unread,
Jun 22, 2015, 3:00:25 PM6/22/15
to Ram Kumar Rengaswamy, bazel-...@googlegroups.com
It still should be picking up resources from java/ and javatests/, too.  Resources should be included in the jar, if you run `jar -tf bazel-bin/path/to/your.jar` does it show them?

Ram Kumar Rengaswamy

unread,
Jun 22, 2015, 3:20:36 PM6/22/15
to bazel-...@googlegroups.com, ra...@beeswax.com
Thanks ! You are right. 
jar -tf output does indeed contain the resources.
However, I couldn't find the resources in the directory named bazel-bin/path/to/libyourjar_files
I guess that threw me off.
Out of curiosity, where does bazel store the resource files before packaging them into the JAR ?

Kristina Chodorow

unread,
Jun 22, 2015, 3:27:10 PM6/22/15
to Ram Kumar Rengaswamy, bazel-...@googlegroups.com
It doesn't really store them anywhere, but it runs jar from the "execution root."  If you run "bazel info" you can see where that is (or use `bazel build -s //whatever` to see the gory details of where and how it's running "jar"). 

If you want the file available on the filesystem, you might want to use data = [], instead of resources = [].

--
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.
Reply all
Reply to author
Forward
0 new messages