package com.bazaarvoice.maven.plugin.process;

import com.google.common.base.Joiner;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

import java.io.File;

@Mojo (name = "start", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST)
public class ProcessStartMojo extends AbstractProcessMojo {

    @Override
    public void execute()
            throws MojoExecutionException, MojoFailureException {
        if (skip) {
            getLog().info("Skipping " + name + " due to configuration skip=true");
            return;
        }
        for (String arg : arguments) {
            getLog().info("arg: " + arg);
        }
        getLog().info("Full command line: " + Joiner.on(" ").join(arguments));
        try {
            startProcess();
            if (waitForInterrupt) {
                sleepUntilInterrupted();
            }
        } catch (Exception e) {
            getLog().error(e);
        }
    }

    private void startProcess() {
        final ExecProcess exec = new ExecProcess(name);
        if (null != processLogFile) {
            exec.setProcessLogFile(new File(processLogFile));
        }
        getLog().info("Starting process: " + exec.getName());
        exec.execute(processWorkingDirectory(), getLog(), arguments);
        CrossMojoState.addProcess(exec, getPluginContext());
        ProcessHealthCondition.waitSecondsUntilHealthy(healthcheckUrl, waitAfterLaunch);
        getLog().info("Started process: " + exec.getName());
    }

    private File processWorkingDirectory() {
        if (workingDir == null) {
            return ensureDirectory(new File(project.getBuild().getDirectory()));
        }
        return ensureDirectory(new File(project.getBuild().getDirectory(), workingDir));
    }

}

@@ -1788,32 +1788,379 @@
 y()));%0A        %7D
+%0A%0A        // try to check if directory is absolute%0A        // https://github.com/bazaarvoice/maven-process-plugin/issues/11%0A        File potentialWorkingDir = new File(workingDir);%0A        if (potentialWorkingDir.isAbsolute() && potentialWorkingDir.exists() && potentialWorkingDir.isDirectory()) %7B%0A            return potentialWorkingDir;%0A        %7D
 %0A        return 

check if `workingDir` is existing, might be absolute path (#12)