开发者

Making HTTP post request (with an input type="file") from within Maven, using command line parameters if possible

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
开发者_开发技巧

into something that could be placed into a maven pom.xml file.

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).

Thank you

Yours Misha


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>

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.


Try the Exec Maven Plugin. You might want to just store the cookie in ${project.build.directory} instead of /tmp (and then you don't need to remove it.

You can use any property name for the host you like, say host.name. You should set a default in the POM:

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

That can be overridden with -Dhost.name=... on the command line.


Top voted answer updated to work for latest JDK's, had to move to gmavenplus - had a similar challenge to try to post a file from Maven, no great options I've found so far to do an easy equivalent of this;

curl <url> --data-binary '@filename'

<plugin>


            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.13.1</version>

            <dependencies>
                <dependency>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpclient</artifactId>
                    <version>4.5.13</version>
                </dependency>

                    <dependency>
                      <groupId>org.codehaus.groovy</groupId>
                      <artifactId>groovy</artifactId>
                      <!-- any version of Groovy \>= 1.5.0 should work here -->
                      <version>3.0.9</version>
                      <scope>runtime</scope>
                    </dependency>


            </dependencies>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <scripts>
                            <script><![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.HttpEntity;
                            import org.apache.http.StatusLine;
                            import org.apache.http.impl.client.DefaultHttpClient;
                            import java.io.File;
        
                            String url = 'https://kroki.io/plantuml/svg';
                            
                            def file = new File("${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);
                        
                            
                            println "Status: " + response.getStatusLine().getStatusCode();  
                            println "Response " + response.toString();

                            ]]></script></scripts>
                    </configuration>

                </execution>
            </executions>

        </plugin>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜