如何使用gradle源集创建多个war-artifact

时间:2017-03-10 06:02:51

标签: java spring gradle

我正在尝试将现有的gradle项目转换为使用源集,以便我可以在两个单独的应用程序中重用某些类。

到目前为止,我有以下新的build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
        classpath('com.h2database:h2:1.4.193')
        classpath('org.hibernate:hibernate-core:5.0.1.Final')
    }
}

apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8

sourceSets{
    main{
        apply plugin: 'idea'
        java{
            srcDirs = ['src/main/java']
        }
    }
    api{
        apply plugin: 'idea'
        apply plugin: 'org.springframework.boot'
        apply plugin: 'war'
        springBoot{
            mainClass="com.robocubs4205.cubscout.Application"
        }
        java{
            srcDirs = ['src/main/java','src/api/java']
        }
        war{
            baseName="cubscout-server"
            version="0.0.0"
        }

    }
    web_client{
        apply plugin: 'idea'
        apply plugin: 'org.springframework.boot'
        apply plugin: 'war'
        springBoot{
            mainClass="com.robocubs4205.cubscout.Application"
        }
        java {
            srcDirs = ['src/web-client/java', 'src/main/java']
        }
        war{
            baseName="cubscout-client"
            version="0.0.0"
        }
    }
}

repositories {
    jcenter()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.2.RELEASE'
    //compile 'org.springframework:spring-context:4.3.6.RELEASE'
    //compile 'org.springframework.hateoas:spring-hateoas:0.23.0.RELEASE'
    //compile 'org.springframework.integration:spring-integration-core:4.3.7.RELEASE'
    //compile 'org.springframework.batch:spring-batch-core:3.0.7.RELEASE'
    compile 'org.springframework.data:spring-data-jpa:1.11.0.RELEASE'
    compile 'org.springframework.data:spring-data-rest-webmvc:2.6.0.RELEASE'
    //compile 'org.springframework.security:spring-security-web:4.2.1.RELEASE'
    compile 'org.hibernate:hibernate-core:5.0.1.Final'
    compile 'com.h2database:h2:1.4.193'
    compile 'org.springframework.boot:spring-boot-starter-tomcat:1.5.1.RELEASE'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-all:1.10.19'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat:1.5.1.RELEASE'
}

我的目的是创建两个工件:cubscout-client-0.0.0.warcubscout-server-0.0.0.war,前者使用src/web-client中的源,后者使用src/api中的源,两者都是使用源表单src/main。但是,它一次只能创建一个。

我能找到答案的最接近的是Gradle make multiple war with different resources and classes,但我无法理解答案,因为正如我在Gradle上写的大部分内容一样,人们不会给出任何背景信息。在build.gradle他们的片段去。

我正在使用Intellij Idea作为我的IDE,并且有助于让这两个工件对Intellij可见。

编辑:我尝试过使用多项目构建而不是使用源集。 build.gradle如下:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
        classpath('com.h2database:h2:1.4.193')
        classpath('org.hibernate:hibernate-core:5.0.1.Final')
    }
}
plugins{
    id 'org.springframework.boot' version '1.5.2.RELEASE'
}

apply plugin: 'idea'

allprojects{
    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'war'
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    repositories {
        jcenter()
    }

    dependencies {
        compile 'org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE'
        compile 'org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE'
        compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.2.RELEASE'
        //compile 'org.springframework:spring-context:4.3.6.RELEASE'
        //compile 'org.springframework.hateoas:spring-hateoas:0.23.0.RELEASE'
        //compile 'org.springframework.integration:spring-integration-core:4.3.7.RELEASE'
        //compile 'org.springframework.batch:spring-batch-core:3.0.7.RELEASE'
        compile 'org.springframework.data:spring-data-jpa:1.11.0.RELEASE'
        compile 'org.springframework.data:spring-data-rest-webmvc:2.6.0.RELEASE'
        //compile 'org.springframework.security:spring-security-web:4.2.1.RELEASE'
        compile 'org.hibernate:hibernate-core:5.0.1.Final'
        compile 'com.h2database:h2:1.4.193'
        compile 'org.springframework.boot:spring-boot-starter-tomcat:1.5.1.RELEASE'
        testCompile 'junit:junit:4.12'
        testCompile 'org.mockito:mockito-all:1.10.19'
        providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat:1.5.1.RELEASE'
    }
}

project(':api'){
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'war'
    springBoot{
        mainClass="com.robocubs4205.cubscout.Application"
    }
    war{
        baseName="cubscout-server"
        version="0.0.0"
    }
    dependencies{
        compile project(':')
    }
}

project(':web-client'){
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'war'
    springBoot{
        mainClass="com.robocubs4205.cubscout.Application"
    }
    war{
        baseName="cubscout-client"
        version="0.0.0"
    }
    dependencies{
        compile project(':')
    }
}

当我api:buildclient:build时,构建成功,但运行build导致spring bootRepackage任务失败并显示Unable to find main class。此外,来自api:buildclient:build的工件似乎没有启用弹簧启动,因为它们通常有几页时没有任何来自spring的控制台输出。

0 个答案:

没有答案