简体   繁体   English

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

[英]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: 我正在使用NetBeans 8.0.2 IDE,gradle和tomcat6。我已经创建了一个带有一些子项目的根项目:

  • rootproject 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. 我想知道的是,部署rootproject(或前端),然后将front-war和rest-war部署到嵌入式和本地tomcat服务器。 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. 我尝试使用标识符为com.bmuschko.cargo的Cargo插件,但是没有正确配置它的方法,因此它可以像我想要的那样运行。

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? 我应该改用Maven吗?

Here are my configuration files: 这是我的配置文件:

Rootproject build.gradle: 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: 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: 前端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. 您正在尝试部署由两个不同子项目创建的两个WAR,这两个子项目是Gradle多项目构建的一部分。 The Gradle Cargo plugin only supports local or remote containers so I am not sure why you are mentioning an embedded container. Gradle Cargo插件仅支持本地或远程容器,因此我不确定为什么要提到嵌入式容器。 For now let's assume we want to deploy to a locally installed Tomcat 6 container. 现在,假设我们要部署到本地安装的Tomcat 6容器。

In the following Cargo plugin configuration, I assume the subprojects to have the project paths :frontend and :rest (they might be different for you). 在以下Cargo插件配置中,我假设子项目具有项目路径:frontend:rest (它们可能与您不同)。 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. 我认为NetBeans与回答您的问题并不真正相关。 In the IDE you can simply call the corresponding Gradle task. 在IDE中,您可以简单地调用相应的Gradle任务。 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. 您可能还想在“使用Gradle进行Web应用程序部署”上观看此视频,以更好地了解今天可以在Gradle中完成的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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