/**
 * The MIT License (MIT)
 *
 * Copyright (C) 2015 Anton Epple <toni.epple@eppleton.de>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.dukescript.archetype.ko.test;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;

final class MavenRunner extends Verifier {
    private static Logger LOG;

    static void initializeOutput() {
        if (LOG == null) {
            LOG = Logger.getLogger("maven");
            LOG.info("Initializing Maven Runner");
        }
    }
    static {
        initializeOutput();
    }

    public MavenRunner(String basedir) throws VerificationException {
        super(basedir);
    }

    @Override
    public void executeGoals(List<String> goals, Map envVars) throws VerificationException {
        long now = System.currentTimeMillis();
        LOG.log(Level.INFO, "executeGoals {0} in {1}/{2}", new Object[]{goals, getBasedir(), getLogFileName()});
        try {
            super.executeGoals(goals, envVars);
            LOG.log(Level.INFO, "OK for {0}", goals);
        } catch (VerificationException ex) {
            LOG.log(Level.WARNING, "FAIL log file at {0}/{1}", new Object[]{getBasedir(), getLogFileName()});
            throw ex;
        } finally {
            long took = System.currentTimeMillis() - now;
            if (took > 10000) {
                LOG.log(Level.INFO, "Took {0}s", took / 1000);
            }
        }
    }

}

@@ -1222,39 +1222,140 @@
 ava.
-util.List;%0Aimport java.util.Map
+io.File;%0Aimport java.io.IOException;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Timer;%0Aimport java.util.TimerTask
 ;%0Aim
@@ -1503,16 +1503,51 @@
 erifier;
+%0Aimport org.testng.reporters.Files;
 %0A%0Afinal 
@@ -1613,16 +1613,83 @@
 ger LOG;
+%0A    private static final Timer TIMEOUT = new Timer(%22mvn timeout%22);
 %0A%0A    st
@@ -2266,32 +2266,606 @@
 ogFileName()%7D);%0A
+        TimerTask tt = new TimerTask() %7B%0A            @Override%0A            public void run() %7B%0A                File log = new File(getBasedir(), getLogFileName());%0A                String content;%0A                try %7B%0A                    content = Files.readFile(log);%0A                %7D catch (IOException ex) %7B%0A                    content = %22Cannot read %22 + log + %22: %22 + ex.getMessage();%0A                %7D%0A                LOG.log(Level.WARNING, %22Timeout in execution, printing log file%5Cn%7B0%7D%22, content);%0A            %7D%0A        %7D;%0A        TIMEOUT.schedule(tt, 60 * 1000 * 5);%0A
         try %7B%0A  
@@ -3320,32 +3320,57 @@
 ;%0A            %7D%0A
+            tt.cancel();%0A
         %7D%0A    %7D%0A

Print log file for tasks that run more than five minutes