Deploy to remote Glassfish instance via cli

2019-05-06 20:40发布

问题:

I am trying to automatically deploy our Java EE application from our build server (Jenkins) to a remote Glassfish server via the command line. At the moment I am using asadmin for this and it works fine, but this option requires me to have Glassfish installed on the build server as well - which I would like to avoid as I do not need it there. The build server is really only running the builds and the deployment so I would like to keep the server as "clean" as possible.

I can't find any download that installs only the asadmin tools, and also my attempt to manually copy over only the required files failed as there are some dependencies to certain *.jars that I don't know of so it always fails unless I copy the whole glassfish installation folder to the build server.

So my question is: Does anybody know how to install only the asadmin tools without installing the whole Glassfish server? Alternatively I would also be happy to use any other command line tools as long as they allow me to deploy to a remote Glassfish instance using secure communication.

回答1:

After doing a bit more research I gave up on trying to install asadmin without the full Glassfish installation and instead used Glassfish's REST admin interface.

I now made it work using CURL in a simple batch file:

curl.exe ^
    --user glassfish_username:glassfish_password ^
    --insecure ^
    -H "Accept: application/json" ^
    -H "X-Requested-By: dummy" ^
    -X POST ^
    -F id=@yourfile.war ^
    -F contextroot=yourcontextroot ^
    -F force=true ^
    https://yourservername:4848/management/domain/applications/application/

The REST API is fairly straight forward once you know what you need to do but just in case somebody else needs this, here a couple of important points:

  • --insecure is required (by CURL) to allow self-signed and untrusted SSL certificates
  • The header attributes for "Accept" and "X-Requested-By" must be set, otherwise Glassfish doesn't process the request and simply returns a blank document as an ansower. No idea why but setting these parameters made it work.
  • The content of the war file is passed as the "id" parameter on the POST
  • The URL needs to be exactly as shown in the snipped above, i.e. do not replace "domain" with your domain name or "application" with your application name. This is the actual REST interface endpoint. There is no need to specify the domain/application name anywhere.