我在Jenkins中使用Ant Script来处理文件的部署。我要执行的操作是触发对具有Web服务的URL的调用。我的问题是,如何从Ant Script或Jenkins中做到这一点?

提前致谢,
蒙特

最佳答案

选项1:“获取”任务

Ant的get task可用于调用Web服务,但仅限于GET操作。仅适用于非常简单的Web服务

选项2: curl

调用unix curl命令以调用Web服务(有关示例,请参见此post)

<target name="invoke-webservice">
    <exec executable="curl">
        <arg line="-d 'param1=value1&param2=value2' http://example.com/resource.cgi"/>
    </exec>
</target>

注意:

在Jenkins中,curl命令也可以作为构建后 Action 来调用

选项3:Groovy ANT任务

如果您需要跨平台和灵活的解决方案,请在构建中嵌入groovy脚本以调用Web服务。
<target name="invoke-webservice">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

    <groovy>
        import static groovyx.net.http.ContentType.JSON
        import groovyx.net.http.RESTClient

        def client = new RESTClient("http://localhost:5498/")
        def response = client.put(path: "parking_tickets",
                                  requestContentType: JSON,
                                  contentType: JSON)

        log.info "response status: ${response.status}"
    </groovy>
</target>

选项4:Groovy Jenkins的后期制作

使用Groovy Postbuild plugin调用Web服务。

选项5:ANT HTTP任务

ANT HTTP task是上述常规任务的替代方法

09-04 20:22
查看更多