aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Kristina Chodorow <kchodorow@google.com>2015-04-23 19:16:06 +0000
committerGravatar Lukacs Berki <lberki@google.com>2015-04-24 10:14:11 +0000
commit464edea15fe3f5d01f2625c2ca0319a08088c155 (patch)
tree216ea566fdf04ec82f0599ddc5f958fc0e405a6b
parent3b2ac8e731f6e523e96ddcd4886164d9e0acffa6 (diff)
Look for resources under Maven's standard directory layout
Doesn't completely fix but improves #97 for a lot of people. -- MOS_MIGRATED_REVID=91909946
-rw-r--r--examples/java-native/src/main/java/com/example/myproject/Greeter.java3
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java19
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java6
-rw-r--r--src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemanticsTest.java38
4 files changed, 59 insertions, 7 deletions
diff --git a/examples/java-native/src/main/java/com/example/myproject/Greeter.java b/examples/java-native/src/main/java/com/example/myproject/Greeter.java
index 4123f2e202..fa10562aab 100644
--- a/examples/java-native/src/main/java/com/example/myproject/Greeter.java
+++ b/examples/java-native/src/main/java/com/example/myproject/Greeter.java
@@ -33,8 +33,7 @@ public class Greeter {
public void hello(String obj) throws Exception {
String greeting = "Hello";
- InputStream stream = Greeter.class.getResourceAsStream(
- "/examples/java-native/src/main/resources/greeting.txt");
+ InputStream stream = Greeter.class.getResourceAsStream("/greeting.txt");
if (stream != null) {
greeting = convertStreamToString(stream);
}
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java
index f11a3cac82..bae17e8927 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java
@@ -194,12 +194,21 @@ public class BazelJavaRuleClasses {
/* <!-- #BLAZE_RULE($java_rule).ATTRIBUTE(resources) -->
A list of data files to include in a Java jar.
${SYNOPSIS}
- If resources are specified, they will be bundled in the jar along with the usual
- <code>.class</code> files produced by compilation. 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. Resources may be source files or generated files.
+ <p>
+ If resources are specified, they will be bundled in the jar along with the usual
+ <code>.class</code> files produced by compilation. The location of the resources inside
+ of the jar file is determined by the project structure. Bazel first looks for Maven's
+ <a href="https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html">standard directory layout</a>,
+ (a "src" directory followed by a "resources" directory grandchild). If that is not
+ found, Bazel then looks for the topmost directory named "java" or "javatests" (so, for
+ example, if a resource is at &lt;workspace root&gt;/x/java/y/java/z, Bazel will use the
+ path y/java/z. This heuristic cannot be overridden.
+ </p>
+
+ <p>
+ Resources may be source files or generated files.
+ </p>
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
- // TODO(bazel-team): be more specific about this heuristic.
.add(attr("resources", LABEL_LIST).orderIndependent()
.allowedFileTypes(FileTypeSet.ANY_FILE))
/* <!-- #BLAZE_RULE($java_rule).ATTRIBUTE(plugins) -->
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
index f1a01ad3f9..11aefb8d13 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
@@ -329,6 +329,12 @@ public class BazelJavaSemantics implements JavaSemantics {
@Override
public PathFragment getJavaResourcePath(PathFragment path) {
+ // Look for src/.../resources to match Maven repository structure.
+ for (int i = 0; i < path.segmentCount() - 2; ++i) {
+ if (path.getSegment(i).equals("src") && path.getSegment(i + 2).equals("resources")) {
+ return path.subFragment(i + 3, path.segmentCount());
+ }
+ }
PathFragment javaPath = JavaUtil.getJavaPath(path);
return javaPath == null ? path : javaPath;
}
diff --git a/src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemanticsTest.java b/src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemanticsTest.java
new file mode 100644
index 0000000000..f109290ee0
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemanticsTest.java
@@ -0,0 +1,38 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.bazel.rules.java;
+
+import com.google.devtools.build.lib.vfs.PathFragment;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for {@link BazelJavaSemantics}.
+ */
+public class BazelJavaSemanticsTest extends TestCase {
+ public void testFindingResources() {
+ BazelJavaSemantics semantics = BazelJavaSemantics.INSTANCE;
+ assertEquals(PathFragment.EMPTY_FRAGMENT,
+ semantics.getJavaResourcePath(new PathFragment("x/y/src/main/resources")));
+ assertEquals(new PathFragment("foo"),
+ semantics.getJavaResourcePath(new PathFragment("x/y/src/main/resources/foo")));
+ assertEquals(new PathFragment("foo"),
+ semantics.getJavaResourcePath(new PathFragment("java/x/y/src/main/resources/foo")));
+ assertEquals(new PathFragment("foo/java/bar"),
+ semantics.getJavaResourcePath(new PathFragment("java/foo/java/bar")));
+ assertEquals(new PathFragment("foo/java/bar"),
+ semantics.getJavaResourcePath(new PathFragment("javatests/foo/java/bar")));
+ }
+}