mirror of
https://github.com/apache/maven-mvnd.git
synced 2026-01-13 07:04:14 +08:00
Display warning in case of environment mismatch #122
This commit is contained in:
@@ -189,7 +189,7 @@ public class DefaultClient implements Client {
|
||||
daemon.dispatch(new Message.BuildRequest(
|
||||
args,
|
||||
layout.userDir().toString(),
|
||||
layout.multiModuleProjectDirectory().toString()));
|
||||
layout.multiModuleProjectDirectory().toString(), System.getenv()));
|
||||
|
||||
while (true) {
|
||||
Message m = daemon.receive();
|
||||
|
||||
@@ -23,7 +23,9 @@ import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.UTFDataFormatException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class Message {
|
||||
|
||||
@@ -37,11 +39,13 @@ public abstract class Message {
|
||||
final List<String> args;
|
||||
final String workingDir;
|
||||
final String projectDir;
|
||||
final Map<String, String> env;
|
||||
|
||||
public BuildRequest(List<String> args, String workingDir, String projectDir) {
|
||||
public BuildRequest(List<String> args, String workingDir, String projectDir, Map<String, String> env) {
|
||||
this.args = args;
|
||||
this.workingDir = workingDir;
|
||||
this.projectDir = projectDir;
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
public List<String> getArgs() {
|
||||
@@ -56,6 +60,10 @@ public abstract class Message {
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
public Map<String, String> getEnv() {
|
||||
return env;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BuildRequest{" +
|
||||
@@ -221,13 +229,15 @@ public abstract class Message {
|
||||
List<String> args = readStringList(input);
|
||||
String workingDir = readUTF(input);
|
||||
String projectDir = readUTF(input);
|
||||
return new BuildRequest(args, workingDir, projectDir);
|
||||
Map<String, String> env = readStringMap(input);
|
||||
return new BuildRequest(args, workingDir, projectDir, env);
|
||||
}
|
||||
|
||||
private void writeBuildRequest(DataOutputStream output, BuildRequest value) throws IOException {
|
||||
writeStringList(output, value.args);
|
||||
writeUTF(output, value.workingDir);
|
||||
writeUTF(output, value.projectDir);
|
||||
writeStringMap(output, value.env);
|
||||
}
|
||||
|
||||
private BuildEvent readBuildEvent(DataInputStream input) throws IOException {
|
||||
@@ -283,6 +293,25 @@ public abstract class Message {
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> readStringMap(DataInputStream input) throws IOException {
|
||||
LinkedHashMap<String, String> m = new LinkedHashMap<>();
|
||||
int nb = input.readInt();
|
||||
for (int i = 0; i < nb; i++) {
|
||||
String k = readUTF(input);
|
||||
String v = readUTF(input);
|
||||
m.put(k, v);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
private void writeStringMap(DataOutputStream output, Map<String, String> value) throws IOException {
|
||||
output.writeInt(value.size());
|
||||
for (Map.Entry<String, String> e : value.entrySet()) {
|
||||
writeUTF(output, e.getKey());
|
||||
writeUTF(output, e.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private static final String INVALID_BYTE = "Invalid byte";
|
||||
private static final int UTF_BUFS_CHAR_CNT = 256;
|
||||
private static final int UTF_BUFS_BYTE_CNT = UTF_BUFS_CHAR_CNT * 3;
|
||||
|
||||
@@ -32,11 +32,15 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.TreeMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.Option;
|
||||
import org.apache.commons.cli.ParseException;
|
||||
@@ -157,7 +161,7 @@ public class DaemonMavenCli {
|
||||
}
|
||||
|
||||
// TODO need to externalize CliRequest
|
||||
public int doMain(CliRequest cliRequest) throws Exception {
|
||||
public int doMain(CliRequest cliRequest, Map<String, String> clientEnv) throws Exception {
|
||||
Properties props = (Properties) System.getProperties().clone();
|
||||
try {
|
||||
initialize(cliRequest);
|
||||
@@ -170,6 +174,7 @@ public class DaemonMavenCli {
|
||||
populateRequest(cliRequest);
|
||||
encryption(cliRequest);
|
||||
repository(cliRequest);
|
||||
environment(clientEnv);
|
||||
return execute(cliRequest);
|
||||
} catch (ExitException e) {
|
||||
return e.exitCode;
|
||||
@@ -472,6 +477,27 @@ public class DaemonMavenCli {
|
||||
}
|
||||
}
|
||||
|
||||
private void environment(Map<String, String> clientEnv) {
|
||||
Map<String, String> requested = new TreeMap<>(clientEnv);
|
||||
Map<String, String> actual = new TreeMap<>(System.getenv());
|
||||
List<String> diffs = Stream.concat(requested.keySet().stream(), actual.keySet().stream())
|
||||
.sorted()
|
||||
.distinct()
|
||||
.filter(s -> !s.startsWith("JAVA_MAIN_CLASS_"))
|
||||
.filter(key -> !Objects.equals(requested.get(key), actual.get(key)))
|
||||
.collect(Collectors.toList());
|
||||
if (!diffs.isEmpty()) {
|
||||
slf4jLogger.warn("Environment mistmach !");
|
||||
slf4jLogger.warn("A few environment mismatches have been detected between the client and the daemon.");
|
||||
diffs.forEach(key -> {
|
||||
String vr = requested.get(key);
|
||||
String va = actual.get(key);
|
||||
slf4jLogger.warn(" {} -> {} instead of {}", key, va, vr);
|
||||
});
|
||||
slf4jLogger.warn("This is usually harmless.");
|
||||
}
|
||||
}
|
||||
|
||||
private int execute(CliRequest cliRequest)
|
||||
throws MavenExecutionRequestPopulationException {
|
||||
commands(cliRequest);
|
||||
|
||||
@@ -423,7 +423,7 @@ public class Server implements AutoCloseable, Runnable {
|
||||
});
|
||||
pumper.start();
|
||||
try {
|
||||
cli.doMain(req);
|
||||
cli.doMain(req, buildRequest.getEnv());
|
||||
LOGGER.info("Build finished, finishing message dispatch");
|
||||
loggingSpy.finish();
|
||||
} catch (Throwable t) {
|
||||
|
||||
Reference in New Issue
Block a user