/** @throws MojoExecutionException if anything unexpected happens. */
  public void execute() throws MojoExecutionException {
    if (!workingDirectory.exists()) {
      workingDirectory.mkdirs();
    }

    CommandLine commandLine = new CommandLine(executable);
    Executor exec = new DefaultExecutor();
    exec.setWorkingDirectory(workingDirectory);

    Map env = new HashMap();
    try {
      Map systemEnvVars = EnvironmentUtils.getProcEnvironment();
      env.putAll(systemEnvVars);
    } catch (IOException e) {
      getLog().error("Could not assign default system enviroment variables.", e);
    }
    env.put("NODE_PATH", new File(workingDirectory, "node_modules").getAbsolutePath());
    env.put("XUNIT_FILE", outputFile.getAbsolutePath());

    List commandArguments = new ArrayList();
    commandArguments.add("test");

    String[] args = new String[commandArguments.size()];
    for (int i = 0; i < commandArguments.size(); i++) {
      args[i] = (String) commandArguments.get(i);
    }

    commandLine.addArguments(args, false);

    OutputStream stdout = System.out;
    OutputStream stderr = System.err;

    try {
      outputFile.getParentFile().mkdirs();
      getLog()
          .debug(
              "Executing command line "
                  + commandLine
                  + " in directory "
                  + workingDirectory.getAbsolutePath());
      exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in));

      int resultCode = exec.execute(commandLine, env);

      if (0 != resultCode) {
        throw new MojoExecutionException(
            "Result of " + commandLine + " execution is: '" + resultCode + "'.");
      }
    } catch (ExecuteException e) {
      throw new MojoExecutionException(EXECUTION_FAILED, e);

    } catch (IOException e) {
      throw new MojoExecutionException(EXECUTION_FAILED, e);
    }
  }
示例#2
0
 /**
  * Executes a java command from within
  *
  * @param session
  * @param toolchainManager
  * @param mode
  * @param mainClass
  * @param vmargs
  * @param mainArgs
  * @throws MojoExecutionException
  */
 @SuppressWarnings("rawtypes")
 public static void executeJava(
     MavenSession session,
     ToolchainManager toolchainManager,
     ExecutionMode mode,
     String mainClass,
     String[] vmargs,
     String[] mainArgs)
     throws MojoExecutionException {
   String javaExecutable = getJavaExecutable(session, toolchainManager);
   CommandLine command = getCommandLine(mode, javaExecutable);
   if (vmargs != null) {
     command.addArguments(vmargs, false);
   }
   command.addArgument(mainClass);
   if (mainArgs != null) {
     command.addArguments(mainArgs, false);
   }
   execute(session, command, null, new HashMap());
 }
示例#3
0
  public int execute(String[] args, @Nullable Path workingDir) throws IOException {
    if (!Files.isExecutable(file)) {
      Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
      perms.add(PosixFilePermission.OWNER_READ);
      perms.add(PosixFilePermission.OWNER_EXECUTE);
      Files.setPosixFilePermissions(file, perms);
    }

    ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
    CommandLine cmd = new CommandLine(file.toFile());
    cmd.addArguments(args);
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(watchdog);
    exec.setStreamHandler(createStreamHandler());
    exec.setExitValues(null);
    if (workingDir != null) {
      exec.setWorkingDirectory(workingDir.toFile());
    }
    in.close();
    LOG.info("Executing: {}", cmd.toString());
    return exec.execute(cmd);
  }