将repo中的war部署到远程服务器上

时间:2016-08-23 13:29:35

标签: gradle continuous-deployment cargo

如何使用先前已发布到maven repo上的gradle将战争部署到其中一个Web服务器上? gradle的货物插件是否便利?我可以拥有多个远程环境(DEV / TEST / PROD)吗?我一直在使用货物远程部署它,但总是在构建结束时使用生成的战争并且仅在一个“远程”环境中完成。

任何输入都会有所帮助。

1 个答案:

答案 0 :(得分:0)

我认为此代码会对您有所帮助。在gradle任务runDeployment上创建并运行任务:

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());
            }
        }       
    }

这里我尝试将xyz.war部署到jboss服务器。 我在deployments.properties文件中保留服务器和身份验证。 我们将获取warPath,并且我们在此代码中运行CLI命令。这将使用serverIP和serverPort将战争部署到远程服务器。

这段代码对我有用。