19
19
import java .util .ArrayList ;
20
20
import java .util .Comparator ;
21
21
import java .util .List ;
22
+ import java .util .Map ;
22
23
import java .util .Scanner ;
23
24
import java .util .stream .Stream ;
24
25
25
26
@ Accessors (chain = true )
26
27
public class Exec implements IComponentDeployer {
27
28
29
+ @ Setter
28
30
private String workingDirectory ;
29
-
30
31
@ Setter
31
32
private String executable ;
32
33
@ Setter
33
34
private String executablePrefix ;
35
+ @ Setter
36
+ private Map <String , String > env ;
34
37
@ Getter
38
+ @ Setter
35
39
private String [] args ;
36
- private Event event = Event .ON_DEPLOY ;
37
40
@ Setter
38
41
private boolean ignoreExitValue ;
39
42
private int needRebootExitValue ;
43
+ private Event event = Event .ON_DEPLOY ;
40
44
41
45
public Exec onDeploy () {
42
46
event = Event .ON_DEPLOY ;
@@ -62,15 +66,12 @@ public Exec onStop() {
62
66
private String deploymentPath ;
63
67
private File defaultDeployExecutable ;
64
68
65
- public Exec setWorkingDirectory (String workingDirectory ) {
66
- this .workingDirectory = workingDirectory ;
67
- return this ;
68
- }
69
-
70
69
@ 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 ) {
72
71
ProcessBuilder builder = new ProcessBuilder (command )
73
72
.directory (directory );
73
+ if (env != null && !env .isEmpty ())
74
+ builder .environment ().putAll (env );
74
75
if (!directory .exists ())
75
76
directory .mkdirs ();
76
77
Process p = builder .start ();
@@ -88,12 +89,7 @@ private static void realInheritIO(final InputStream src, final PrintStream dest)
88
89
}).start ();
89
90
}
90
91
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 ) {
97
93
List <String > command = new ArrayList <>();
98
94
command .add ("cmd" );
99
95
command .add ("/c" );
@@ -112,7 +108,7 @@ private DeploymentResult executeCommand(String executable, String[] args) {
112
108
113
109
String workingDirectoryName = workingDirectory != null ? workingDirectory : deploymentPath ;
114
110
115
- int exitValue = exec (command , new File (workingDirectoryName ));
111
+ int exitValue = exec (command , env , new File (workingDirectoryName ));
116
112
117
113
if (exitValue == needRebootExitValue )
118
114
return DeploymentResult .NEED_REBOOT ;
@@ -134,7 +130,7 @@ private enum Event {ON_DEPLOY, ON_UNDEPLOY, ON_START, ON_STOP}
134
130
public DeploymentResult deploy () {
135
131
if (event != Event .ON_DEPLOY || executable == null && defaultDeployExecutable == null )
136
132
return DeploymentResult .OK ;
137
- return executeCommand (executable == null ? defaultDeployExecutable .getPath () : executable , args );
133
+ return executeCommand (executable == null ? defaultDeployExecutable .getPath () : executable , args , env );
138
134
}
139
135
140
136
@ Override
@@ -146,7 +142,7 @@ public DeploymentResult undeploy() {
146
142
} catch (Exception e ) {
147
143
return DeploymentResult .FAILED ;
148
144
}
149
- return executeCommand (executable , args );
145
+ return executeCommand (executable , args , env );
150
146
}
151
147
152
148
private String findLatestUnins (String deploymentPath , String executablePrefix ) throws IOException {
@@ -164,14 +160,14 @@ private String findLatestUnins(String deploymentPath, String executablePrefix) t
164
160
public DeploymentResult stop () {
165
161
if (event != Event .ON_STOP || executable == null )
166
162
return DeploymentResult .OK ;
167
- return executeCommand (executable , args );
163
+ return executeCommand (executable , args , env );
168
164
}
169
165
170
166
@ Override
171
167
public DeploymentResult start () {
172
168
if (event != Event .ON_START || executable == null )
173
169
return DeploymentResult .OK ;
174
- return executeCommand (executable , args );
170
+ return executeCommand (executable , args , env );
175
171
}
176
172
177
173
@ Override
0 commit comments