0

How can I deploy a war using gradle that has been previously published onto maven repo onto one of the web servers? Does cargo plugin for gradle facilitates it? Can I have multiple remote environments (DEV/TEST/PROD)? I have been using cargo to deploy it remotely but that was done always at the end of the build using generated war and only with one "remote" environment.

Any input is going to be helpful.

karruma
  • 768
  • 1
  • 12
  • 32

1 Answers1

0

I think this code will help you. Create on gradle task runDeployment and run the task:

task runDeployment { 
    description 'deploy the war to the server'

    doLast {
        String serverIP;
        String serverPort;
        String userName;
        String password;
        String jbossPath;
        Properties prop = new Properties();
        InputStream input;


        try {
            input = new FileInputStream("deployment.properties");
            // load a properties file
            prop.load(input);
            // get the property value setting in the variables.
            serverIP = prop.getProperty("serverIP");
            serverPort = prop.getProperty("serverPort");
            userName = prop.getProperty("userName");
            password = prop.getProperty("password");
            jbossPath = prop.getProperty("jbossPath");
        } catch (IOException ex) {
            logger.info(ex.printStackTrace())
            throw new GradleException("Unable to load deployment.properties file. Exiting....")
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {

                }
            }
        }



            File file = new File("xyz/build/libs"); //path of the war location
            String warPath = file.getAbsolutePath();

            String[] comm = [jbossPath+"jboss-cli.bat","--connect","controller="+serverIP+":"+serverPort,"-u="+userName,"-p="+password, "--command=\"deploy","--force","xyz.war"];
            ProcessBuilder pb = new ProcessBuilder();
            pb.command(comm);
            pb.directory(new File(warPath));
            pb.inheritIO();    
            Process p = pb.start();
            try {
                 p.waitFor();
            } 
            catch (InterruptedException e) {
                 logger.info(e.printStackTrace());
            }
        }       
    }

Here I am try to deploy xyz.war to jboss server. I am keeping server and authentication in deployments.properties file. We will get the warPath and we are running a CLI command in this code. This will deploy the war to the remote server with serverIP and serverPort.

This code worked for me.

Jince Martin
  • 301
  • 1
  • 9
  • 18