From 1df308115f92029b6484a52b6452140e2f930cfd Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 29 Jan 2002 16:20:20 +0000 Subject: [PATCH] add environment variable support in for forked VMs. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271000 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 ++ docs/manual/CoreTasks/java.html | 17 +++++++- .../org/apache/tools/ant/taskdefs/Java.java | 41 ++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 9db3402cd..06fce786b 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -135,6 +135,9 @@ Other changes: * A "package" mapper type has been added to allow package directory names replaced with the dotted form. +* you can now specify environment variables in the task + if the fork attribute has been set to true. + Changes from Ant 1.4 to Ant 1.4.1 =========================================== diff --git a/docs/manual/CoreTasks/java.html b/docs/manual/CoreTasks/java.html index a02ea86f3..3c92b03cb 100644 --- a/docs/manual/CoreTasks/java.html +++ b/docs/manual/CoreTasks/java.html @@ -94,6 +94,13 @@ JVM. Name of a file to write the output to. No + + newenvironment + Do not propagate old environment when new + environment variables are specified. Default is "false" + (ignored if fork is disabled). + No +

Parameters specified as nested elements

arg and jvmarg

@@ -111,6 +118,14 @@ variables.

Java's classpath attribute is a PATH like structure and can also be set via a nested classpath element.

+

env

+

It is possible to specify environment variables to pass to the +forked VM via nested env elements. See the description in the +section about exec

+

Please note that the environment of the current Ant process is +not passed to the forked VM if you specify variables using +env.

+

Settings will be ignored if fork is disabled.

Examples

  
        <java classname="test.Main" >
@@ -150,7 +165,7 @@ and with a maximum memory of 128MB. Any non zero return code breaks the build.
 JVM, as it takes different parameters for other JVMs,
 That JVM can be started from <exec> if required.
 
-

Copyright © 2001 Apache Software Foundation. All rights +

Copyright © 2001-2002 Apache Software Foundation. All rights Reserved.

diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java index 19a0f78ea..1f6b65d2f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Java.java +++ b/src/main/org/apache/tools/ant/taskdefs/Java.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2001 The Apache Software Foundation. All rights + * Copyright (c) 2000-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -81,7 +81,9 @@ import java.util.Vector; public class Java extends Task { private CommandlineJava cmdl = new CommandlineJava(); + private Environment env = new Environment(); private boolean fork = false; + private boolean newEnvironment = false; private File dir = null; private File out; private PrintStream outStream = null; @@ -128,6 +130,11 @@ public class Java extends Task { log("Working directory ignored when same JVM is used.", Project.MSG_WARN); } + if (newEnvironment || null != env.getVariables()) { + log("Changes to environment variables are ignored when same JVM is used.", + Project.MSG_WARN); + } + log("Running in same VM " + cmdl.getJavaCommand().toString(), Project.MSG_VERBOSE); try { @@ -268,6 +275,28 @@ public class Java extends Task { cmdl.setVmversion(value); } + /** + * Add a nested env element - an environment variable. + * + *

Will be ignored if we are not forking a new VM. + * + * @since 1.32, Ant 1.5 + */ + public void addEnv(Environment.Variable var) { + env.addVariable(var); + } + + /** + * Use a completely new environment + * + *

Will be ignored if we are not forking a new VM. + * + * @since 1.32, Ant 1.5 + */ + public void setNewenvironment(boolean newenv) { + newEnvironment = newenv; + } + protected void handleOutput(String line) { if (outStream != null) { outStream.println(line); @@ -340,6 +369,16 @@ public class Java extends Task { exe.setWorkingDirectory(dir); + String[] environment = env.getVariables(); + if (environment != null) { + for (int i=0; i