org.codehaus.plexus.util.cli.Commandline Java Examples

The following examples show how to use org.codehaus.plexus.util.cli.Commandline. 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: GccCompilerTest.java    From maven-native with MIT License 6 votes vote down vote up
public void testMiddleOptions()
    throws Exception
{
    File[] includePaths = { new File( "p1" ), new File( "p2" ) };
    config.setIncludePaths( includePaths );

    String[] startOptions = { "-s1", "-s2" };
    String[] middleOptions = { "-m1", "-m2" };
    config.setStartOptions( startOptions );
    config.setMiddleOptions( middleOptions );

    Commandline cl = compiler.getCommandLine( sourceFile, objectFile, config );

    assertArrayEquals( new String[] { "gcc", "-s1", "-s2", "-Ip1", "-Ip2", "-m1", "-m2", simpleArgv[0],
            simpleArgv[1], simpleArgv[2], simpleArgv[3] }, cl.getCommandline() );
}
 
Example #3
Source File: GccCompilerTest.java    From maven-native with MIT License 6 votes vote down vote up
public void testEndOptions()
    throws Exception
{
    File[] includePaths = { new File( "p1" ), new File( "p2" ) };
    config.setIncludePaths( includePaths );

    String[] startOptions = { "-s1", "-s2" };
    String[] middleOptions = { "-m1", "-m2" };
    String[] endOptions = { "-e1", "-e2" };
    config.setStartOptions( startOptions );
    config.setMiddleOptions( middleOptions );
    config.setEndOptions( endOptions );

    Commandline cl = compiler.getCommandLine( sourceFile, objectFile, config );

    assertArrayEquals( new String[] { "gcc", "-s1", "-s2", "-Ip1", "-Ip2", "-m1", "-m2", simpleArgv[0],
            simpleArgv[1], simpleArgv[2], simpleArgv[3], "-e1", "-e2" }, cl.getCommandline() );
}
 
Example #4
Source File: PhantomJsProcessBuilder.java    From phantomjs-maven-plugin with MIT License 6 votes vote down vote up
public Process start() throws ExecutionException {
  Commandline commandline = getCommandLine();

  String[] shellCommandline = commandline.getShellCommandline();

  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug(PHANTOMJS_COMMAND, Arrays.asList(shellCommandline));
  }

  ProcessBuilder processBuilder = new ProcessBuilder(shellCommandline);

  if (commandline.getWorkingDirectory() != null) {
    processBuilder.directory(commandline.getWorkingDirectory());
  }

  try {
    return processBuilder.start();
  } catch(IOException e) {
    throw new ExecutionException(UNABLE_TO_START,e);
  }
}
 
Example #5
Source File: CanteenExecutableIT.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void stdoutWorks() throws Exception {
    File exe = getExecutable(getPlatformClassifier());

    Commandline commandLine = new Commandline();
    commandLine.setExecutable(exe.getCanonicalPath());
    commandLine.addArguments(new String[] {"1"});

    StringWriter stdOut = new StringWriter();
    StringWriter stdErr = new StringWriter();

    int returnCode = CommandLineUtils.executeCommandLine(commandLine, new WriterStreamConsumer(stdOut), new WriterStreamConsumer(stdErr));

    assertThat(returnCode).withFailMessage("Unexpected return code").isEqualTo(0);
    assertThat(stdOut.toString()).isEqualTo("success\n");
    assertThat(stdErr.toString()).isEmpty();
}
 
Example #6
Source File: CanteenExecutableIT.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void stderrWorks() throws Exception {
    File exe = getExecutable(getPlatformClassifier());

    Commandline commandLine = new Commandline();
    commandLine.setExecutable(exe.getCanonicalPath());
    commandLine.addArguments(new String[] {"0"});

    StringWriter stdOut = new StringWriter();
    StringWriter stdErr = new StringWriter();

    int returnCode = CommandLineUtils.executeCommandLine(commandLine, new WriterStreamConsumer(stdOut), new WriterStreamConsumer(stdErr));

    assertThat(returnCode).withFailMessage("Unexpected return code").isEqualTo(42);
    assertThat(stdOut.toString()).isEmpty();
    assertThat(stdErr.toString()).isEqualTo("failure\n");
}
 
Example #7
Source File: EnvUtil.java    From maven-native with MIT License 6 votes vote down vote up
public static void setupCommandlineEnv( Commandline cl, EnvFactory envFactory )
    throws NativeBuildException
{
    if ( envFactory != null )
    {
        Map<String, String> envs = envFactory.getEnvironmentVariables();

        Iterator<String> iter = envs.keySet().iterator();

        while ( iter.hasNext() )
        {
            String key = iter.next();
            cl.addEnvironment( key, envs.get( key ) );
        }
    }
}
 
Example #8
Source File: AbstractCommunityEnvFactory.java    From maven-native with MIT License 6 votes vote down vote up
protected Map<String, String> executeCommandLine(Commandline command) throws NativeBuildException
{
    EnvStreamConsumer stdout = new EnvStreamConsumer();
    StreamConsumer stderr = new DefaultConsumer();

    try
    {
        CommandLineUtils.executeCommandLine( command, stdout, stderr );
    }
    catch ( CommandLineException e )
    {
        throw new NativeBuildException( "Failed to execute vcvarsall.bat" );
    }

    return stdout.getParsedEnv();
}
 
Example #9
Source File: CCompilerTest.java    From maven-native with MIT License 6 votes vote down vote up
public void testEndOptions()
    throws Exception
{
    File[] includePaths = { new File( "p1" ), new File( "p2" ) };
    config.setIncludePaths( includePaths );

    String[] startOptions = { "-s1", "-s2" };
    String[] middleOptions = { "-m1", "-m2" };
    String[] endOptions = { "-e1", "-e2" };
    config.setStartOptions( startOptions );
    config.setMiddleOptions( middleOptions );
    config.setEndOptions( endOptions );

    Commandline cl = compiler.getCommandLine( sourceFile, objectFile, config );

    assertArrayEquals( new String[] { "gcc", "-s1", "-s2", "-Ip1", "-Ip2", "-m1", "-m2", simpleArgv[0],
            simpleArgv[1], simpleArgv[2], simpleArgv[3], "-e1", "-e2" }, cl.getCommandline() );
}
 
Example #10
Source File: CLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testLinkerResponseFile()
    throws Exception
{
    this.config.setUsingLinkerResponseFile( true );
    this.config.setWorkingDirectory( new File( getBasedir(), "target" ) );
    Commandline cl = this.getCommandline();

    assertTrue( Arrays.asList( cl.getArguments() ).indexOf( "@objectsFile" ) >= 0 );
}
 
Example #11
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 #12
Source File: GccLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testOptions()
    throws Exception
{
    String[] options = { "-o1", "-o2", "-o3" };
    config.setStartOptions( options );

    Commandline cl = this.getCommandline();

    int index = Arrays.asList( cl.getArguments() ).indexOf( "-o1" );
    assertTrue( index >= 0 );
    assertEquals( "-o2", cl.getArguments()[index + 1] );
    assertEquals( "-o3", cl.getArguments()[index + 2] );

}
 
Example #13
Source File: GccLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testRelativeObjectFileList()
    throws Exception
{
    ArrayList<File> objectFiles = new ArrayList<>( 2 );
    objectFiles.add( new File( config.getOutputDirectory(), "file1.o" ) );
    objectFiles.add( new File( config.getOutputDirectory(), "file2.o" ) );

    Commandline cl = this.getCommandline( objectFiles );

    int index = Arrays.asList( cl.getArguments() ).indexOf( "target" + File.separator + "file1.o" );
    assertTrue( index >= 0 );
    assertEquals( "target" + File.separator + "file2.o", cl.getArguments()[index + 1] );

}
 
Example #14
Source File: GccLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testLinkerResponseFile()
    throws Exception
{
    this.config.setUsingLinkerResponseFile( true );
    this.config.setWorkingDirectory( new File( getBasedir(), "target" ) );
    Commandline cl = this.getCommandline();
    assertTrue( cl.toString().indexOf( "@objectsFile" ) != -1 );
}
 
Example #15
Source File: GccLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testObjectFileList()
    throws Exception
{
    Commandline cl = this.getCommandline();

    int index = Arrays.asList( cl.getArguments() ).indexOf( "source1.o" );
    assertTrue( index >= 0 );
    assertEquals( "source2.o", cl.getArguments()[index + 1] );

}
 
Example #16
Source File: GccLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testOverrideLinkerExecutable()
    throws Exception
{
    config.setExecutable( "ld" );

    Commandline cl = this.getCommandline();
    assertEquals( "ld", cl.getLiteralExecutable() );
}
 
Example #17
Source File: GccCompilerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testIncludePaths()
    throws Exception
{
    File[] includePaths = { new File( "p1" ), new File( "p2" ) };

    config.setIncludePaths( includePaths );

    Commandline cl = compiler.getCommandLine( sourceFile, objectFile, config );

    assertArrayEquals(
            new String[] { "gcc", "-Ip1", "-Ip2", simpleArgv[0], simpleArgv[1], simpleArgv[2], simpleArgv[3] },
            cl.getCommandline() );
}
 
Example #18
Source File: TLibLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testCommandLine()
    throws Exception
{
    TLibLinker linker = new TLibLinker();

    LinkerConfiguration config = new LinkerConfiguration();

    config.setWorkingDirectory( new File( "." ) );

    String[] options = { "/C" };
    config.setStartOptions( options );
    config.setOutputFileName( "tlib" );
    config.setOutputFileExtension( "lib" );
    config.setOutputDirectory( new File( "target" ) );

    List<File> objectFiles = new ArrayList<>();
    objectFiles.add( new File( "target" + File.separator + "a.obj" ) );
    objectFiles.add( new File( "target" + File.separator + "b.obj" ) );
    objectFiles.add( new File( "target" + File.separator + "c.obj" ) );

    Commandline cl = linker.createLinkerCommandLine( objectFiles, config );

    assertArrayEquals( new String[] { "tlib", "\"target" + File.separator + "tlib.lib\"", "/C",
            "+\"target" + File.separator + "a.obj\"", "+\"target" + File.separator + "b.obj\"",
            "+\"target" + File.separator + "c.obj\"" }, cl.getArguments() );

}
 
Example #19
Source File: MSVCLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testSimpleLinkerCommand()
    throws Exception
{
    Commandline cl = this.getCommandline();
    assertArrayEquals( new String[] { "link.exe", "/out:" + config.getOutputFile(), "source1.obj", "source2.obj" },
            cl.getCommandline() );
}
 
Example #20
Source File: AbstractCCompiler.java    From maven-native with MIT License 5 votes vote down vote up
private void setOptions( Commandline cl, String[] options )
{
    if ( options != null )
    {
        for ( int i = 0; i < options.length; ++i )
        {
            cl.createArg().setValue( options[i] );
        }
    }
}
 
Example #21
Source File: GccCompilerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testStartOptions()
    throws Exception
{
    String[] startOptions = { "-s1", "-s2" };
    config.setStartOptions( startOptions );

    Commandline cl = compiler.getCommandLine( sourceFile, objectFile, config );

    assertArrayEquals(
            new String[] { "gcc", "-s1", "-s2", simpleArgv[0], simpleArgv[1], simpleArgv[2], simpleArgv[3] },
            cl.getCommandline() );
}
 
Example #22
Source File: CLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testDefaultLinkerExecutable()
    throws Exception
{
    Commandline cl = this.getCommandline();

    assertEquals( "gcc", cl.getLiteralExecutable() );
    assertEquals( basedir, cl.getWorkingDirectory().getPath() );

}
 
Example #23
Source File: AbstractCCompiler.java    From maven-native with MIT License 5 votes vote down vote up
private void setIncludePaths( Commandline cl, File[] includePaths )
{
    if ( includePaths != null )
    {
        for ( int i = 0; i < includePaths.length; ++i )
        {
            cl.createArg().setValue( "-I" + includePaths[i].getPath() );
        }
    }
}
 
Example #24
Source File: AbstractCCompiler.java    From maven-native with MIT License 5 votes vote down vote up
private void setOutputArgs( Commandline cl, File outputFile )
{
    String outputFileOption = this.getOutputFileOption();

    if ( outputFileOption.endsWith( " " ) )
    {
        cl.createArg().setValue( outputFileOption.trim() );
        cl.createArg().setValue( outputFile.getPath() );
    }
    else
    {
        cl.createArg().setValue( outputFileOption + outputFile.getPath() );
    }
}
 
Example #25
Source File: CCompilerClassicTest.java    From maven-native with MIT License 5 votes vote down vote up
/**
 * Simple test, note: -o option has no space
 *
 * @throws Exception
 */
public void testSimpleCompilation()
    throws Exception
{
    CompilerConfiguration config = new CompilerConfiguration();
    CCompilerClassic compiler = new CCompilerClassic();
    Commandline cl = compiler.getCommandLine( new File( "source.c" ), new File( "object.o" ), config );
    assertArrayEquals( new String[] { "gcc", "-oobject.o", "-c", "source.c" }, cl.getCommandline() );
}
 
Example #26
Source File: ScmUtils.java    From gitflow-helper-maven-plugin with Apache License 2.0 5 votes vote down vote up
private static String sha1ForHEAD(ScmLogger logger, ScmFileSet fileSet) throws ScmException {
    Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "rev-parse" );
    cl.createArg().setValue("HEAD");

    RevParseConsumer rpConsumer = new RevParseConsumer(logger);
    execGitCmd(logger, rpConsumer, cl);

    return rpConsumer.getRev();
}
 
Example #27
Source File: CCompilerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testNonDefaultExecutable()
    throws Exception
{
    this.config.setExecutable( "cc" );
    Commandline cl = compiler.getCommandLine( sourceFile, objectFile, config );
    assertArrayEquals( new String[] { "cc", simpleArgv[0], simpleArgv[1], simpleArgv[2], simpleArgv[3] },
            cl.getCommandline() );
}
 
Example #28
Source File: MSVCLinkerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testDefaultLinkerExecutable()
    throws Exception
{
    Commandline cl = this.getCommandline();
    assertEquals( "link.exe", cl.getLiteralExecutable() );
    assertEquals( basedir, cl.getWorkingDirectory().getPath() );
}
 
Example #29
Source File: CCompilerTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testIncludePaths()
    throws Exception
{
    File[] includePaths = { new File( "p1" ), new File( "p2" ) };

    config.setIncludePaths( includePaths );

    Commandline cl = compiler.getCommandLine( sourceFile, objectFile, config );

    assertArrayEquals(
            new String[] { "gcc", "-Ip1", "-Ip2", simpleArgv[0], simpleArgv[1], simpleArgv[2], simpleArgv[3] },
            cl.getCommandline() );
}
 
Example #30
Source File: CreateApplicationBundleMojo.java    From appbundle-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void createApplicationsSymlink() throws MojoExecutionException, CommandLineException {
    Commandline symlink = new Commandline();
    symlink.setExecutable("ln");
    symlink.createArgument().setValue("-s");
    symlink.createArgument().setValue("/Applications");
    symlink.createArgument().setValue(buildDirectory.getAbsolutePath());
    try {
        symlink.execute().waitFor();
    } catch (InterruptedException ex) {
        throw new MojoExecutionException("Error preparing bundle disk image while creating symlink" + diskImageFile, ex);
    }
}