简体   繁体   中英

Run multiple war's with gradle and tomcat in NetBeans IDE

I'm using NetBeans 8.0.2 IDE, gradle and tomcat 6. I've creates a root-project with some sub-projects:

  • rootproject
    • frontend (war)
    • rest (war)
    • data (jar)

What I want know is, deploy the rootproject (or frontend) and then deploy the frontend-war and rest-war to the embedded and local tomcat server. I tried the Cargo plugin with identifier com.bmuschko.cargo but there's no way to configure it right, so it can run like i want.

Some stuff on the internet says, that this isn't working well or isn't possible...

Has anyone an idea, how to solve this? Should i use maven instead?

Here are my configuration files:

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

Frontend 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'
}

It seems like you are mixing up multiple concerns in your question. Let me see if I can give you an answer based on what I understand.

You are trying to deploy two WARs created by two different subprojects that are part of a Gradle multi-project build. The Gradle Cargo plugin only supports local or remote containers so I am not sure why you are mentioning an embedded container. For now let's assume we want to deploy to a locally installed Tomcat 6 container.

In the following Cargo plugin configuration, I assume the subprojects to have the project paths :frontend and :rest (they might be different for you). I'd put this configuration into the build script of root project.

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

I don't think NetBeans is really relevant for answering your question. In the IDE you can simply call the corresponding Gradle task. Deployment order shouldn't matter as long as you create the correct project dependencies. You might also want to watch this video on "Web Application Deployments With Gradle" to better understand what can be done in Gradle today.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM