grails: pass program arguments to trait injector via classpath JAR to avoid problems with too long command line (IDEA-158581)

This commit is contained in:
nik
2016-07-26 09:49:55 +03:00
parent 319adf8e1f
commit ed6660ce68
3 changed files with 94 additions and 6 deletions
@@ -49,6 +49,7 @@ public class CommandLineWrapper {
}
private static MainPair loadMainClassFromClasspathJar(File jarFile, String[] args) throws Exception {
String[] mainArgs;
final JarInputStream inputStream = new JarInputStream(new FileInputStream(jarFile));
try {
final Manifest manifest = inputStream.getManifest();
@@ -61,6 +62,14 @@ public class CommandLineWrapper {
System.setProperty(optionName, (String)vmOptions.get(optionName));
}
}
String programParameters = manifest.getMainAttributes().getValue("Program-Parameters");
if (programParameters == null) {
mainArgs = new String[args.length - 2];
System.arraycopy(args, 2, mainArgs, 0, mainArgs.length);
}
else {
mainArgs = splitBySpaces(programParameters);
}
}
finally {
if (inputStream != null) {
@@ -69,11 +78,60 @@ public class CommandLineWrapper {
jarFile.deleteOnExit();
}
String[] mainArgs = new String[args.length - 2];
System.arraycopy(args, 2, mainArgs, 0, mainArgs.length);
return new MainPair(Class.forName(args[1]), mainArgs);
}
/**
* The implementation is copied from copied from com.intellij.util.execution.ParametersListUtil.parse and adapted to old Java versions
*/
private static String[] splitBySpaces(String parameterString) {
parameterString = parameterString.trim();
final ArrayList params = new ArrayList();
final StringBuffer token = new StringBuffer(128);
boolean inQuotes = false;
boolean escapedQuote = false;
boolean nonEmpty = false;
for (int i = 0; i < parameterString.length(); i++) {
final char ch = parameterString.charAt(i);
if (ch == '\"') {
if (!escapedQuote) {
inQuotes = !inQuotes;
nonEmpty = true;
continue;
}
escapedQuote = false;
}
else if (Character.isWhitespace(ch)) {
if (!inQuotes) {
if (token.length() > 0 || nonEmpty) {
params.add(token.toString());
token.setLength(0);
nonEmpty = false;
}
continue;
}
}
else if (ch == '\\') {
if (i < parameterString.length() - 1 && parameterString.charAt(i + 1) == '"') {
escapedQuote = true;
continue;
}
}
token.append(ch);
}
if (token.length() > 0 || nonEmpty) {
params.add(token.toString());
}
//noinspection SSBasedInspection
return (String[])params.toArray(new String[params.size()]);
}
private static class MainPair {
private Class mainClass;
private String[] args;