List<String> (@NotNull List<String> jvmArgs, @NotNull List<String> jvmArgsFromIdeSettings) { MultiMap<String, String> argumentsMap = MultiMap.createLinkedSet(); String lastKey = null; for (String jvmArg : ContainerUtil.concat(jvmArgs, jvmArgsFromIdeSettings)) { if (jvmArg.startsWith("-")) { argumentsMap.putValue(jvmArg, ""); lastKey = jvmArg; } else { if (lastKey != null) { argumentsMap.putValue(lastKey, jvmArg); lastKey = null; } else { argumentsMap.putValue(jvmArg, ""); } } } Map<String, String> mergedKeys = new LinkedHashMap<>(); Set<String> argKeySet = new LinkedHashSet<>(argumentsMap.keySet()); for (String argKey : argKeySet) { Collection<String> values = argumentsMap.getModifiable(argKey); if (values.size() == 1 && values.iterator().next().isEmpty()) { Couple<String> couple = splitArg(argKey); mergedKeys.put(couple.first, couple.second); } else { mergedKeys.put(argKey, ""); Map<String, String> mergedArgs = new LinkedHashMap<>(); for (String jvmArg : values) { if (jvmArg.isEmpty()) continue; Couple<String> couple = splitArg(jvmArg); mergedArgs.put(couple.first, couple.second); } values.clear(); mergedArgs.forEach((key, value) -> values.add(key + value)); } } List<String> mergedArgs = new SmartList<>(); mergedKeys.forEach((s1, s2) -> mergedArgs.add(s1 + s2)); argKeySet.stream().filter(argKey -> !mergedArgs.contains(argKey)).forEach(argumentsMap::remove); // remove `--add-opens` options, because same options will be added by gradle producing the option duplicates. // And the daemon will become uncompilable with the CLI invocations. // see https://github.com/gradle/gradle/blob/v5.1.1/subprojects/launcher/src/main/java/org/gradle/launcher/daemon/configuration/DaemonParameters.java#L125 argumentsMap.remove("--add-opens"); List<String> result = new SmartList<>(); argumentsMap.keySet().forEach(key -> argumentsMap.get(key).forEach(val -> { result.add(key); if (StringUtil.isNotEmpty(val)) { result.add(val); } })); return result; }