private FixedStringSearchInterpolator createEnvInterpolator() { PrefixedPropertiesValueSource envProps = new PrefixedPropertiesValueSource(Collections.singletonList("env."), CommandLineUtils.getSystemEnvVars(false), true ); return FixedStringSearchInterpolator.create( envProps ); } }
/** * @param cl The command line {@link Commandline} * @param systemOut {@link StreamConsumer} * @param systemErr {@link StreamConsumer} * @return return code. * @throws CommandLineException in case of a problem. */ public static int executeCommandLine( @Nonnull Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr ) throws CommandLineException { return executeCommandLine( cl, null, systemOut, systemErr, 0 ); }
public static void launchSubversion( String line, String basedir ) throws VerificationException { try { Commandline cli = new Commandline( line ); cli.setWorkingDirectory( basedir ); Writer logWriter = new FileWriter( new File( basedir, LOG_FILENAME ) ); StreamConsumer out = new WriterStreamConsumer( logWriter ); StreamConsumer err = new WriterStreamConsumer( logWriter ); System.out.println( "Command: " + CommandLineUtils.toString( cli.getCommandline() ) ); int ret = CommandLineUtils.executeCommandLine( cli, out, err ); logWriter.close(); if ( ret > 0 ) { System.err.println( "Exit code: " + ret ); throw new VerificationException(); } } catch ( CommandLineException e ) { throw new VerificationException( e ); } catch ( IOException e ) { throw new VerificationException( e ); } }
/** * {@inheritDoc} */ public void setLine( String line ) { if ( line == null ) { return; } try { parts = CommandLineUtils.translateCommandline( line ); } catch ( Exception e ) { System.err.println( "Error translating Commandline." ); } }
/** * Immediately forks a process, returns a callable that will block until process is complete. * * @param cl The command line to execute * @param systemIn The input to read from, must be thread safe * @param systemOut A consumer that receives output, must be thread safe * @param systemErr A consumer that receives system error stream output, must be thread safe * @param timeoutInSeconds Positive integer to specify timeout, zero and negative integers for no timeout. * @param runAfterProcessTermination Optional callback to run after the process terminated or the the timeout was * @return A CommandLineCallable that provides the process return value, see {@link Process#exitValue()}. "call" * must be called on this to be sure the forked process has terminated, no guarantees is made about * any internal state before after the completion of the call statements * @throws CommandLineException or CommandLineTimeOutException if time out occurs */ public static CommandLineCallable executeCommandLineAsCallable( @Nonnull final Commandline cl, @Nullable final InputStream systemIn, final StreamConsumer systemOut, final StreamConsumer systemErr, final int timeoutInSeconds, @Nullable final Runnable runAfterProcessTermination ) throws CommandLineException { return executeCommandLineAsCallable( cl, systemIn, systemOut, systemErr, timeoutInSeconds, runAfterProcessTermination, null ); }
/** * Return the shell environment variables. If <code>caseSensitive == true</code>, then envar * keys will all be upper-case. * * @param caseSensitive Whether environment variable keys should be treated case-sensitively. * @return Properties object of (possibly modified) envar keys mapped to their values. * @see System#getenv() System.getenv() API, new in JDK 5.0, to get the same result * <b>since 2.0.2 System#getenv() will be used if available in the current running jvm.</b> */ public static Properties getSystemEnvVars( boolean caseSensitive ) { Map<String, String> envs = System.getenv(); return ensureCaseSensitivity( envs, caseSensitive ); }
/** * @param cl The command line to execute * @param systemIn The input to read from, must be thread safe * @param systemOut A consumer that receives output, must be thread safe * @param systemErr A consumer that receives system error stream output, must be thread safe * @param timeoutInSeconds Positive integer to specify timeout, zero and negative integers for no timeout. * @return A return value, see {@link Process#exitValue()} * @throws CommandLineException or CommandLineTimeOutException if time out occurs */ public static int executeCommandLine( @Nonnull Commandline cl, InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds ) throws CommandLineException { return executeCommandLine( cl, systemIn, systemOut, systemErr, timeoutInSeconds, null ); }
/** * Create a new command line object. * Shell is autodetected from operating system * * @param toProcess The command to process */ public Commandline( String toProcess ) { setDefaultShell(); String[] tmp = new String[0]; try { tmp = CommandLineUtils.translateCommandline( toProcess ); } catch ( Exception e ) { System.err.println( "Error translating Commandline." ); } if ( ( tmp != null ) && ( tmp.length > 0 ) ) { setExecutable( tmp[0] ); for ( int i = 1; i < tmp.length; i++ ) { createArg().setValue( tmp[i] ); } } }
/** * @param cl The command line to execute * @param systemIn The input to read from, must be thread safe * @param systemOut A consumer that receives output, must be thread safe * @param systemErr A consumer that receives system error stream output, must be thread safe * @param timeoutInSeconds Positive integer to specify timeout, zero and negative integers for no timeout. * @param runAfterProcessTermination Optional callback to run after the process terminated or the the timeout was * exceeded, but before waiting on the stream feeder and pumpers to finish. * @param streamCharset Charset to use for reading streams * @return A return value, see {@link Process#exitValue()} * @throws CommandLineException or CommandLineTimeOutException if time out occurs */ public static int executeCommandLine( @Nonnull Commandline cl, InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, @Nullable Runnable runAfterProcessTermination, @Nullable final Charset streamCharset ) throws CommandLineException { final CommandLineCallable future = executeCommandLineAsCallable( cl, systemIn, systemOut, systemErr, timeoutInSeconds, runAfterProcessTermination, streamCharset ); return future.call(); }
/** * Gets the shell environment variables for this process. Note that the returned mapping from variable names to * values will always be case-sensitive regardless of the platform, i.e. <code>getSystemEnvVars().get("path")</code> * and <code>getSystemEnvVars().get("PATH")</code> will in general return different values. However, on platforms * with case-insensitive environment variables like Windows, all variable names will be normalized to upper case. * * @return The shell environment variables, can be empty but never <code>null</code>. * @see System#getenv() System.getenv() API, new in JDK 5.0, to get the same result * <b>since 2.0.2 System#getenv() will be used if available in the current running jvm.</b> */ public static Properties getSystemEnvVars() { return getSystemEnvVars( !Os.isFamily( Os.FAMILY_WINDOWS ) ); }
public int execute(StreamConsumer outStreamConsumer, StreamConsumer errStreamConsumer) throws CommandLineException { log.debug("Executing command line: " + commandline); return CommandLineUtils.executeCommandLine(commandline, outStreamConsumer, errStreamConsumer); }
args.addAll( Arrays.asList( CommandLineUtils.translateCommandline( resolvedArg ) ) );
private void findDefaultMavenHome() throws VerificationException { defaultClasspath = System.getProperty( "maven.bootclasspath" ); defaultClassworldConf = System.getProperty( "classworlds.conf" ); defaultMavenHome = System.getProperty( "maven.home" ); if ( defaultMavenHome == null ) { Properties envVars = CommandLineUtils.getSystemEnvVars(); defaultMavenHome = envVars.getProperty( "M2_HOME" ); } if ( defaultMavenHome == null ) { File f = new File( System.getProperty( "user.home" ), "m2" ); if ( new File( f, "bin/mvn" ).isFile() ) { defaultMavenHome = f.getAbsolutePath(); } } }
/** * @param cl The command line {@link Commandline} * @param systemOut {@link StreamConsumer} * @param systemErr {@link StreamConsumer} * @param timeoutInSeconds The timeout. * @return return code. * @throws CommandLineException in case of a problem. */ public static int executeCommandLine( @Nonnull Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds ) throws CommandLineException { return executeCommandLine( cl, null, systemOut, systemErr, timeoutInSeconds ); }
/** * Add system environment variables */ public void addSystemEnvironment() { Properties systemEnvVars = CommandLineUtils.getSystemEnvVars(); for ( Object o : systemEnvVars.keySet() ) { String key = (String) o; if ( !envVars.containsKey( key ) ) { addEnvironment( key, systemEnvVars.getProperty( key ) ); } } }
/** * @param cl The command line {@link Commandline} * @param systemIn {@link StreamConsumer} * @param systemOut {@link StreamConsumer} * @param systemErr {@link StreamConsumer} * @return return code. * @throws CommandLineException in case of a problem. */ public static int executeCommandLine( @Nonnull Commandline cl, InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr ) throws CommandLineException { return executeCommandLine( cl, systemIn, systemOut, systemErr, 0 ); }
private FixedStringSearchInterpolator createEnvInterpolator() { PrefixedPropertiesValueSource envProps = new PrefixedPropertiesValueSource( Collections.singletonList( "env." ), CommandLineUtils.getSystemEnvVars( false ), true ); return FixedStringSearchInterpolator.create( envProps ); }
/** * @param cl The command line to execute * @param systemIn The input to read from, must be thread safe * @param systemOut A consumer that receives output, must be thread safe * @param systemErr A consumer that receives system error stream output, must be thread safe * @param timeoutInSeconds Positive integer to specify timeout, zero and negative integers for no timeout. * @param runAfterProcessTermination Optional callback to run after the process terminated or the the timeout was * exceeded, but before waiting on the stream feeder and pumpers to finish. * @return A return value, see {@link Process#exitValue()} * @throws CommandLineException or CommandLineTimeOutException if time out occurs */ public static int executeCommandLine( @Nonnull Commandline cl, InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, @Nullable Runnable runAfterProcessTermination ) throws CommandLineException { return executeCommandLine( cl, systemIn, systemOut, systemErr, timeoutInSeconds, runAfterProcessTermination, null ); }
return CommandLineUtils.executeCommandLine( cmd, out, err );
/** * @param cli {@link Commandline} * @param request The request. * @return {@link JavaToolRequest} */ protected JavaToolResult executeCommandLine( Commandline cli, Request request ) { if ( getLogger().isDebugEnabled() ) { getLogger().debug( "Executing: " + cli ); } JavaToolResult result = createResult(); result.setCommandline( cli ); InputStream systemIn = createSystemInputStream(); StreamConsumer systemOut = createSystemOutStreamConsumer( request ); StreamConsumer systemErr = createSystemErrorStreamConsumer( request ); try { int resultCode = CommandLineUtils.executeCommandLine( cli, systemIn, systemOut, systemErr ); result.setExitCode( resultCode ); } catch ( CommandLineException e ) { result.setExecutionException( e ); } return result; }