如果可能,使用命令行参数从Maven内部发出HTTP post请求(带有输入类型=" file") [英] Making HTTP post request (with an input type="file") from within Maven, using command line parameters if possible
问题描述
我想转换此bash脚本:
I would like to convert this bash script:
#!/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
可放入maven pom.xml中的内容文件。
into something that could be placed into a maven pom.xml file.
请注意,module-file是一个jar文件(重命名为.omod),理想情况下会在命令行中指定admin-password,类似于命令行参数maven archetype:创建
http:// maven。 apache.org/guides/mini/guide-creating-archetypes.html#Alternative_way_to_start_creating_your_Archetype
Note that module-file is a jar file (renamed .omod), admin-password would ideally be specified on the command line, similar to command line parameters to maven archetype:create http://maven.apache.org/guides/mini/guide-creating-archetypes.html#Alternative_way_to_start_creating_your_Archetype
(理想情况下,主机名也应在命令行中指定)。
(the hostname should ideally be specified on the command line too).
谢谢
你的
Misha
Yours Misha
推荐答案
使用 GMaven 来嵌入内联Groovy脚本,并使用 apache httpclient 来实现帖子请求。这样的事情:
Use GMaven to embed an inline Groovy Script, and use apache httpclient to implement the post request. Something like this:
<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>
这使用maven属性 http.url
和 http.attachmentFile
可以使用-D语法在命令行中指定,也可以在< properties>中的pom.xml文件中指定
阻止。显然,你需要将功能扩展到你的shell脚本正在做的其他事情,但这应该让你开始。
This uses the maven properties http.url
and http.attachmentFile
that you can specify on the command line using the -D syntax or in a pom.xml file in a <properties>
block. Obviously, you'd need to extend the functionality to what else your shell script is doing, but this should get you started.
这篇关于如果可能,使用命令行参数从Maven内部发出HTTP post请求(带有输入类型=" file")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!