aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-06-21 16:53:08 +0000
committerGravatar Philipp Wollermann <philwo@google.com>2016-06-22 10:45:30 +0000
commitf68ae5d90ba5ffb46d285e83bbc0a3cdb3597efa (patch)
tree36797e69038d5c923ee57fb759341116b6864977 /src/main/java/com/google/devtools/build/lib/rules
parent9f40da7e56aa740a3e50a08c3643806851a305f2 (diff)
Consider /src/ in the path when locating the java root directory. -- MOS_MIGRATED_REVID=125461909
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java46
1 files changed, 43 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java
index c4f95fe22c..e5677d813e 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java
@@ -61,8 +61,13 @@ public final class JavaUtil {
}
/**
- * Find the index of the "java" or "javatests" segment in a Java path fragment
- * that precedes the source root.
+ * Finds the index of the segment in a Java path fragment that precedes the source root.
+ * Starts from the first "java" or "javatests" or "src" segment.
+ * If the found item was "src", check if this is followed by "main" or "test" and then "java"
+ * or "resources" (maven layout).
+ * If the found item was "src", or "java"/"javatests" at the first segment, check for a nested
+ * root directory (src, java or javatests). A nested root must be followed by (com|net|org),
+ * or matching maven structure for nested "src", to be accepted, to avoid false positives.
*
* @param path a Java source dir or file path
* @return the index of the java segment or -1 iff no java segment was found.
@@ -71,7 +76,42 @@ public final class JavaUtil {
if (path.isAbsolute()) {
throw new IllegalArgumentException("path must not be absolute: '" + path + "'");
}
- return path.getFirstSegment(ImmutableSet.of("java", "javatests"));
+ int rootIndex = path.getFirstSegment(ImmutableSet.of("java", "javatests", "src"));
+ if (rootIndex < 0) {
+ return rootIndex;
+ }
+ final boolean isSrc = "src".equals(path.getSegment(rootIndex));
+ int checkMavenIndex = isSrc ? rootIndex : -1;
+ if (rootIndex == 0 || isSrc) {
+ // Check for a nested "src" directory.
+ // Also, to support an existing case, "javatests" within "src".
+ for (int i = rootIndex + 1, max = path.segmentCount() - 2; i <= max; i++) {
+ String segment = path.getSegment(i);
+ if ("src".equals(segment)
+ || (isSrc && ("javatests".equals(segment) || "java".equals(segment)))) {
+ String next = path.getSegment(i + 1);
+ if ("com".equals(next) || "org".equals(next) || "net".equals(next)) {
+ // Check for common first element of java package, to avoid false positives.
+ rootIndex = i;
+ } else if ("src".equals(segment)) {
+ // Also accept maven style src/(main|test)/(java|resources).
+ checkMavenIndex = i;
+ }
+ break;
+ }
+ }
+ }
+ // Check for (main|test)/(java|resources) after /src/.
+ if (checkMavenIndex >= 0 && checkMavenIndex + 2 < path.segmentCount()) {
+ String next = path.getSegment(checkMavenIndex + 1);
+ if ("main".equals(next) || "test".equals(next)) {
+ next = path.getSegment(checkMavenIndex + 2);
+ if ("java".equals(next) || "resources".equals(next)) {
+ rootIndex = checkMavenIndex + 2;
+ }
+ }
+ }
+ return rootIndex;
}
/**