飞常年华
Do not go gentle into that good night...

一、概述

  通过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路径下;

  

  再次运行,即正常;

 

posted on 2019-03-18 17:43  飞常年华  阅读(4256)  评论(0编辑  收藏  举报