在NetBeans IDE中使用gradle和tomcat运行多个war

时间:2015-01-28 15:10:01

标签: java maven tomcat netbeans gradle

我正在使用NetBeans 8.0.2 IDE,gradle和tomcat 6。 我已经创建了一个包含一些子项目的根项目:

  • rootproject
    • 前端(战争)
    • 休息(战争)
    • 数据(jar)

我想知道的是,部署rootproject(或前端),然后将frontend-war和rest-war部署到嵌入式和本地tomcat服务器。 我尝试了标识为com.bmuschko.cargo的Cargo插件,但没有办法正确配置,所以它可以像我想的那样运行。

互联网上的一些内容说,这样做不好或者不可能......

有谁有想法,如何解决这个问题? 我应该使用maven吗?

以下是我的配置文件:

Rootproject build.gradle:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-cargo-plugin:2.0.3'
    }
}


import org.gradle.api.artifacts.*



apply plugin: 'base' // To add "clean" task to the root project.
apply plugin: 'com.bmuschko.cargo'

subprojects { 
    apply from: rootProject.file('common.gradle')
}

task wrapper(type: Wrapper, description: 'Creates and deploys the Gradle wrapper to the current directory.') {
    gradleVersion = '2.1'
}

dependencies {
    def cargoVersion = '1.4.5'
    cargo "org.codehaus.cargo:cargo-core-uberjar:$cargoVersion",
          "org.codehaus.cargo:cargo-ant:$cargoVersion"
}

cargo {
    containerId = 'tomcat6x'

    deployable {
        file = project(':Frontend').war.archivePath
        context = 'frontend'
    }

    deployable {
        file = project(':Rest').war.archivePath
        context = 'rest'
    }

    local {
        homeDir = file('C:\\Program Files\\Apache Software Foundation\\Apache Tomcat 6.0.35')
    }
}

[cargoDeployRemote, cargoRunLocal]*.dependsOn project(':Frontend').war, project(':Rest').war

Rootproject common.gradle:

apply plugin: 'java'
apply plugin: 'maven'

String mavenGroupId = 'rootproject'
String mavenVersion = '1.0-SNAPSHOT'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

repositories {
    mavenCentral();
    // You may define additional repositories, or even remove "mavenCentral()".
    // Read more about repositories here:
    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}

dependencies {
    // Adding dependencies here will add the dependencies to each subproject.
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

String mavenArtifactId = name

group = mavenGroupId
version = mavenVersion

task sourcesJar(type: Jar, dependsOn: classes, description: 'Creates a jar from the source files.') {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives jar
    archives sourcesJar
}

configure(install.repositories.mavenInstaller) {
    pom.project {
        groupId = mavenGroupId
        artifactId = mavenArtifactId
        version = mavenVersion
    }    
}

task createFolders(description: 'Creates the source folders if they do not exist.') doLast {
    sourceSets*.allSource*.srcDirs*.each { File srcDir ->
        if (!srcDir.isDirectory()) {
            println "Creating source folder: ${srcDir}"
            srcDir.mkdirs()
        }
    }
}

前端build.gradle:

apply plugin: 'war'

sourceCompatibility = 1.7
targetCompatibility = 1.7

if (!hasProperty('mainClass')) {
    ext.mainClass = ''
}

sourceSets {
    main {
        java {
            srcDir '/src/main/java'
        }
        resources {
            srcDir '/src/main/java'
            include '**/*.xml'
        }
    }
}

war {
    archiveName = 'Frontend.war'
    exclude('**/*.java,**/*.form')
}

dependencies {
    providedCompile 'javax.servlet:servlet-api:2.5',
                    'javax.servlet:jsp-api:2.0'

    compile project(':Data')

    compile 'antlr:antlr:2.7.6'
}

1 个答案:

答案 0 :(得分:1)

您的问题似乎混淆了多个问题。让我看看我是否可以根据我的理解给你一个答案。

您正在尝试部署由两个不同子项目创建的两个WAR,这两个子项目是Gradle多项目构建的一部分。 Gradle Cargo插件仅支持本地或远程容器,因此我不确定您提到嵌入式容器的原因。现在让我们假设我们要部署到本地安装的Tomcat 6容器。

在以下Cargo插件配置中,我假设子项目具有项目路径:frontend:rest(它们可能与您不同)。我将此配置放入根项目的构建脚本中。

cargo {
    containerId = 'tomcat6x'

    deployable {
        file = project(':frontend').war.archivePath
        context = 'frontend'
    }

    deployable {
        file = project(':rest').war.archivePath
        context = 'rest'
    }

    local {
        homeDir = file('...')
    }
}

[cargoDeployRemote, cargoRunLocal]*.dependsOn project(':frontend').war, project(':rest').war

我认为NetBeans与回答您的问题无关。在IDE中,您只需调用相应的Gradle任务即可。只要您创建正确的项目依赖项,部署顺序就无关紧要。您可能还希望在"Web Application Deployments With Gradle"上观看此视频,以便更好地了解Gradle今天可以做些什么。