Is it possible to call Ant or NSIS scripts from Java code?

后端 未结 4 1353
旧时难觅i
旧时难觅i 2020-11-29 09:15

Is it possible to call Ant or NSIS scripts programmatically from Java code at runtime? If so, how?

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 09:24

    You can call ant scripts from Java code.

    See this article (scroll down to the "Running Ant via Java" section) and this article:

       File buildFile = new File("build.xml");
       Project p = new Project();
       p.setUserProperty("ant.file", buildFile.getAbsolutePath());
       p.init();
       ProjectHelper helper = ProjectHelper.getProjectHelper();
       p.addReference("ant.projectHelper", helper);
       helper.parse(p, buildFile);
       p.executeTarget(p.getDefaultTarget());
    

    Update

    I tried with the following ant file , it did not "tell" anything (no console output), but it worked: the file was indeed moved

       
          
            
          
       
    

    And when I try it again (when there is no test.txt to move(it is already moved)), I got an java.io.FileNotFoundException.

    I think this is what you would expect when you run something from Java.

    If you want the console output of the ant tasks, you might want to add a Logger as a build listener.

    From @Perception's answer below.

       DefaultLogger consoleLogger = new DefaultLogger();
       consoleLogger.setErrorPrintStream(System.err);
       consoleLogger.setOutputPrintStream(System.out);
       consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
       p.addBuildListener(consoleLogger);
    

提交回复
热议问题