aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/java/SingleJarActionBuilder.java
diff options
context:
space:
mode:
authorGravatar elenairina <elenairina@google.com>2017-11-24 07:23:55 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-11-24 07:25:22 -0800
commita6ed4c01d374bcf98bf39c1efc12a0f4f20fe313 (patch)
treeb0a6f8ffb7d9efd8b9f1ca3f3224366319f1a3fa /src/main/java/com/google/devtools/build/lib/rules/java/SingleJarActionBuilder.java
parent263c5e07dd2860a866fd361cd29c03afaf3e019f (diff)
Create the output source jar in java_common.compile
and expose transitive source jars to Skylark. Slightly refactor java classes to take in specific host javabase inputs and host java executable for creating the source jar, instead of always relying on fetching them from native java rules specific attributes. Creating the output source jar in java_common.compile makes the behavior more similar to java_library. Exposing the transitive_source_jars to Skylark helps with the Skylark migration from the old 'java' provider to JavaInfo. Progress on #2614. RELNOTES: transitive_source_jars is now exposed on JavaInfo. PiperOrigin-RevId: 176844750
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/java/SingleJarActionBuilder.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/SingleJarActionBuilder.java73
1 files changed, 52 insertions, 21 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/SingleJarActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/java/SingleJarActionBuilder.java
index 61ee42d124..1897453608 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/SingleJarActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/SingleJarActionBuilder.java
@@ -28,7 +28,7 @@ import com.google.devtools.build.lib.analysis.actions.ParamFileInfo;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
-import javax.annotation.Nullable;
+import com.google.devtools.build.lib.vfs.PathFragment;
/**
* Helper class to create singlejar actions - singlejar can merge multiple zip files without
@@ -45,19 +45,22 @@ public final class SingleJarActionBuilder {
"--warn_duplicate_resources");
/** Constructs the base spawn for a singlejar action. */
- private static SpawnAction.Builder singleJarActionBuilder(RuleContext ruleContext) {
- Artifact singleJar = JavaToolchainProvider.from(ruleContext).getSingleJar();
+ private static SpawnAction.Builder singleJarActionBuilder(
+ JavaToolchainProvider provider,
+ NestedSet<Artifact> hostJavabaseInputs,
+ PathFragment hostJavaExecutable) {
+ Artifact singleJar = provider.getSingleJar();;
SpawnAction.Builder builder = new SpawnAction.Builder();
// If singlejar's name ends with .jar, it is Java application, otherwise it is native.
// TODO(asmundak): once https://github.com/bazelbuild/bazel/issues/2241 is fixed (that is,
// the native singlejar is used on windows) remove support for the Java implementation
if (singleJar.getFilename().endsWith(".jar")) {
builder
- .addTransitiveInputs(JavaHelper.getHostJavabaseInputs(ruleContext))
+ .addTransitiveInputs(hostJavabaseInputs)
.setJarExecutable(
- JavaCommon.getHostJavaExecutable(ruleContext),
+ hostJavaExecutable,
singleJar,
- JavaToolchainProvider.from(ruleContext).getJvmOptions())
+ provider.getJvmOptions())
.setExecutionInfo(ExecutionRequirements.WORKER_MODE_ENABLED);
} else {
builder.setExecutable(singleJar);
@@ -68,26 +71,50 @@ public final class SingleJarActionBuilder {
/**
* Creates an Action that packages files into a Jar file.
*
- * @param semantics the current Java semantics, which must be non-{@code null} if {@code
- * resources} is non-empty
- * @param resources the resources to put into the Jar
+ * @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 static void createSourceJarAction(
RuleContext ruleContext,
- @Nullable JavaSemantics semantics,
+ JavaSemantics semantics,
ImmutableCollection<Artifact> resources,
NestedSet<Artifact> resourceJars,
Artifact outputJar) {
+ createSourceJarAction(
+ ruleContext, semantics, resources, resourceJars, outputJar,
+ JavaToolchainProvider.from(ruleContext),
+ JavaHelper.getHostJavabaseInputs(ruleContext),
+ JavaCommon.getHostJavaExecutable(ruleContext));
+ }
+
+ /**
+ * Creates an Action that packages files into a Jar file.
+ *
+ * @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
+ * @param toolchainProvider is used to retrieve jvm options
+ * @param hostJavabaseInputs Artifacts required to invoke java executable in the created action
+ * @param hostJavaExecutable the jar executable of the created action
+ */
+ public static void createSourceJarAction(
+ RuleContext ruleContext,
+ JavaSemantics semantics,
+ ImmutableCollection<Artifact> resources,
+ NestedSet<Artifact> resourceJars,
+ Artifact outputJar,
+ JavaToolchainProvider toolchainProvider,
+ NestedSet<Artifact> hostJavabaseInputs,
+ PathFragment hostJavaExecutable) {
requireNonNull(ruleContext);
requireNonNull(resourceJars);
requireNonNull(outputJar);
if (!resources.isEmpty()) {
requireNonNull(semantics);
}
- SpawnAction.Builder builder =
- singleJarActionBuilder(ruleContext)
+ SpawnAction.Builder builder = singleJarActionBuilder(
+ toolchainProvider, hostJavabaseInputs, hostJavaExecutable)
.addOutput(outputJar)
.addInputs(resources)
.addTransitiveInputs(resourceJars)
@@ -111,15 +138,18 @@ public final class SingleJarActionBuilder {
requireNonNull(jars);
requireNonNull(output);
SpawnAction.Builder builder =
- singleJarActionBuilder(ruleContext)
- .addOutput(output)
- .addInputs(jars)
- .addCommandLine(
- sourceJarCommandLine(
- output, /* semantics= */ null, /* resources= */ ImmutableList.of(), jars),
- ParamFileInfo.builder(ParameterFileType.SHELL_QUOTED).setUseAlways(true).build())
- .setProgressMessage("Building singlejar jar %s", output.prettyPrint())
- .setMnemonic("JavaSingleJar");
+ singleJarActionBuilder(
+ JavaToolchainProvider.from(ruleContext),
+ JavaHelper.getHostJavabaseInputs(ruleContext),
+ JavaCommon.getHostJavaExecutable(ruleContext))
+ .addOutput(output)
+ .addInputs(jars)
+ .addCommandLine(
+ sourceJarCommandLine(
+ output, /* semantics= */ null, /* resources= */ ImmutableList.of(), jars),
+ ParamFileInfo.builder(ParameterFileType.SHELL_QUOTED).setUseAlways(true).build())
+ .setProgressMessage("Building singlejar jar %s", output.prettyPrint())
+ .setMnemonic("JavaSingleJar");
ruleContext.registerAction(builder.build(ruleContext));
}
@@ -146,3 +176,4 @@ public final class SingleJarActionBuilder {
semantics.getDefaultJavaResourcePath(resource.getRootRelativePath()));
}
}
+