Skip to content

Commit 2a80e1b

Browse files
committed
can specify env for Exec
1 parent 1862df3 commit 2a80e1b

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/main/java/org/scm4j/deployer/installers/Exec.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,28 @@
1919
import java.util.ArrayList;
2020
import java.util.Comparator;
2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.Scanner;
2324
import java.util.stream.Stream;
2425

2526
@Accessors(chain = true)
2627
public class Exec implements IComponentDeployer {
2728

29+
@Setter
2830
private String workingDirectory;
29-
3031
@Setter
3132
private String executable;
3233
@Setter
3334
private String executablePrefix;
35+
@Setter
36+
private Map<String, String> env;
3437
@Getter
38+
@Setter
3539
private String[] args;
36-
private Event event = Event.ON_DEPLOY;
3740
@Setter
3841
private boolean ignoreExitValue;
3942
private int needRebootExitValue;
43+
private Event event = Event.ON_DEPLOY;
4044

4145
public Exec onDeploy() {
4246
event = Event.ON_DEPLOY;
@@ -62,15 +66,12 @@ public Exec onStop() {
6266
private String deploymentPath;
6367
private File defaultDeployExecutable;
6468

65-
public Exec setWorkingDirectory(String workingDirectory) {
66-
this.workingDirectory = workingDirectory;
67-
return this;
68-
}
69-
7069
@SneakyThrows
71-
public static int exec(List<String> command, File directory) {
70+
public static int exec(List<String> command, Map<String, String> env, File directory) {
7271
ProcessBuilder builder = new ProcessBuilder(command)
7372
.directory(directory);
73+
if (env != null && !env.isEmpty())
74+
builder.environment().putAll(env);
7475
if (!directory.exists())
7576
directory.mkdirs();
7677
Process p = builder.start();
@@ -88,12 +89,7 @@ private static void realInheritIO(final InputStream src, final PrintStream dest)
8889
}).start();
8990
}
9091

91-
public Exec setArgs(String... args) {
92-
this.args = args;
93-
return this;
94-
}
95-
96-
private DeploymentResult executeCommand(String executable, String[] args) {
92+
private DeploymentResult executeCommand(String executable, String[] args, Map<String, String> env) {
9793
List<String> command = new ArrayList<>();
9894
command.add("cmd");
9995
command.add("/c");
@@ -112,7 +108,7 @@ private DeploymentResult executeCommand(String executable, String[] args) {
112108

113109
String workingDirectoryName = workingDirectory != null ? workingDirectory : deploymentPath;
114110

115-
int exitValue = exec(command, new File(workingDirectoryName));
111+
int exitValue = exec(command, env, new File(workingDirectoryName));
116112

117113
if (exitValue == needRebootExitValue)
118114
return DeploymentResult.NEED_REBOOT;
@@ -134,7 +130,7 @@ private enum Event {ON_DEPLOY, ON_UNDEPLOY, ON_START, ON_STOP}
134130
public DeploymentResult deploy() {
135131
if (event != Event.ON_DEPLOY || executable == null && defaultDeployExecutable == null)
136132
return DeploymentResult.OK;
137-
return executeCommand(executable == null ? defaultDeployExecutable.getPath() : executable, args);
133+
return executeCommand(executable == null ? defaultDeployExecutable.getPath() : executable, args, env);
138134
}
139135

140136
@Override
@@ -146,7 +142,7 @@ public DeploymentResult undeploy() {
146142
} catch (Exception e) {
147143
return DeploymentResult.FAILED;
148144
}
149-
return executeCommand(executable, args);
145+
return executeCommand(executable, args, env);
150146
}
151147

152148
private String findLatestUnins(String deploymentPath, String executablePrefix) throws IOException {
@@ -164,14 +160,14 @@ private String findLatestUnins(String deploymentPath, String executablePrefix) t
164160
public DeploymentResult stop() {
165161
if (event != Event.ON_STOP || executable == null)
166162
return DeploymentResult.OK;
167-
return executeCommand(executable, args);
163+
return executeCommand(executable, args, env);
168164
}
169165

170166
@Override
171167
public DeploymentResult start() {
172168
if (event != Event.ON_START || executable == null)
173169
return DeploymentResult.OK;
174-
return executeCommand(executable, args);
170+
return executeCommand(executable, args, env);
175171
}
176172

177173
@Override

src/main/java/org/scm4j/deployer/installers/ProcrunDeployer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public DeploymentResult stop() {
126126

127127
@SneakyThrows
128128
private void execute(List<String> command) {
129-
int res = Exec.exec(command, new File(deploymentPath));
129+
int res = Exec.exec(command, null, new File(deploymentPath));
130130
if (res != 0)
131131
throw new Exception(String.format("%s: %d", command.toString(), res));
132132
}

0 commit comments

Comments
 (0)