aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2015-10-06 11:16:31 +0000
committerGravatar Laszlo Csomor <laszlocsomor@google.com>2015-10-06 11:41:00 +0000
commitbeccb9bfee87fb4869b5c00abd9400ebaf647bf0 (patch)
treeee8bfb4adb21ef6076044f7b6e1e11795c5bb111 /src
parentfb8ee791d3b39f294bf4e083b972aee2ac6f8e27 (diff)
Implement the prefix stripping for Java resources.
This CL sure works, but it leaves a few call sites for JavaSemantics#getJavaResourcePath() which make me uncomfortable. RELNOTES: Java rules now support a resource_strip_prefix attribute that allows the removal of path prefixes from Java resources. -- MOS_MIGRATED_REVID=104748537
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java12
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java9
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/BaseJavaCompilationHelper.java17
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java6
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java9
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java26
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaHelper.java22
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaSourceInfoProvider.java11
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java29
-rw-r--r--src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemanticsTest.java10
13 files changed, 95 insertions, 64 deletions
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 5cd576f567..4925cb7fcd 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
@@ -223,6 +223,18 @@ public class BazelJavaRuleClasses {
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("resources", LABEL_LIST).orderIndependent()
.allowedFileTypes(FileTypeSet.ANY_FILE))
+ /* <!-- #BLAZE_RULE($java_rule).ATTRIBUTE(resource_strip_prefix) -->
+ The path prefix to strip from Java resources.
+ ${SYNOPSIS}
+ <p>
+ If specified, this path prefix is stripped from every file in the <code>resources</code>
+ attribute. It is an error for a resource file not to be under this directory. If not
+ specified (the default), the path of resource file is determined according to the same
+ logic as the Java package of source files. For example, a source file at
+ <code>stuff/java/foo/bar/a.txt</code> will be located at <code>foo/bar/a.txt</code>.
+ </p>
+ <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
+ .add(attr("resource_strip_prefix", STRING))
/* <!-- #BLAZE_RULE($java_rule).ATTRIBUTE(plugins) -->
Java compiler plugins to run at compile-time.
${SYNOPSIS}
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 8208ee98ee..d702dd9ede 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
@@ -295,7 +295,7 @@ public class BazelJavaSemantics implements JavaSemantics {
}
@Override
- public PathFragment getJavaResourcePath(PathFragment path) {
+ public PathFragment getDefaultJavaResourcePath(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")) {
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java
index 598d121146..59f7ac7770 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java
@@ -19,7 +19,6 @@ import static com.google.devtools.build.lib.analysis.config.BuildConfiguration.S
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.ResourceSet;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
@@ -67,9 +66,7 @@ import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
import javax.annotation.Nullable;
@@ -471,10 +468,6 @@ public class AndroidCommon {
JackCompilationHelper initJack(JavaTargetAttributes attributes, JavaSemantics javaSemantics)
throws InterruptedException {
- Map<PathFragment, Artifact> resourcesMap = new LinkedHashMap<>();
- for (Artifact resource : attributes.getResources()) {
- resourcesMap.put(javaSemantics.getJavaResourcePath(resource.getRootRelativePath()), resource);
- }
AndroidSdkProvider sdk = AndroidSdkProvider.fromRuleContext(ruleContext);
return new JackCompilationHelper.Builder()
// blaze infrastructure
@@ -488,7 +481,7 @@ public class AndroidCommon {
.addJavaSources(attributes.getSourceFiles())
.addSourceJars(attributes.getSourceJars())
.addCompiledJars(attributes.getJarFiles())
- .addResources(ImmutableMap.copyOf(resourcesMap))
+ .addResources(attributes.getResources())
.addProcessorNames(attributes.getProcessorNames())
.addProcessorClasspathJars(attributes.getProcessorPath())
.addExports(JavaCommon.getExports(ruleContext))
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/BaseJavaCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/BaseJavaCompilationHelper.java
index 6a325d44b2..85573f8e3d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/BaseJavaCompilationHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/BaseJavaCompilationHelper.java
@@ -32,6 +32,7 @@ import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Collection;
+import java.util.Map;
/**
* A helper class for compiling Java targets. This helper does not rely on the
@@ -66,16 +67,15 @@ public class BaseJavaCompilationHelper {
"--exclude_build_data",
"--warn_duplicate_resources");
- private CommandLine sourceJarCommandLine(JavaSemantics semantics, Artifact outputJar,
- Iterable<Artifact> resources, Iterable<Artifact> resourceJars) {
+ private CommandLine sourceJarCommandLine(Artifact outputJar,
+ Map<PathFragment, Artifact> resources, Iterable<Artifact> resourceJars) {
CustomCommandLine.Builder args = CustomCommandLine.builder();
args.addExecPath("--output", outputJar);
args.add(SOURCE_JAR_COMMAND_LINE_ARGS);
args.addExecPaths("--sources", resourceJars);
args.add("--resources");
- for (Artifact resource : resources) {
- args.addPaths("%s:%s", resource.getExecPath(),
- semantics.getJavaResourcePath(resource.getRootRelativePath()));
+ for (Map.Entry<PathFragment, Artifact> resource : resources.entrySet()) {
+ args.addPaths("%s:%s", resource.getValue().getExecPath(), resource.getKey());
}
return args.build();
}
@@ -83,23 +83,22 @@ public class BaseJavaCompilationHelper {
/**
* Creates an Action that packages files into a Jar file.
*
- * @param semantics delegate semantics for java.
* @param resources the resources to put into the Jar.
* @param resourceJars the resource jars to merge into the jar
* @param outputJar the Jar to create
*/
- public void createSourceJarAction(JavaSemantics semantics, Collection<Artifact> resources,
+ public void createSourceJarAction(Map<PathFragment, Artifact> resources,
Collection<Artifact> resourceJars, Artifact outputJar) {
ruleContext.registerAction(new SpawnAction.Builder()
.addOutput(outputJar)
- .addInputs(resources)
+ .addInputs(resources.values())
.addInputs(resourceJars)
.addTransitiveInputs(JavaCompilationHelper.getHostJavabaseInputs(ruleContext))
.setJarExecutable(
ruleContext.getHostConfiguration().getFragment(Jvm.class).getJavaExecutable(),
ruleContext.getPrerequisiteArtifact("$singlejar", Mode.HOST),
ImmutableList.of("-client", SINGLEJAR_MAX_MEMORY))
- .setCommandLine(sourceJarCommandLine(semantics, outputJar, resources, resourceJars))
+ .setCommandLine(sourceJarCommandLine(outputJar, resources, resourceJars))
.useParameterFile(ParameterFileType.SHELL_QUOTED)
.setProgressMessage("Building source jar " + outputJar.prettyPrint())
.setMnemonic("JavaSourceJar")
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java
index 84efb5de3b..2855ff110a 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java
@@ -205,7 +205,7 @@ public class JavaBinary implements RuleConfiguredTargetFactory {
// TODO(bazel-team): if (getOptions().sourceJars) then make this a dummy prerequisite for the
// DeployArchiveAction ? Needs a few changes there as we can't pass inputs
- helper.createSourceJarAction(semantics, ImmutableList.<Artifact>of(),
+ helper.createSourceJarAction(ImmutableMap.<PathFragment, Artifact>of(),
transitiveSourceJars.toCollection(),
ruleContext.getImplicitOutputArtifact(JavaSemantics.JAVA_BINARY_DEPLOY_SOURCE_JAR));
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
index 8ffd0f176b..fd9825f32b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
@@ -518,7 +518,11 @@ public class JavaCommon {
ruleContext.attributeError("deps", "deps not allowed without srcs; move to runtime_deps?");
}
- javaTargetAttributes.addResources(semantics.collectResources(ruleContext));
+ for (Artifact resource : semantics.collectResources(ruleContext)) {
+ javaTargetAttributes.addResource(
+ JavaHelper.getJavaResourcePath(semantics, ruleContext, resource), resource);
+ }
+
addPlugins(javaTargetAttributes);
javaTargetAttributes.setRuleKind(ruleContext.getRule().getRuleClass());
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
index 29d3b70ce5..2bb56e9580 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
@@ -34,10 +34,13 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgs.ClasspathType;
import com.google.devtools.build.lib.rules.java.JavaConfiguration.JavaClasspathMode;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
+import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
import javax.annotation.Nullable;
@@ -344,7 +347,11 @@ public class JavaCompilationHelper extends BaseJavaCompilationHelper {
if (gensrcJar != null) {
resourceJars.add(gensrcJar);
}
- createSourceJarAction(semantics, attributes.getSourceFiles(), resourceJars, outputJar);
+ Map<PathFragment, Artifact> resources = new LinkedHashMap<>();
+ for (Artifact sourceFile : attributes.getSourceFiles()) {
+ resources.put(semantics.getDefaultJavaResourcePath(sourceFile.getRootRelativePath()), sourceFile);
+ }
+ createSourceJarAction(resources, resourceJars, outputJar);
}
/**
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java
index d8debf3efe..72ab4fb934 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java
@@ -63,8 +63,10 @@ import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
/**
* Action that represents a Java compilation.
@@ -180,7 +182,7 @@ public class JavaCompileAction extends AbstractAction {
Iterable<Artifact> instrumentationJars,
List<String> processorNames,
Collection<Artifact> messages,
- Collection<Artifact> resources,
+ Map<PathFragment, Artifact> resources,
Collection<Artifact> classpathResources,
Collection<Artifact> sourceJars,
Collection<Artifact> sourceFiles,
@@ -192,7 +194,7 @@ public class JavaCompileAction extends AbstractAction {
.addTransitive(classpathEntries)
.addAll(processorPath)
.addAll(messages)
- .addAll(resources)
+ .addAll(resources.values())
.addAll(classpathResources)
.addAll(sourceJars)
.addAll(sourceFiles)
@@ -213,7 +215,7 @@ public class JavaCompileAction extends AbstractAction {
this.processorPath = ImmutableList.copyOf(processorPath);
this.processorNames = ImmutableList.copyOf(processorNames);
this.messages = ImmutableList.copyOf(messages);
- this.resources = ImmutableList.copyOf(resources);
+ this.resources = ImmutableList.copyOf(resources.values());
this.classpathResources = ImmutableList.copyOf(classpathResources);
this.sourceJars = ImmutableList.copyOf(sourceJars);
this.sourceFiles = ImmutableList.copyOf(sourceFiles);
@@ -462,7 +464,7 @@ public class JavaCompileAction extends AbstractAction {
List<Artifact> processorPath,
List<String> processorNames,
Collection<Artifact> messages,
- Collection<Artifact> resources,
+ Map<PathFragment, Artifact> resources,
Collection<Artifact> classpathResources,
Collection<Artifact> sourceJars,
Collection<Artifact> sourceFiles,
@@ -539,14 +541,15 @@ public class JavaCompileAction extends AbstractAction {
if (!messages.isEmpty()) {
result.add("--messages");
for (Artifact message : messages) {
- addAsResourcePrefixedExecPath(semantics, message, result);
+ addAsResourcePrefixedExecPath(
+ semantics.getDefaultJavaResourcePath(message.getRootRelativePath()), message, result);
}
}
if (!resources.isEmpty()) {
result.add("--resources");
- for (Artifact resource : resources) {
- addAsResourcePrefixedExecPath(semantics, resource, result);
+ for (Map.Entry<PathFragment, Artifact> resource : resources.entrySet()) {
+ addAsResourcePrefixedExecPath(resource.getKey(), resource.getValue(), result);
}
}
@@ -604,10 +607,9 @@ public class JavaCompileAction extends AbstractAction {
return result;
}
- private static void addAsResourcePrefixedExecPath(JavaSemantics semantics,
+ private static void addAsResourcePrefixedExecPath(PathFragment resourcePath,
Artifact artifact, CustomCommandLine.Builder builder) {
PathFragment execPath = artifact.getExecPath();
- PathFragment resourcePath = semantics.getJavaResourcePath(artifact.getRootRelativePath());
if (execPath.equals(resourcePath)) {
builder.addPaths(":%s", resourcePath);
} else {
@@ -734,7 +736,7 @@ public class JavaCompileAction extends AbstractAction {
private Artifact metadata;
private final Collection<Artifact> sourceFiles = new ArrayList<>();
private final Collection<Artifact> sourceJars = new ArrayList<>();
- private final Collection<Artifact> resources = new ArrayList<>();
+ private final Map<PathFragment, Artifact> resources = new LinkedHashMap<>();
private final Collection<Artifact> classpathResources = new ArrayList<>();
private final Collection<Artifact> translations = new LinkedHashSet<>();
private BuildConfiguration.StrictDepsMode strictJavaDeps =
@@ -966,8 +968,8 @@ public class JavaCompileAction extends AbstractAction {
return this;
}
- public Builder addResources(Collection<Artifact> resources) {
- this.resources.addAll(resources);
+ public Builder addResources(Map<PathFragment, Artifact> resources) {
+ this.resources.putAll(resources);
return this;
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaHelper.java
index 1c84d7342c..8a702e3cc1 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaHelper.java
@@ -19,6 +19,8 @@ import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.shell.ShellUtils;
+import com.google.devtools.build.lib.syntax.Type;
+import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.List;
@@ -101,4 +103,24 @@ public abstract class JavaHelper {
}
return result;
}
+
+ public static PathFragment getJavaResourcePath(
+ JavaSemantics semantics, RuleContext ruleContext, Artifact resource) {
+ PathFragment rootRelativePath = resource.getRootRelativePath();
+ if (!ruleContext.attributes().has("resource_strip_prefix", Type.STRING)
+ || !ruleContext.attributes().isAttributeValueExplicitlySpecified("resource_strip_prefix")) {
+ return semantics.getDefaultJavaResourcePath(rootRelativePath);
+ }
+
+ PathFragment prefix = new PathFragment(
+ ruleContext.attributes().get("resource_strip_prefix", Type.STRING));
+
+ if (!rootRelativePath.startsWith(prefix)) {
+ ruleContext.attributeError("resource_strip_prefix", String.format(
+ "Resource file '%s' is not under the specified prefix to strip", rootRelativePath));
+ return rootRelativePath;
+ }
+
+ return rootRelativePath.relativeTo(prefix);
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java
index a75b414ef1..ba94706a4b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java
@@ -360,12 +360,14 @@ public interface JavaSemantics {
* Takes the path of a Java resource and tries to determine the Java
* root relative path of the resource.
*
+ * <p>This is only used if the Java rule doesn't have a {@code resource_strip_prefix} attribute.
+ *
* @param path the root relative path of the resource.
* @return the Java root relative path of the resource of the root
* relative path of the resource if no Java root relative path can be
* determined.
*/
- PathFragment getJavaResourcePath(PathFragment path);
+ PathFragment getDefaultJavaResourcePath(PathFragment path);
/**
* @return a list of extra arguments to appends to the runfiles support.
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSourceInfoProvider.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSourceInfoProvider.java
index 33b488147a..40794bca92 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSourceInfoProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSourceInfoProvider.java
@@ -22,7 +22,6 @@ import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Collection;
-import java.util.LinkedHashMap;
import java.util.Map;
/**
@@ -117,19 +116,11 @@ public final class JavaSourceInfoProvider implements TransitiveInfoProvider {
*/
public static JavaSourceInfoProvider fromJavaTargetAttributes(
JavaTargetAttributes attributes, JavaSemantics semantics) {
- Map<PathFragment, Artifact> resourcesMap = new LinkedHashMap<>();
- for (Artifact resource : attributes.getResources()) {
- /* The resources are passed in iteration order to the javac command line;
- * javac chooses the last resource with a given path to make it into the jar.
- * So too shall we overwrite the values that have come before and end up with the last of the
- * colliding resources. */
- resourcesMap.put(semantics.getJavaResourcePath(resource.getRootRelativePath()), resource);
- }
return new Builder()
.setSourceFiles(attributes.getSourceFiles())
.setSourceJars(attributes.getSourceJars())
.setJarFiles(attributes.getJarFiles())
- .setResources(ImmutableMap.copyOf(resourcesMap))
+ .setResources(attributes.getResources())
.setProcessorNames(attributes.getProcessorNames())
.setProcessorPath(attributes.getProcessorPath())
.build();
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java
index 1e3cb2e86c..ed4b49dc2a 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java
@@ -16,6 +16,7 @@ package com.google.devtools.build.lib.rules.java;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
@@ -25,11 +26,14 @@ import com.google.devtools.build.lib.collect.IterablesChain;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.cpp.CppFileTypes;
+import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
/**
@@ -71,7 +75,7 @@ public class JavaTargetAttributes {
private final Set<Artifact> processorPath = new LinkedHashSet<>();
private final Set<String> processorNames = new LinkedHashSet<>();
- private final List<Artifact> resources = new ArrayList<>();
+ private final Map<PathFragment, Artifact> resources = new LinkedHashMap<>();
private final List<Artifact> messages = new ArrayList<>();
private final List<Artifact> instrumentationMetadata = new ArrayList<>();
private final List<Artifact> sourceJars = new ArrayList<>();
@@ -108,7 +112,8 @@ public class JavaTargetAttributes {
sourceJars.add(srcArtifact);
} else if (JavaSemantics.PROPERTIES.matches(srcFilename)) {
// output files of the message compiler
- resources.add(srcArtifact);
+ resources.put(
+ semantics.getDefaultJavaResourcePath(srcArtifact.getRootRelativePath()), srcArtifact);
} else if (JavaSemantics.JAVA_SOURCE.matches(srcFilename)) {
sourceFiles.add(srcArtifact);
} else {
@@ -300,15 +305,9 @@ public class JavaTargetAttributes {
return this;
}
- public Builder addResources(Collection<Artifact> resources) {
+ public Builder addResource(PathFragment execPath, Artifact resource) {
Preconditions.checkArgument(!built);
- this.resources.addAll(resources);
- return this;
- }
-
- public Builder addResource(Artifact resource) {
- Preconditions.checkArgument(!built);
- resources.add(resource);
+ this.resources.put(execPath, resource);
return this;
}
@@ -404,7 +403,7 @@ public class JavaTargetAttributes {
private final ImmutableSet<Artifact> processorPath;
private final ImmutableSet<String> processorNames;
- private final ImmutableList<Artifact> resources;
+ private final ImmutableMap<PathFragment, Artifact> resources;
private final ImmutableList<Artifact> messages;
private final ImmutableList<Artifact> sourceJars;
@@ -431,7 +430,7 @@ public class JavaTargetAttributes {
List<Artifact> nativeLibraries,
Set<Artifact> processorPath,
Set<String> processorNames,
- List<Artifact> resources,
+ Map<PathFragment, Artifact> resources,
List<Artifact> messages,
List<Artifact> sourceJars,
List<Artifact> classPathResources,
@@ -450,7 +449,7 @@ public class JavaTargetAttributes {
this.nativeLibraries = ImmutableList.copyOf(nativeLibraries);
this.processorPath = ImmutableSet.copyOf(processorPath);
this.processorNames = ImmutableSet.copyOf(processorNames);
- this.resources = ImmutableList.copyOf(resources);
+ this.resources = ImmutableMap.copyOf(resources);
this.messages = ImmutableList.copyOf(messages);
this.sourceJars = ImmutableList.copyOf(sourceJars);
this.classPathResources = ImmutableList.copyOf(classPathResources);
@@ -474,7 +473,7 @@ public class JavaTargetAttributes {
return sourceJars;
}
- public Collection<Artifact> getResources() {
+ public Map<PathFragment, Artifact> getResources() {
return resources;
}
@@ -577,7 +576,7 @@ public class JavaTargetAttributes {
if (includeClasspath) {
inputs.add(ImmutableList.copyOf(getRuntimeClassPathForArchive()));
}
- inputs.add(getResources());
+ inputs.add(ImmutableList.copyOf(getResources().values()));
inputs.add(getClassPathResources());
return inputs.build();
}
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
index e74f1ca751..529053b42e 100644
--- 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
@@ -31,14 +31,14 @@ public class BazelJavaSemanticsTest {
public void testFindingResources() {
BazelJavaSemantics semantics = BazelJavaSemantics.INSTANCE;
assertEquals(PathFragment.EMPTY_FRAGMENT,
- semantics.getJavaResourcePath(new PathFragment("x/y/src/main/resources")));
+ semantics.getDefaultJavaResourcePath(new PathFragment("x/y/src/main/resources")));
assertEquals(new PathFragment("foo"),
- semantics.getJavaResourcePath(new PathFragment("x/y/src/main/resources/foo")));
+ semantics.getDefaultJavaResourcePath(new PathFragment("x/y/src/main/resources/foo")));
assertEquals(new PathFragment("foo"),
- semantics.getJavaResourcePath(new PathFragment("java/x/y/src/main/resources/foo")));
+ semantics.getDefaultJavaResourcePath(new PathFragment("java/x/y/src/main/resources/foo")));
assertEquals(new PathFragment("foo/java/bar"),
- semantics.getJavaResourcePath(new PathFragment("java/foo/java/bar")));
+ semantics.getDefaultJavaResourcePath(new PathFragment("java/foo/java/bar")));
assertEquals(new PathFragment("foo/java/bar"),
- semantics.getJavaResourcePath(new PathFragment("javatests/foo/java/bar")));
+ semantics.getDefaultJavaResourcePath(new PathFragment("javatests/foo/java/bar")));
}
}