|
12 | 12 | import org.gradle.api.logging.Logging; |
13 | 13 | import org.gradle.api.tasks.JavaExec; |
14 | 14 | import org.gradle.api.tasks.application.CreateStartScripts; |
| 15 | +import org.gradle.util.GradleVersion; |
15 | 16 | import org.javamodularity.moduleplugin.extensions.RunModuleOptions; |
16 | 17 | import org.javamodularity.moduleplugin.internal.TaskOption; |
17 | 18 |
|
|
21 | 22 | import java.nio.file.Path; |
22 | 23 | import java.util.ArrayList; |
23 | 24 | import java.util.List; |
| 25 | +import java.util.regex.Pattern; |
24 | 26 |
|
25 | 27 | public class StartScriptsMutator extends AbstractExecutionMutator { |
26 | 28 | private static final Logger LOGGER = Logging.getLogger(StartScriptsMutator.class); |
@@ -122,8 +124,14 @@ private void removeClasspathArgs(CreateStartScripts startScriptsTask) { |
122 | 124 | Path outputDir = startScriptsTask.getOutputDir().toPath(); |
123 | 125 | Path bashScript = outputDir.resolve(startScriptsTask.getApplicationName()); |
124 | 126 |
|
125 | | - replaceScriptContent(bashScript, "eval set -- \\$DEFAULT_JVM_OPTS \\$JAVA_OPTS \\$(\\S+).*", "eval set -- \\$JAVA_OPTS \\$$1 \\$DEFAULT_JVM_OPTS \\\"\\$APP_ARGS\\\""); |
126 | | - |
| 127 | + if(GradleVersion.current().compareTo(GradleVersion.version("7.2")) < 0) { |
| 128 | + replaceScriptContent(bashScript, "eval set -- \\$DEFAULT_JVM_OPTS \\$JAVA_OPTS \\$(\\S+).*", "eval set -- \\$JAVA_OPTS \\$$1 \\$DEFAULT_JVM_OPTS \\\"\\$APP_ARGS\\\""); |
| 129 | + } else { |
| 130 | + replaceScriptContent(bashScript, "app_path=\\$0", "app_path=\\$0\nAPP_ARGS=`echo \"\\$@\"`"); |
| 131 | + String appOpts = getAppOptsVariableName(bashScript); |
| 132 | + replaceScriptContent(bashScript, "exec \"\\$JAVACMD\" \"\\$@\"", |
| 133 | + "eval set -- \\$JAVA_OPTS " + appOpts + " \\$DEFAULT_JVM_OPTS \\\"\\$APP_ARGS\\\"\nexec \"\\$JAVACMD\" \"\\$@\""); |
| 134 | + } |
127 | 135 |
|
128 | 136 | Path batFile = outputDir.resolve(startScriptsTask.getApplicationName() + ".bat"); |
129 | 137 | replaceScriptContent(batFile, "\"%JAVA_EXE%\" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %(\\S+)%.*", "\"%JAVA_EXE%\" %JAVA_OPTS% %$1% %DEFAULT_JVM_OPTS% %CMD_LINE_ARGS%"); |
@@ -155,6 +163,19 @@ private static String getContent(Path path) { |
155 | 163 | } |
156 | 164 | } |
157 | 165 |
|
| 166 | + private static final Pattern APP_OPTS_VARIABLE_NAME = Pattern.compile("(?s).*\\$DEFAULT_JVM_OPTS \\$JAVA_OPTS \\$(\\w+).*"); |
| 167 | + private static String getAppOptsVariableName(Path path) { |
| 168 | + try { |
| 169 | + var m = APP_OPTS_VARIABLE_NAME.matcher(Files.readString(path)); |
| 170 | + if(m.matches()) { |
| 171 | + return "\\$" + m.group(1); |
| 172 | + } |
| 173 | + } catch (IOException e) { |
| 174 | + throw new GradleException("Couldn't read run script in " + path); |
| 175 | + } |
| 176 | + return ""; |
| 177 | + } |
| 178 | + |
158 | 179 | private static void replaceScriptContent(Path path, String regex, String replacement) { |
159 | 180 | try { |
160 | 181 | String updatedScriptContent = Files.readString(path).replaceAll(regex, replacement); |
|
0 commit comments