public void startApplication()

in plugins/gradle/gradle-plugin/src/main/java/org/wildfly/swarm/plugin/gradle/StartTask.java [58:135]


    public void startApplication() {
        final SwarmExecutor executor;
        if (extension.isUseUberJar()) {
            executor = uberJarExecutor();
        } else if (!getProject().getTasks().withType(War.class).isEmpty()) {
            executor = warExecutor();
        } else if (!getProject().getTasks().withType(Jar.class).isEmpty()) {
            executor = jarExecutor();
        } else {
            throw new GradleException("Unable to determine type of the project, neither war or jar tasks were found.");
        }

        executor.withJVMArguments(extension.getJvmArguments());
        executor.withArguments(extension.getArguments());

        final SwarmProcess process;
        try {

            File processFile;
            try {
                processFile = Files.createTempFile("thorntail-process-file", null).toFile();
                getLogger().info("Thorntail process file: " + processFile.toPath().toString());
            } catch (IOException e) {
                throw new GradleException("Error while creating Thorntail process file");
            }

            process = executor.withDebug(getDebugPort())
                    .withProcessFile(processFile)
                    .withProperties(extension.getProperties())
                    .withProperties(ThorntailUtils.getPropertiesFromFile(extension.getPropertiesFile()))
                    .withStdoutFile(extension.getStdoutFile() != null ? getProject().file(extension.getStdoutFile()).toPath() : null)
                    .withStderrFile(extension.getStderrFile() != null ? getProject().file(extension.getStderrFile()).toPath() : null)
                    .withEnvironment(extension.getEnvironment())
                    .withEnvironment(ThorntailUtils.getPropertiesFromFile(extension.getEnvironmentFile()))
                    .withWorkingDirectory(getProject().getProjectDir().toPath())
                    .withProperty("remote.maven.repo",
                            getProject().getRepositories().withType(MavenArtifactRepository.class).stream()
                                    .map(mavenArtifactRepository -> mavenArtifactRepository.getUrl().toASCIIString())
                                    .collect(Collectors.joining(",")))
                    .execute();
            registerCancellationListeners(process);

            process.awaitReadiness(extension.getStartTimeout(), TimeUnit.SECONDS);

            if (!process.isAlive()) {
                throw new GradleException(
                        MessageFormatter.format(FAILED_TO_START_MESSAGE + "process not ready in {} seconds",
                                extension.getStartTimeout()).getMessage());
            }
            if (process.getError() != null) {
                throw new GradleException(FAILED_TO_START_MESSAGE
                        + (process.getError() != null ? process.getError().getMessage() : "unknown reason"),
                        process.getError());
            }

        } catch (IOException e) {
            throw new GradleException(FAILED_TO_START_MESSAGE + e.getMessage(), e);
        } catch (InterruptedException e) {
            throw new GradleException(FAILED_TO_START_MESSAGE + "interrupted", e);
        }

        if (waitForProcess) {
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                try {
                    process.stop(extension.getStopTimeout(), TimeUnit.SECONDS);
                } catch (InterruptedException ie) {
                    // Do nothing
                }
            } finally {
                process.destroyForcibly();
            }
        } else {
            // save the process into a property so it can be stopped by another task later
            getProject().getExtensions().getExtraProperties().set(PackagePlugin.THORNTAIL_PROCESS_PROPERTY, process);
        }
    }