在Maven中发出HTTP post请求(带有输入类型="file"),如果可能的话使用命令行参数

Миш*_*лев 0 post maven-2 http multipartform-data

我想转换这个bash脚本:

#!/bin/bash
if ! [ $# == 2 ]; then
   echo Usage: update-module admin-password module-file
   exit 1
fi
if ! [ -f $2 ]; then
   echo Error: module file $2 does not exist
   exit 1
fi
curl -c /tmp/cookie.txt -d uname=admin -d pw=${1} http://localhost:8080/openmrs/loginServlet
curl -b /tmp/cookie.txt -F action=upload -F update=true -F moduleFile=\@$2 http://localhost:8080/openmrs/admin/modules/module.list
rm -rf /tmp/cookie.txt > /dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)

进入可以放入maven pom.xml文件的东西.

请注意,module-file是一个jar文件(重命名为.omod),理想情况下会在命令行中指定admin-password,类似于maven原型的命令行参数:create http://maven.apache.org/guides/mini /guide-creating-archetypes.html#Alternative_way_to_start_creating_your_Archetype

(理想情况下,主机名也应在命令行中指定).

谢谢

你的Misha

Sea*_*oyd 5

使用GMaven嵌入内联Groovy脚本,并使用Apache的HttpClient的执行职务的请求.像这样的东西:

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.0.2</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source><![CDATA[

                    import org.apache.http.HttpResponse;
                    import org.apache.http.client.HttpClient;
                    import org.apache.http.client.methods.HttpPost;
                    import org.apache.http.entity.InputStreamEntity;
                    import org.apache.http.impl.client.DefaultHttpClient;

                    String url = pom.properties['http.url'];
                    File file = new File(pom.properties['http.attachmentFile'])
                    HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(url);
                    InputStreamEntity entity = new InputStreamEntity(file.newInputStream());
                    post.setEntity entity;
                    HttpResponse response = client.execute(post);

                ]]></source>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

这将使用Maven的特性http.url,并http.attachmentFile可以使用在该-D语法或一个pom.xml文件的命令行上指定<properties>块.显然,您需要将功能扩展到shell脚本正在执行的操作,但这应该可以帮助您入门.