调用Ant构建服务端搭建
以Jmeter+Ant框架为例详见【Webhook触发Jmeter自动化任务】
1.安装Jdk环境,版本1.8;
2.安装Jmeter+Ant环境,详细步骤请度娘;
3.框架Spring boot+Maven
![](https://img.haomeiwen.com/i3596084/92ce2b39587fe657.png)
接口代码
// post请求
@RestController
public class ShellController {
private static final Logger logger = LoggerFactory.getLogger(ShellController.class);
// @RequestMapping("/test")
@RequestMapping(value = "/autoBuild", method = RequestMethod.POST)
public String test(@RequestParam(required = true, name = "buildLocal") String buildLocal) {
File buildFile = new File(buildLocal);
Project p = new Project();
// 创建一个默认的监听器,监听项目构建过程中的日志操作
AutoShellJmeterLogger consoleLogger = new AutoShellJmeterLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
// 输出信息级别
consoleLogger.setMessageOutputLevel(2);
p.addBuildListener(consoleLogger);
try {
// 初始化该项目
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
// 解析项目的构建文件
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
p.fireBuildFinished(null);
} catch (BuildException ex) {
p.fireBuildFinished(ex);
}
return "success!";
}
// get请求
// @RequestMapping("/test")
@RequestMapping(value = "/autoBuild", method = RequestMethod.GET)
public String test1(@RequestParam(required = true, name = "build") String build) {
try {
File buildFile = new File(build);
Project p = new Project();
// 添加日志输出
AutoShellJmeterLogger consoleLogger = new AutoShellJmeterLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
// 输出信息级别
consoleLogger.setMessageOutputLevel(2);
p.addBuildListener(consoleLogger);
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
p.fireBuildFinished(null);
} catch (Exception ex) {
logger.error("error", ex);
}
return "success!";
}
}
调用日志方法
public class AutoShellJmeterLogger extends DefaultLogger {
private static final Logger logger = LoggerFactory.getLogger(AutoShellJmeterLogger.class);
protected void log(String message) {
logger.info(message);
}
}
配置文件(application.properties)
##服务的名称
spring.application.name=autoshell-service
##服务的端口
server.port=9009
##字符集
server.tomcat.uri-encoding=UTF8
日志配置文件(logback.xml)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d[%p][%c:%L] - %m%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="autoshelllog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>log/autoshell.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/autoshell.%d.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d[%p][%c:%L] - %m%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 控制台输出日志级别 -->
<root level="info">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.autoshell" level="DEBUG">
<appender-ref ref="autoshelllog" />
</logger>
</configuration>
POM文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testweb</groupId>
<artifactId>autoshell</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>autoshell</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>
<dependency>
<groupId>org.programmerplanet.ant</groupId>
<artifactId>ant-jmeter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-javamail</artifactId>
<version>1.10.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Maven构建打包jar包
java -jar XXX.jar启动服务
调用地址
http://ip:9009/autoBuild?buildLocal=/test /iBuildInterfacTest/build.xml
buildLocal参数值为ant的build文件位置,需要输入完整的路径,windows与liunx环境都支持;
注:这个服务只要是含Ant框架的都可以使用,参数值为ant build.xml的完整路径;
是不是超级so easy!!