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

时间:2023-03-09 22:19:00
通过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;

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

四、eclipse配置

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

  如下图所示:

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

  2、添加JRE环境,如下图配置,

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

  3、注意要将tools.jar包添加进JRE system libraries,(不然在程序调用ant脚本中打jar包的target时会报错)添加方法如下图:

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

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

五、调用ant脚本的java程序

 import java.io.File;

 import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class AntDemo {
public static void main(String[] args) throws Exception {
final Logger logger = LoggerFactory.getLogger(AntDemo.class);
String localPath ="D:/devcode/workspace/dataSourceTest/src/main/resources/test-display/build.xml";
File buildFile = new File(localPath.toString());
Project project = new Project();
       String targetName = "test";
try {
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
project.addBuildListener(consoleLogger);
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
helper.parse(project, buildFile);
project.executeTarget(targetName);
project.fireBuildFinished(null);
} catch (BuildException e) {
// 构建抛出异常
project.fireBuildFinished(e);
logger.error("Ant执行异常," + e.toString());
throw new Exception("Ant执行异常," + e.toString(), e);
}
}
}

六、ant脚本-build.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<project name="dataSource" default="test" basedir="D:/devcode/workspace/dataSource">
<property file="D:/build.properties" />
<target name="test">
<echo message="test echo messsage, basedir=${basedir}" />
</target> <!--*****************************export-jar-start***********************************************-->
<target name="compile" depends="delete-jar">
<echo message="--生成jar包开始--" />
<javac srcdir="${src}" destdir="${dest}">
<compilerarg line="-encoding UTF-8 " />
</javac>
<jar jarfile="${dest}\${jar_name}" basedir="${dest}" />
<echo message="--完成jar包完成,本地保存路径 file:${dest}/${jar_name}--" />
</target> <target name="delete-jar">
<delete>
<fileset dir="${dest}" includes="**/*.jar">
</fileset>
</delete>
<echo message="--清空旧本地jar包完成,路径 file:${dest}/${jar_name}--" />
</target> <!--*****************************export-jar-end***********************************************--> <!--*****************************scp-download-jar-start***********************************************-->
<target name="download-jar" description="download" depends="init_backup">
<echo message="--目标服务器待替换jar包下载--" />
<scp todir="${localtion_file}/${system.name}/${host.name}/${backup.date}/${folder.backup}" file="${username}:${password}@${host.name}:
${tomcat.home.linux}/${tomcat.name}/lib/${jar.name}.jar" trust="true" />
<echo message="--目标服务器待替换jar包下载完成,本地保存路径
file:${localtion_file}/${system.name}/${host.name}/${backup.date}/${folder.backup}/${jar.name}.jar--" />
</target> <target name="init_backup" description="create">
<echo message="--本地备份文件夹创建--" />
<mkdir dir="${localtion_file}/${system.name}/${host.name}/${backup.date}/${folder.backup}" />
<echo message="--本地备份文件夹创建完成--" />
</target>
<!--*****************************scp-download-jar-end***********************************************--> <!--*****************************tomcat-stop-start***********************************************-->
<target name="tomcat-stop" description="sshexec">
<echo message="======关停目标服务器...======" />
<sshexec host="${host.name}" username="${username}" password="${password}" port="${port}" command="${tomcat.home.linux}/${tomcat.name}/bin/shutdown.sh" trust="true" />
<echo message="======关停目标服务器完成======" />
</target>
<!--*****************************tomcat-stop-end***********************************************--> <target name="tomcat-start" description="sshexec">
<echo message="======启动服务器...======" />
<sshexec host="${host.name}" username="${username}" password="${password}" port="22" command="${tomcat.home.linux}/${tomcat.name}/bin/startup.sh" trust="true" />
<echo message="======启动服务器完成======" />
</target> </project>

七、测试运行

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

七、常见报错

  因为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...

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

  全选路径D:\development\apache-ant-1.10.5\lib 下的jar包;之所以全选是为了保险,避免缺失jar包;

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

  点击打开即添加成功;注意要事先将jsch-0.1.54.jar包复制到apache-ant-1.10.5\lib路径下;

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

  再次运行,即正常;