Java Code Examples for org.codehaus.plexus.util.cli.Commandline#setWorkingDirectory()

The following examples show how to use org.codehaus.plexus.util.cli.Commandline#setWorkingDirectory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhantomJsProcessBuilder.java    From phantomjs-maven-plugin with MIT License 6 votes vote down vote up
private Commandline getCommandLine() throws ExecutionException {
  Commandline commandline = new Commandline(this.phantomJsBinary);

  if (configFile != null && configFile.exists()) {
    commandline.createArg().setValue("--config=" + configFile.getAbsolutePath());
  } else {
    commandline.addArguments(this.getCommandLineOptions(commandLineOptions));
  }
  if (script != null) {
    commandline.createArg().setValue(script);
  }
  if (arguments != null) {
    commandline.addArguments(arguments.toArray(new String[arguments.size()]));
  }
  if (workingDirectory != null) {
    commandline.setWorkingDirectory(workingDirectory);
  }

  return commandline;
}
 
Example 2
Source File: TLibLinker.java    From maven-native with MIT License 5 votes vote down vote up
@Override
protected Commandline createLinkerCommandLine( List<File> objectFiles, LinkerConfiguration config )
    throws NativeBuildException
{
    Commandline cl = new Commandline();

    cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );

    String executable = EXECUTABLE;

    if ( config.getExecutable() != null && config.getExecutable().trim().length() != 0 )
    {
        executable = config.getExecutable();
    }

    cl.createArg().setValue( executable );

    cl.createArg().setValue( "\"" + config.getOutputFile() + "\"" );

    for ( int i = 0; i < config.getStartOptions().length; ++i )
    {
        cl.createArg().setValue( config.getStartOptions()[i] );
    }

    for ( int i = 0; i < objectFiles.size(); ++i )
    {
        File objFile = objectFiles.get( i );

        cl.createArg().setValue( "+\"" + objFile.getPath() + "\"" );
    }

    return cl;

}
 
Example 3
Source File: MSVCManifest.java    From maven-native with MIT License 5 votes vote down vote up
public void run( ManifestConfiguration config )
    throws NativeBuildException
{
    Commandline cl = new Commandline();

    cl.setExecutable( "mt.exe" );
    cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );

    cl.createArg().setValue( "-manifest" );

    int manifestType = 0;

    if ( "EXE".equalsIgnoreCase( FileUtils.getExtension( config.getInputFile().getPath() ) ) )
    {
        manifestType = 1;
    }
    else if ( "DLL".equalsIgnoreCase( FileUtils.getExtension( config.getInputFile().getPath() ) ) )
    {
        manifestType = 2;
    }

    if ( manifestType == 0 )
    {
        throw new NativeBuildException( "Unknown manifest input file type: " + config.getInputFile() );
    }

    cl.createArg().setFile( config.getManifestFile() );
    cl.createArg().setValue( "-outputresource:" + config.getInputFile() + ";" + manifestType );

    EnvUtil.setupCommandlineEnv( cl, config.getEnvFactory() );

    CommandLineUtil.execute( cl, this.getLogger() );
}
 
Example 4
Source File: AbstractCodegenMojo.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Run the JDK version (could be set via the toolchain) and see if we need to configure the JvmArgs
 * accordingly. Once we remove JDK8 support we can just add the additional args by default and remove
 * this method.
 */
private void setJvmForkArgs(String javaExecutablePath) {
    Commandline cmd = new Commandline();
    cmd.getShell().setQuotedArgumentsEnabled(true); // for JVM args
    cmd.setWorkingDirectory(project.getBuild().getDirectory());
    cmd.setExecutable(javaExecutablePath);
    Java9StreamConsumer consumer = new Java9StreamConsumer();
    try {
        cmd.createArg().setValue("-XshowSettings:properties -version");
        CommandLineUtils.executeCommandLine(cmd, null, consumer);
    } catch (Exception e2) {
        e2.printStackTrace();
    }

    if (additionalJvmArgs == null) {
        additionalJvmArgs = "";
    }
    if (consumer.isJava9Plus()) {
        additionalJvmArgs = "--add-exports=jdk.xml.dom/org.w3c.dom.html=ALL-UNNAMED "
                            + "--add-exports=java.xml/com.sun.org.apache.xerces.internal.impl.xs=ALL-UNNAMED "
                            + "--add-opens java.base/java.security=ALL-UNNAMED "
                            + "--add-opens java.base/java.net=ALL-UNNAMED "
                            + "--add-opens java.base/java.lang=ALL-UNNAMED "
                            + "--add-opens java.base/java.util=ALL-UNNAMED "
                            + "--add-opens java.base/java.util.concurrent=ALL-UNNAMED "
                            + additionalJvmArgs;
    }
}
 
Example 5
Source File: Git.java    From opoopress with Apache License 2.0 5 votes vote down vote up
private boolean execute(String command, String... args) throws GitException {
	Commandline cl = new Commandline();
	cl.setExecutable("git");
	cl.createArg().setValue(command);
	cl.setWorkingDirectory(workingDirectory.getAbsolutePath());
	
	//args
	for (int i = 0; i < args.length; i++){
		cl.createArg().setValue(args[i]);
	}
	
	if (log.isInfoEnabled()) {
		log.info("[" + cl.getWorkingDirectory().getAbsolutePath() + "] Executing: " + cl);
	}
	
	int exitCode;
	try {
		exitCode = CommandLineUtils.executeCommandLine(cl, stdout, stderr);
	} catch (CommandLineException e) {
		throw new GitException("Error while executing command.", e);
	}

	if(log.isDebugEnabled()){
		log.debug("Run: " + cl + " / $? = " + exitCode);
	}
	
	return exitCode == 0;
}
 
Example 6
Source File: ArchiveEntryUtils.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void chmod(final File file, final int mode, final Log logger, boolean useJvmChmod) throws ArchiverException {
    if (!Os.isFamily(Os.FAMILY_UNIX)) {
        return;
    }

    final String m = Integer.toOctalString(mode & 0xfff);

    if (useJvmChmod && !jvmFilePermAvailable) {
        logger.info("chmod it's not possible where your current jvm");
        useJvmChmod = false;
    }

    if (useJvmChmod && jvmFilePermAvailable) {
        applyPermissionsWithJvm(file, m, logger);
        return;
    }

    try {
        final Commandline commandline = new Commandline();

        commandline.setWorkingDirectory(file.getParentFile().getAbsolutePath());

        if (logger.isDebugEnabled()) {
            logger.debug(file + ": mode " + Integer.toOctalString(mode) + ", chmod " + m);
        }

        commandline.setExecutable("chmod");

        commandline.createArg().setValue(m);

        final String path = file.getAbsolutePath();

        commandline.createArg().setValue(path);

        final CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();

        final CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();

        final int exitCode = CommandLineUtils.executeCommandLine(commandline, stderr, stdout);

        if (exitCode != 0) {
            logger.warn("-------------------------------");
            logger.warn("Standard error:");
            logger.warn("-------------------------------");
            logger.warn(stderr.getOutput());
            logger.warn("-------------------------------");
            logger.warn("Standard output:");
            logger.warn("-------------------------------");
            logger.warn(stdout.getOutput());
            logger.warn("-------------------------------");

            throw new ArchiverException("chmod exit code was: " + exitCode);
        }
    } catch (final CommandLineException e) {
        throw new ArchiverException("Error while executing chmod.", e);
    }

}
 
Example 7
Source File: JavaProcessExecutor.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
private Commandline buildCommandLine() throws Exception {
    Commandline cli = new Commandline();

    //Disable explicit quoting of arguments
    cli.getShell().setQuotedArgumentsEnabled(false);

    cli.setExecutable(java.getAbsolutePath());

    environment.forEach(cli::addEnvironment);

    cli.setWorkingDirectory(workingDirectory);

    addClasspath(this.argsList);

    argsLine(cli);

    return cli;
}
 
Example 8
Source File: CommandExecutor.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Override
public void executeCommand(String executable, List<String> commands, File workingDirectory,
                           boolean failsOnErrorOutput) throws ExecutionException {
    if (commands == null) {
        commands = new ArrayList<String>();
    }
    stdOut = new StreamConsumerImpl(logger, captureStdOut);
    stdErr = new ErrorStreamConsumer(logger, errorListener, captureStdErr);
    commandline = new Commandline();
    if (customShell != null) {
        commandline.setShell(customShell);
    }
    commandline.setExecutable(executable);

    // Add the environment variables as needed
    if (environment != null) {
        for (Map.Entry<String, String> entry : environment.entrySet()) {
            commandline.addEnvironment(entry.getKey(), entry.getValue());
        }
    }

    commandline.addArguments(commands.toArray(new String[commands.size()]));
    if (workingDirectory != null && workingDirectory.exists()) {
        commandline.setWorkingDirectory(workingDirectory.getAbsolutePath());
    }
    try {
        logger.debug("ANDROID-040-000: Executing command: Commandline = " + commandline);
        result = CommandLineUtils.executeCommandLine(commandline, stdOut, stdErr);
        if (logger != null) {
            logger.debug("ANDROID-040-000: Executed command: Commandline = " + commandline + ", Result = "
                    + result);
        } else {
            System.out.println("ANDROID-040-000: Executed command: Commandline = " + commandline
                    + ", Result = " + result);
        }
        if (failsOnErrorOutput && stdErr.hasError() || result != 0) {
            throw new ExecutionException("ANDROID-040-001: Could not execute: Command = "
                    + commandline.toString() + ", Result = " + result);
        }
    } catch (CommandLineException e) {
        throw new ExecutionException("ANDROID-040-002: Could not execute: Command = "
                + commandline.toString() + ", Error message = " + e.getMessage());
    }
    setPid(commandline.getPid());
}
 
Example 9
Source File: JavaProcessExecutor.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public Commandline buildCommandLine() throws Exception {
    Commandline cli = new Commandline();

    //Disable explicit quoting of arguments
    cli.getShell().setQuotedArgumentsEnabled(false);

    cli.setExecutable(javaPath.toString());

    cli.setWorkingDirectory(workingDirectory);

    addClasspath(this.argsList);

    argsLine(cli);

    return cli;
}
 
Example 10
Source File: JavahExecutable.java    From maven-native with MIT License 4 votes vote down vote up
protected Commandline createJavahCommand( JavahConfiguration config )
    throws NativeBuildException
{
    this.validateConfiguration( config );

    Commandline cl = new Commandline();

    if ( config.getWorkingDirectory() != null )
    {
        cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );
    }

    cl.setExecutable( this.getJavaHExecutable( config ) );

    if ( config.getFileName() != null && config.getFileName().length() > 0 )
    {
        File outputFile = new File( config.getOutputDirectory(), config.getFileName() );
        cl.createArg().setValue( "-o" );
        cl.createArg().setFile( outputFile );
    }
    else
    {
        if ( config.getOutputDirectory() != null )
        {
            cl.createArg().setValue( "-d" );
            cl.createArg().setFile( config.getOutputDirectory() );
        }
    }

    String[] classPaths = config.getClassPaths();

    StringBuffer classPathBuffer = new StringBuffer();

    for ( int i = 0; i < classPaths.length; ++i )
    {
        classPathBuffer.append( classPaths[i] );
        if ( i != classPaths.length - 1 )
        {
            classPathBuffer.append( File.pathSeparatorChar );
        }
    }

    if ( config.getUseEnvClasspath() )
    {
        cl.addEnvironment( "CLASSPATH", classPathBuffer.toString() );
    }
    else
    {
        cl.createArg().setValue( "-classpath" );

        cl.createArg().setValue( classPathBuffer.toString() );
    }

    if ( config.getVerbose() )
    {
        cl.createArg().setValue( "-verbose" );
    }

    cl.addArguments( config.getClassNames() );

    return cl;
}
 
Example 11
Source File: MSVCMessageCompiler.java    From maven-native with MIT License 4 votes vote down vote up
protected Commandline getCommandLine( MessageCompilerConfiguration config, File source )
    throws NativeBuildException
{

    Commandline cl = new Commandline();

    EnvUtil.setupCommandlineEnv( cl, config.getEnvFactory() );

    if ( config.getWorkingDirectory() != null )
    {
        cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );
    }

    if ( config.getExecutable() == null || config.getExecutable().trim().length() == 0 )
    {
        config.setExecutable( "mc.exe" );
    }
    cl.setExecutable( config.getExecutable().trim() );

    cl.addArguments( config.getOptions() );

    if ( config.getOutputDirectory() != null && config.getOutputDirectory().getPath().trim().length() != 0 )
    {
        cl.createArg().setValue( "-r" );
        cl.createArg().setValue( config.getOutputDirectory().getPath() );

        cl.createArg().setValue( "-h" );
        cl.createArg().setValue( config.getOutputDirectory().getPath() );

    }

    if ( config.getDebugOutputDirectory() != null
            && config.getDebugOutputDirectory().getPath().trim().length() != 0 )
    {
        cl.createArg().setValue( "-x" );
        cl.createArg().setValue( config.getDebugOutputDirectory().getPath() );
    }

    cl.createArg().setValue( source.getPath() );

    return cl;
}
 
Example 12
Source File: ArchiveLinker.java    From maven-native with MIT License 4 votes vote down vote up
@Override
protected Commandline createLinkerCommandLine( List<File> objectFiles, LinkerConfiguration config )
{
    Commandline cl = new Commandline();

    cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );

    String executable = EXECUTABLE;

    if ( !StringUtils.isBlank( config.getExecutable() ) )
    {
        executable = config.getExecutable();
    }

    cl.setExecutable( executable );

    for ( int i = 0; i < config.getStartOptions().length; ++i )
    {
        cl.createArg().setValue( config.getStartOptions()[i] );
    }

    // the next 2 are for completeness, the start options should be good enough
    for ( int i = 0; i < config.getMiddleOptions().length; ++i )
    {
        cl.createArg().setValue( config.getMiddleOptions()[i] );
    }

    for ( int i = 0; i < config.getEndOptions().length; ++i )
    {
        cl.createArg().setValue( config.getEndOptions()[i] );
    }

    cl.createArg().setFile( config.getOutputFile() );

    for ( int i = 0; i < objectFiles.size(); ++i )
    {
        File objFile = objectFiles.get( i );

        cl.createArg().setValue( objFile.getPath() );
    }

    return cl;

}
 
Example 13
Source File: AbstractGccCompiler.java    From maven-native with MIT License 3 votes vote down vote up
/**
 * Setup Compiler Command line
 */
protected Commandline getCommandLine( File srcFile, File destFile, CompilerConfiguration config )
    throws NativeBuildException
{

    if ( config.getExecutable() == null )
    {
        config.setExecutable( "gcc" );
    }

    Commandline cl = new Commandline();

    cl.setExecutable( config.getExecutable() );

    if ( config.getWorkingDirectory() != null )
    {
        cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );
    }

    this.setStartOptions( cl, config );

    this.setIncludePaths( cl, config.getIncludePaths() );

    this.setIncludePaths( cl, config.getSystemIncludePaths() );

    this.setMiddleOptions( cl, config );

    this.setOutputArgs( cl, destFile );

    this.setSourceArgs( cl, srcFile );

    this.setEndOptions( cl, config );

    return cl;
}
 
Example 14
Source File: AbstractCCompiler.java    From maven-native with MIT License 3 votes vote down vote up
/**
 * Setup Compiler Command line
 */
protected Commandline getCommandLine( File srcFile, File destFile, CompilerConfiguration config )
    throws NativeBuildException
{

    if ( config.getExecutable() == null )
    {
        config.setExecutable( "gcc" );
    }

    Commandline cl = new Commandline();

    cl.setExecutable( config.getExecutable() );

    if ( config.getWorkingDirectory() != null )
    {
        cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );
    }

    this.setStartOptions( cl, config );

    this.setIncludePaths( cl, config.getIncludePaths() );

    this.setIncludePaths( cl, config.getSystemIncludePaths() );

    this.setMiddleOptions( cl, config );

    this.setOutputArgs( cl, destFile );

    this.setSourceArgs( cl, srcFile );

    this.setEndOptions( cl, config );

    return cl;
}