Making HTTP requests during maven archetype:genera

2020-06-24 08:18发布

问题:

I'm building an archetype for creating standardised projects within the company. The actual project files generated are fine, I'd now like to integrate the archetype with the process of setting up infrastructure, such as Stash repositories and Jenkins jobs.

For example, simplistically I'd like the maven archetype:generate process to include an HTTP REST POST to http://stash.example.com/rest/api/1.0/projects/LABS/repos/{artifactId} in order to create a GIT repo.

Likewise something similar for Jenkins build jobs. I would be nice if, prior to creating the maven project, some tests could be done to Stash/Jenkins to prevent name collisions.

I imagine the solution to this is to bind some maven plugins to some build phases.

I've been unable to find any existing plugins, or examples of people doing similar during an archetype generation (without the Groovy hammer).

  1. Are there any existing Maven plugins that assists in making HTTP/REST requests during build?
  2. Are there any existing Maven plugins that assist accessing Stash or Jenkins APIs?
  3. Which Maven phases would be good candidates for checking selected names/ids against remote systems, and which phases would be good candidates for actually creating the remote resources?

Has anyone tried this sort of thing, and if so how did it work out?

I see some related questions

  • Making HTTP post request (with an input type="file") from within Maven, using command line parameters if possible
  • maven plugin to call or invoke a rest web service

And some potential plugins

  • maven-http-client-plugin (stale)

回答1:

There are couple of of ways you can do this. Two ways we did this previously were using:

1. Using groovy - gmaven plugin and RESTClient inside it

Our code is a bit complicated to paste here, but we did some thing very similar to this:

Groovy Rest Invocation Using Maven

2. Antcontrib's loadresource :

Example:

<loadresource property="build_start_date">
   <url url="${JOB_URL}/lastSuccessfulBuild/api/xml?xpath=/*/id/text()"/>
</loadresource>

This connects to a hudson rest api and get's me the last build date which is stored in the property build_start_date.