Skip to content

Commit b7c5920

Browse files
committed
Pass environment to refresh AOT maven/gralde command on the client
Signed-off-by: BoykoAlex <alex.boyko@broadcom.com>
1 parent 64cf119 commit b7c5920

File tree

3 files changed

+84
-21
lines changed

3 files changed

+84
-21
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/BuildCommandProvider.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,26 @@
1010
*******************************************************************************/
1111
package org.springframework.ide.vscode.boot.java;
1212

13+
import java.util.LinkedHashMap;
14+
import java.util.Map;
15+
1316
import org.eclipse.lsp4j.Command;
1417
import org.springframework.ide.vscode.commons.java.IJavaProject;
18+
import org.springframework.ide.vscode.commons.protocol.java.Jre;
1519

1620
public interface BuildCommandProvider {
1721

1822
Command executeMavenGoal(IJavaProject project, String goal);
1923

2024
Command executeGradleBuild(IJavaProject project, String command);
21-
25+
26+
static Map<String, String> buildEnv(IJavaProject project) {
27+
Map<String, String> env = new LinkedHashMap<>();
28+
Jre jre = project.getClasspath().getJre();
29+
if (jre != null && jre.installationPath() != null) {
30+
env.put("JAVA_HOME", jre.installationPath());
31+
}
32+
return env;
33+
}
34+
2235
}

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/DefaultBuildCommandProvider.java

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414
import java.nio.file.Files;
1515
import java.nio.file.Path;
1616
import java.nio.file.Paths;
17+
import java.util.ArrayList;
18+
import java.util.Arrays;
19+
import java.util.LinkedHashMap;
1720
import java.util.List;
21+
import java.util.Map;
1822
import java.util.concurrent.CompletableFuture;
1923
import java.util.concurrent.CompletionException;
2024

2125
import org.eclipse.lsp4j.Command;
2226
import org.springframework.ide.vscode.commons.java.IJavaProject;
2327
import org.springframework.ide.vscode.commons.languageserver.util.OS;
2428
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
25-
29+
import com.google.gson.JsonObject;
2630
import com.google.gson.JsonPrimitive;
2731

2832
public class DefaultBuildCommandProvider implements BuildCommandProvider {
@@ -38,9 +42,10 @@ public DefaultBuildCommandProvider(SimpleLanguageServer server) {
3842
server.onCommand(CMD_EXEC_MAVEN_GOAL, params -> {
3943
String pomPath = extractString(params.getArguments().get(0));
4044
String goal = extractString(params.getArguments().get(1));
45+
Map<String, String> env = params.getArguments().size() > 2 ? extractEnv(params.getArguments().get(2)) : null;
4146
return CompletableFuture.runAsync(() -> {
4247
try {
43-
executeMaven(Paths.get(pomPath), goal.trim().split("\\s+")).get();
48+
executeMaven(Paths.get(pomPath), goal.trim().split("\\s+"), env).get();
4449
} catch (Exception e) {
4550
throw new CompletionException(e);
4651
}
@@ -51,9 +56,10 @@ public DefaultBuildCommandProvider(SimpleLanguageServer server) {
5156
server.onCommand(CMD_EXEC_GRADLE_BUILD, params -> {
5257
String gradleBuildPath = extractString(params.getArguments().get(0));
5358
String command = extractString(params.getArguments().get(1));
59+
Map<String, String> env = params.getArguments().size() > 2 ? extractEnv(params.getArguments().get(2)) : null;
5460
return CompletableFuture.runAsync(() -> {
5561
try {
56-
executeGradle(Paths.get(gradleBuildPath), command.trim().split("\\s+")).get();
62+
executeGradle(Paths.get(gradleBuildPath), command.trim().split("\\s+"), env).get();
5763
} catch (Exception e) {
5864
throw new CompletionException(e);
5965
}
@@ -66,7 +72,13 @@ public Command executeMavenGoal(IJavaProject project, String goal) {
6672
Command cmd = new Command();
6773
cmd.setCommand(CMD_EXEC_MAVEN_GOAL);
6874
cmd.setTitle("Execute Maven Goal");
69-
cmd.setArguments(List.of(Paths.get(project.getProjectBuild().getBuildFile()).toFile().toString(), goal));
75+
List<Object> args = new ArrayList<>(List.of(
76+
Paths.get(project.getProjectBuild().getBuildFile()).toFile().toString(), goal));
77+
Map<String, String> env = BuildCommandProvider.buildEnv(project);
78+
if (!env.isEmpty()) {
79+
args.add(env);
80+
}
81+
cmd.setArguments(args);
7082
return cmd;
7183
}
7284

@@ -75,25 +87,44 @@ public Command executeGradleBuild(IJavaProject project, String command) {
7587
Command cmd = new Command();
7688
cmd.setCommand(CMD_EXEC_GRADLE_BUILD);
7789
cmd.setTitle("Execute Gradle Build");
78-
cmd.setArguments(List.of(Paths.get(project.getProjectBuild().getBuildFile()).toFile().toString(), command));
90+
List<Object> args = new ArrayList<>(List.of(
91+
Paths.get(project.getProjectBuild().getBuildFile()).toFile().toString(), command));
92+
Map<String, String> env = BuildCommandProvider.buildEnv(project);
93+
if (env != null && !env.isEmpty()) {
94+
args.add(env);
95+
}
96+
cmd.setArguments(args);
7997
return cmd;
8098
}
8199

82100
private static String extractString(Object o) {
83101
return o instanceof JsonPrimitive ? ((JsonPrimitive) o).getAsString() : o.toString();
84102
}
85103

86-
private CompletableFuture<Void> executeMaven(Path pom, String[] goal) {
104+
private static Map<String, String> extractEnv(Object o) {
105+
Map<String, String> env = new LinkedHashMap<>();
106+
if (o instanceof JsonObject jo) {
107+
jo.entrySet().forEach(e -> env.put(e.getKey(), e.getValue().getAsString()));
108+
}
109+
return env;
110+
}
111+
112+
private CompletableFuture<Void> executeMaven(Path pom, String[] goal, Map<String, String> env) {
87113
synchronized(MAVEN_LOCK) {
88-
String[] cmd = new String[1 + goal.length];
89114
Path projectPath = pom.getParent();
90115
Path mvnw = projectPath.resolve(OS.isWindows() ? "mvnw.cmd" : "mvnw");
91-
cmd[0] = Files.isRegularFile(mvnw) ? mvnw.toFile().toString() : "mvn";
92-
System.arraycopy(goal, 0, cmd, 1, goal.length);
116+
List<String> cmdList = new ArrayList<>();
117+
cmdList.add(Files.isRegularFile(mvnw) ? mvnw.toFile().toString() : "mvn");
118+
cmdList.addAll(Arrays.asList(goal));
93119
try {
94-
return Runtime.getRuntime().exec(cmd, null, projectPath.toFile()).onExit().thenAccept(process -> {
120+
ProcessBuilder pb = new ProcessBuilder(cmdList);
121+
pb.directory(projectPath.toFile());
122+
if (env != null && !env.isEmpty()) {
123+
pb.environment().putAll(env);
124+
}
125+
return pb.start().onExit().thenAccept(process -> {
95126
if (process.exitValue() != 0) {
96-
throw new CompletionException("Failed to execute Maven goal", new IllegalStateException("Errors running maven command: %s".formatted(String.join(" ", cmd))));
127+
throw new CompletionException("Failed to execute Maven goal", new IllegalStateException("Errors running maven command: %s".formatted(String.join(" ", cmdList))));
97128
}
98129
});
99130
} catch (IOException e) {
@@ -102,16 +133,21 @@ private CompletableFuture<Void> executeMaven(Path pom, String[] goal) {
102133
}
103134
}
104135

105-
private CompletableFuture<Void> executeGradle(Path gradleBuildPath, String[] command) {
106-
String[] cmd = new String[1 + command.length];
136+
private CompletableFuture<Void> executeGradle(Path gradleBuildPath, String[] command, Map<String, String> env) {
107137
Path projectPath = gradleBuildPath.getParent();
108-
Path mvnw = projectPath.resolve(OS.isWindows() ? "gradlew.cmd" : "gradlew");
109-
cmd[0] = Files.isRegularFile(mvnw) ? mvnw.toFile().toString() : "gradle";
110-
System.arraycopy(command, 0, cmd, 1, command.length);
138+
Path gradlew = projectPath.resolve(OS.isWindows() ? "gradlew.cmd" : "gradlew");
139+
List<String> cmdList = new ArrayList<>();
140+
cmdList.add(Files.isRegularFile(gradlew) ? gradlew.toFile().toString() : "gradle");
141+
cmdList.addAll(Arrays.asList(command));
111142
try {
112-
return Runtime.getRuntime().exec(cmd, null, projectPath.toFile()).onExit().thenAccept(process -> {
143+
ProcessBuilder pb = new ProcessBuilder(cmdList);
144+
pb.directory(projectPath.toFile());
145+
if (env != null && !env.isEmpty()) {
146+
pb.environment().putAll(env);
147+
}
148+
return pb.start().onExit().thenAccept(process -> {
113149
if (process.exitValue() != 0) {
114-
throw new CompletionException("Failed to execute Gradle build", new IllegalStateException("Errors running gradle command: %s".formatted(String.join(" ", cmd))));
150+
throw new CompletionException("Failed to execute Gradle build", new IllegalStateException("Errors running gradle command: %s".formatted(String.join(" ", cmdList))));
115151
}
116152
});
117153
} catch (IOException e) {

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/VSCodeBuildCommandProvider.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
package org.springframework.ide.vscode.boot.java;
1212

1313
import java.nio.file.Paths;
14+
import java.util.ArrayList;
1415
import java.util.List;
16+
import java.util.Map;
1517

1618
import org.eclipse.lsp4j.Command;
1719
import org.springframework.ide.vscode.commons.java.IJavaProject;
@@ -23,7 +25,13 @@ public Command executeMavenGoal(IJavaProject project, String goal) {
2325
Command cmd = new Command();
2426
cmd.setCommand("maven.goal.custom");
2527
cmd.setTitle("Execute Maven Goal");
26-
cmd.setArguments(List.of(Paths.get(project.getProjectBuild().getBuildFile()).toFile().toString(), goal));
28+
List<Object> args = new ArrayList<>(List.of(
29+
Paths.get(project.getProjectBuild().getBuildFile()).toFile().toString(), goal));
30+
Map<String, String> env = BuildCommandProvider.buildEnv(project);
31+
if (env != null && !env.isEmpty()) {
32+
args.add(env);
33+
}
34+
cmd.setArguments(args);
2735
return cmd;
2836
}
2937

@@ -32,7 +40,13 @@ public Command executeGradleBuild(IJavaProject project, String command) {
3240
Command cmd = new Command();
3341
cmd.setCommand("gradle.runBuild");
3442
cmd.setTitle("Execute Gradle Build");
35-
cmd.setArguments(List.of(Paths.get(project.getProjectBuild().getBuildFile()).toFile().toString(), command));
43+
List<Object> args = new ArrayList<>(List.of(
44+
Paths.get(project.getProjectBuild().getBuildFile()).toFile().toString(), command));
45+
Map<String, String> env = BuildCommandProvider.buildEnv(project);
46+
if (env != null && !env.isEmpty()) {
47+
args.add(env);
48+
}
49+
cmd.setArguments(args);
3650
return cmd;
3751
}
3852

0 commit comments

Comments
 (0)