淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

maven job命令行执行外部shell

上一篇 / 下一篇  2012-03-08 11:02:17 / 个人分类:java开发

 目前编译环境是

[maven3@inc-platform-dev-144-9 hudson-2.0.1_win]$ mvn -v

Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800)

Maven home: /home/maven3/apache-maven-3.0.3

Java version: 1.6.0_25, vendor: Sun Microsystems Inc.

Java home: /usr/alibaba/install/jdk1.6.0_25/jre

Default locale: en_US, platform. encoding: ISO-8859-1

 

 

)hudson.maven.agent.Main

 

 

增加callShell函数的2个版本。

 

   //see http://www.devdaily.com/java/java-exec-processbuilder-process-2

   //fixedexeclinuxshell hang

   //多线程版

   privatestaticint callShell_mt(String cmd)throwsIOException,

   InterruptedException {

      List commands =newArrayList();

      System.out

             .print(" ***warning!only support shell!callShell start byliangjz.*** ");

      commands.add("/bin/sh");

      commands.add("-c");

      commands.add(cmd);

      SystemCommandExecutor commandExecutor =newSystemCommandExecutor(commands);

      intshellExitStatus = commandExecutor.executeCommand();

      //stdoutandstderrof the command are returned as StringBuilder objects

      StringBuilder stdout = commandExecutor.getStandardOutputFromCommand();

      System.out.println("The numeric result of the command was: "+ shellExitStatus);

      System.out.println(stdout);

      returnshellExitStatus;

   }

   privatestaticintcallShell(String cmd)throwsIOException,

          InterruptedException{

      List commands =newArrayList();

      System.out

             .print(" ***warning!support shell!may be waiting long time ,please!!callShell start byliangjz.*** ");

      commands.add("/bin/sh");

      commands.add("-c");

      commands.add(cmd);

      ProcessBuilder pb =newProcessBuilder(commands);

      pb.redirectErrorStream(true);// use this to capture messages sent to

                                  //stderr

      Process shell = pb.start();

      InputStream shellIn = shell.getInputStream();// this captures the

                                                // output from the

                                                // command

      intshellExitStatus = shell.waitFor();// wait for the shell to finish

                                         // and get the return code

 

      // at this point you can process the output issued by the command

      // for instance, this reads the output and writes it to System.out:

      intc;

      while((c = shellIn.read()) != -1) {

          System.out.write(c);

      }

      // close the stream

      try{

          shellIn.close();

      }catch(IOException ignoreMe) {

 

      }

      System.out.print(" *** callShell End byliangjz,return "+ shellExitStatus);

      returnshellExitStatus;

   }

 

 

 

 

//launch根据mvn命令行传递入的参数才触发调用外部shell.

 

publicstaticintlaunch(String[] args)throwsNoSuchMethodException,

          IllegalAccessException, NoSuchRealmException,

          InvocationTargetException, ClassNotFoundException {

      // ClassWorld world = ClassWorldAdapter.getInstance( launcher.getWorld()

      // );

 

      ClassWorld world =launcher.getWorld();

 

      Set builtinRealms =newHashSet(world.getRealms());

      booleancallShell =false;

      intcallShellRet=-1;

      try{

          System.out.println("----- hudson.maven.agent.Main.launch start by liangjz----");

          String libs =null;

          String workspace=null;

          String apps=null;

                       String crid="0";

          StringBuilder sb=newStringBuilder();

          sb.append("'");

          for(inti = 0; i < args.length; i++) {

             //System.out.println("arg" + i + "=" +args[i]);

             if(args[i].startsWith("-DlibsOrder")) {

                 String[] tmpArr = args[i].split("=");

                 if(tmpArr.length== 2) {

                       callShell =true;

                    libs = tmpArr[1];

                 }

             }elseif(args[i].startsWith("-Dapps")){

                 String[] tmpArr = args[i].split("=");

                 if(tmpArr.length== 2) {

                    apps = tmpArr[1];

                 }

             }elseif(args[i].startsWith("-DWORKSPACE")){

                 String[] tmpArr = args[i].split("=");

                 if(tmpArr.length

TAG: hudson Hudson 源代码

 

评分:0

我来说两句

Open Toolbar