---
title: "使用gradle-cargo-plugin热部署(轻量级)war"
author: "L.x"
date: "2014-11-12T16:07:16Z"
tags: []
categories: ["编程语言"]
slug: "gradle-cargo-plugin-war"
draft: false
lastmod: "2014-11-12T16:21:10Z"
id: 2155013
url: "https://l-x.iteye.com/blog/2155013"
---

1. 下载tomcat6:[http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.41/bin/apache-tomcat-6.0.41.zip](http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.41/bin/apache-tomcat-6.0.41.zip)
2. 解压Tomcat到服务器，本例子将tomcat解压在本地
3. 修改%TOMCAT\_HOME%/conf/tomcat-users.xml文件如下：
   
   ```
   <tomcat-users>
   
   
     <role rolename="tomcat"/>
     <role rolename="manager"/>
     
     <user username="admin" password="admin" roles="tomcat,manager"/>
   
   </tomcat-users>
   ```
4. 在gradle项目中引入gradle-cargo-plugin
   
   ```
   apply plugin: 'com.bmuschko.cargo'
   apply plugin: 'com.bmuschko.cargo-base'
   
   buildscript {
       repositories {
           jcenter()
       }
   
       dependencies {
           classpath 'com.bmuschko:gradle-cargo-plugin:2.0.1'
       }
   }
   ```
5. 配置cargo插件
   
   ```
   cargo {
       containerId = 'tomcat6x'
       port = 8080 //服务器tomcat端口号
   
       deployable {
           context = '/'
       }
       remote {
           hostname = "localhost"//服务器地址
           username = "admin" //来源于tomcat-users.xml
           password = "admin"
           timeout = 6000
       }
   }
   ```
6. 重新配置war task只包含源码
   
   ```
   war {
       classpath = classpath.filter {
           return !it.name.endsWith(".jar")
       }
   }
   ```
7. 将所有的依赖包拷贝到服务器%TOMCAT\_HOME%/lib目录下(本例子只依赖commons-io)
8. 运行gradle war创建war文件
9. 运行gradle cargoDeployRemote/cargoRedeployRemote

 

**完整的build.gradle**

 

 

```
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-cargo-plugin:2.0.1'
    }
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'com.bmuschko.cargo'
apply plugin: 'com.bmuschko.cargo-base'

sourceCompatibility = 1.5
version = '1.0'
repositories {
    mavenLocal()
    mavenCentral()
}


dependencies {
    compile 'commons-io:commons-io:2.4'
}
war {
    classpath = classpath.filter {
        return !it.name.endsWith(".jar")
    }
}
cargo {
    containerId = 'tomcat6x'
    port = 8080

    deployable {
        context = '/'
    }
    remote {
        hostname = "localhost"
        username = "admin"
        password = "admin"
        timeout = 6000
    }
}
```