通過java程序調用ant build.xml配置文件中指定的target


一、概述

  通過ant實現項目的自動化部署,jar包生成,替換,tomcat關停、啟動,查看項目日志;

  通過java程序調用已編輯好的ant腳本build.xml配置文件中指定的target;

  文中文件路徑均為作者自定義路徑;讀者可根據自己實際情況命名並做相應修改;只要實現目的即可;

二、環境

  jdk版本:jdk1.8.0_161;

  ant版本:apache-ant-1.10.5;

  maven版本:apache-maven-3.5.2;

  IDE:eclipse Luna Release (4.4.0);

三、環境變量配置

  1、ANT_HOME;

  2、CLASSPATH

  3、JAVA_HOME;

  4、Path;

  5、MAVEN_HOME;

  

四、eclipse配置

  1、Window-->Preferences-->Java-->Installed JREs-->Add

  如下圖所示:

  

  2、添加JRE環境,如下圖配置,

  

  3、注意要將tools.jar包添加進JRE system libraries,(不然在程序調用ant腳本中打jar包的target時會報錯)添加方法如下圖:

  

五、調用ant腳本的java程序

 1 import java.io.File;
 2 
 3 import org.apache.tools.ant.BuildException;
 4 import org.apache.tools.ant.DefaultLogger;
 5 import org.apache.tools.ant.Project;
 6 import org.apache.tools.ant.ProjectHelper;
 7 import org.slf4j.Logger;
 8 import org.slf4j.LoggerFactory;
 9 
10 public class AntDemo {
11     public static void main(String[] args) throws Exception {
12         final Logger logger = LoggerFactory.getLogger(AntDemo.class);
13         String localPath ="D:/devcode/workspace/dataSourceTest/src/main/resources/test-display/build.xml";
14         File buildFile = new File(localPath.toString());
15         Project project = new Project();
       String targetName = "test";
16 try { 17 DefaultLogger consoleLogger = new DefaultLogger(); 18 consoleLogger.setErrorPrintStream(System.err); 19 consoleLogger.setOutputPrintStream(System.out); 20 consoleLogger.setMessageOutputLevel(Project.MSG_INFO); 21 project.addBuildListener(consoleLogger); 22 project.fireBuildStarted(); 23 project.init(); 24 ProjectHelper helper = ProjectHelper.getProjectHelper(); 25 helper.parse(project, buildFile); 26 project.executeTarget(targetName); 27 project.fireBuildFinished(null); 28 } catch (BuildException e) { 29 // 構建拋出異常 30 project.fireBuildFinished(e); 31 logger.error("Ant執行異常," + e.toString()); 32 throw new Exception("Ant執行異常," + e.toString(), e); 33 } 34 } 35 }

六、ant腳本-build.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <project name="dataSource" default="test" basedir="D:/devcode/workspace/dataSource">
 3     <property file="D:/build.properties" />
 4     <target name="test">
 5         <echo message="test echo messsage, basedir=${basedir}" />
 6     </target>
 7 
 8     <!--*****************************export-jar-start***********************************************-->
 9     <target name="compile" depends="delete-jar">
10         <echo message="--生成jar包開始--" />
11         <javac srcdir="${src}" destdir="${dest}">
12             <compilerarg line="-encoding UTF-8 " />
13         </javac>
14         <jar jarfile="${dest}\${jar_name}" basedir="${dest}" />
15         <echo message="--完成jar包完成,本地保存路徑 file:${dest}/${jar_name}--" />
16     </target>
17 
18     <target name="delete-jar">
19         <delete>
20             <fileset dir="${dest}" includes="**/*.jar">
21             </fileset>
22         </delete>
23         <echo message="--清空舊本地jar包完成,路徑 file:${dest}/${jar_name}--" />
24     </target>
25 
26     <!--*****************************export-jar-end***********************************************-->
27 
28 
29     <!--*****************************scp-download-jar-start***********************************************-->
30     <target name="download-jar" description="download" depends="init_backup">
31         <echo message="--目標服務器待替換jar包下載--" />
32         <scp todir="${localtion_file}/${system.name}/${host.name}/${backup.date}/${folder.backup}" file="${username}:${password}@${host.name}:
33                         ${tomcat.home.linux}/${tomcat.name}/lib/${jar.name}.jar" trust="true" />
34         <echo message="--目標服務器待替換jar包下載完成,本地保存路徑
35                     file:${localtion_file}/${system.name}/${host.name}/${backup.date}/${folder.backup}/${jar.name}.jar--" />
36     </target>
37 
38     <target name="init_backup" description="create">
39         <echo message="--本地備份文件夾創建--" />
40         <mkdir dir="${localtion_file}/${system.name}/${host.name}/${backup.date}/${folder.backup}" />
41         <echo message="--本地備份文件夾創建完成--" />
42     </target>
43     <!--*****************************scp-download-jar-end***********************************************-->
44 
45     <!--*****************************tomcat-stop-start***********************************************-->
46         <target name="tomcat-stop" description="sshexec">
47         <echo message="======關停目標服務器...======" />
48         <sshexec host="${host.name}" username="${username}" password="${password}" port="${port}" command="${tomcat.home.linux}/${tomcat.name}/bin/shutdown.sh" trust="true" />
49         <echo message="======關停目標服務器完成======" />
50     </target>
51     <!--*****************************tomcat-stop-end***********************************************-->
52     
53     <target name="tomcat-start" description="sshexec">
54         <echo message="======啟動服務器...======" />
55         <sshexec host="${host.name}" username="${username}" password="${password}" port="22" command="${tomcat.home.linux}/${tomcat.name}/bin/startup.sh" trust="true" />
56         <echo message="======啟動服務器完成======" />
57     </target>
58 
59 </project>

七、測試運行

  

 

七、常見報錯

  因為ant腳本中存在scp標簽,用執行文件上傳,下載;sshexec標簽,用於執行連接服務器並執行Linux命令;

  因此在執行程序過程中,調用target(download-jar)或(tomcat-stop)時,可能會報錯;需要單獨下載jsch-0.1.54.jar;並將其復制粘貼到D:\development\apache-ant-1.10.5\lib路徑下;

  常見報錯一:

  Cause: the class org.apache.tools.ant.taskdefs.optional.ssh.SSHExec was not found.

        This looks like one of Ant's optional components.

  Action: Check that the appropriate optional JAR exists in

            -ANT_HOME\lib

            -the IDE Ant configuration dialogs

 

  Do not panic, this is a common problem.

  The commonest cause is a missing JAR.

This is not a bug; it is a configuration problem

  解決方案:

  在程序代碼上右鍵-->Run As-->Run Configurations...-->classpath--User Entries-->Add External JARs...

  

  全選路徑D:\development\apache-ant-1.10.5\lib 下的jar包;之所以全選是為了保險,避免缺失jar包;

  

   點擊打開即添加成功;注意要事先將jsch-0.1.54.jar包復制到apache-ant-1.10.5\lib路徑下;

  

  再次運行,即正常;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM