Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

   <project name="testproject" default="test" basedir=".">
      <target name="test">
        <move file="test.txt" tofile="test2.txt" />
      </target>
   </project>

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...