0

我想转换这个 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

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

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

(理想情况下,主机名也应该在命令行上指定)。

谢谢

你的米莎

4

2 回答 2

5

使用GMaven嵌入内联 Groovy Script,并使用apache httpclient来实现 post 请求。像这样的东西:

<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.urlhttp.attachmentFile您可以使用 -D 语法在命令行上或在<properties>块中的 pom.xml 文件中指定。显然,您需要将功能扩展到您的 shell 脚本正在执行的其他操作,但这应该可以帮助您入门。

于 2010-09-23T09:26:07.030 回答
2

试试Exec Maven 插件。您可能只想存储 cookie${project.build.directory}而不是/tmp(然后您不需要删除它。

您可以为您喜欢的主机使用任何属性名称,例如host.name. 您应该在 POM 中设置默认值:

<properties>
  <host.name>...</host.name>
</properties>

可以-Dhost.name=...在命令行上覆盖它。

于 2010-09-23T05:33:47.790 回答