public static String execute()

in tooling/perf-regression/src/main/java/org/apache/camel/quarkus/performance/regression/MvnwCmdHelper.java [39:95]


    public static String execute(Path cqVersionUnderTestFolder, String args) {

        ByteArrayOutputStream stdoutAndStderrMemoryStream = null;
        FileOutputStream stdoutFileStream = null;
        TeeOutputStream teeOutputStream = null;

        try {
            File mvnwFile = cqVersionUnderTestFolder.resolve("mvnw").toFile();
            CommandLine cmd = CommandLine.parse(mvnwFile.getAbsolutePath() + " " + args);

            stdoutAndStderrMemoryStream = new ByteArrayOutputStream();
            File logFile = cqVersionUnderTestFolder.resolve("logs.txt").toFile();
            stdoutFileStream = new FileOutputStream(logFile, true);

            stdoutFileStream.write("\n\n**********************************************************************\n"
                    .getBytes(StandardCharsets.UTF_8));
            stdoutFileStream.write("**********************************************************************\n"
                    .getBytes(StandardCharsets.UTF_8));
            stdoutFileStream.write(("** " + cmd + "\n").getBytes(StandardCharsets.UTF_8));
            stdoutFileStream.write("**********************************************************************\n"
                    .getBytes(StandardCharsets.UTF_8));
            stdoutFileStream.write("**********************************************************************\n"
                    .getBytes(StandardCharsets.UTF_8));

            teeOutputStream = new TeeOutputStream(stdoutAndStderrMemoryStream, stdoutFileStream);
            DefaultExecutor executor = DefaultExecutor.builder().setWorkingDirectory(cqVersionUnderTestFolder.toFile()).get();
            PumpStreamHandler psh = new PumpStreamHandler(teeOutputStream);
            executor.setStreamHandler(psh);

            Map<String, String> environment = EnvironmentUtils.getProcEnvironment();

            String newMavenOpts = "-Duser.language=en -Duser.country=US";
            if (environment.containsKey("MAVEN_OPTS")) {
                String currentMavenOpts = environment.get("MAVEN_OPTS");
                LOGGER.debugf("MAVEN_OPTS is already set up in the main process with value: %s", currentMavenOpts);
                newMavenOpts = currentMavenOpts + " " + newMavenOpts;
            }
            LOGGER.debugf("Setting MAVEN_OPTS in child process with value: %s", newMavenOpts);
            EnvironmentUtils.addVariableToEnvironment(environment, "MAVEN_OPTS=" + newMavenOpts);

            int exitValue = executor.execute(cmd, environment);
            String outAndErr = stdoutAndStderrMemoryStream.toString(StandardCharsets.UTF_8);
            if (exitValue != 0) {
                throw new RuntimeException("The command '" + cmd + "' has returned exitValue " + exitValue
                        + ", process logs below:\n" + outAndErr);
            }

            return outAndErr;
        } catch (IOException ex) {
            throw new RuntimeException("An issue occurred while attempting to execute 'mvnw " + args
                    + "', more logs may be found in " + cqVersionUnderTestFolder + "/logs.txt if exists", ex);
        } finally {
            IOUtils.closeQuietly(stdoutAndStderrMemoryStream);
            IOUtils.closeQuietly(stdoutFileStream);
            IOUtils.closeQuietly(teeOutputStream);
        }
    }