2017-10-08 52 views
2

請幫我使用gradle與節點ui模塊和Spring引導進行集成。如何與npm web應用程序和Spring引導與gradle集成?

我只是想用web jar文件部署文件。

我的項目結構是這樣

myproject 
    api 
     src/main/java 
     src/main/resources 
     build/libs 
    web 
     <--- node files 
     dist 
     build/libs 
    gradle 
    build.gradle 
    gradlew 
    gradlew.bat 
    settings.gradle 

api模塊是REST的API的Java應用程序。

web模塊是npm節點應用程序。

我想做這個塞納里奧。

  1. 如果鍵入./gradlew clean build

  2. 然後:web項目編譯第一,使dist目錄,然後進行jar文件。

  3. 然後:api項目與這個jar文件打仗。我將部署api war到服務器。

也許上面的步驟是不正確的,因爲我不擅長它。

我應該如何編寫代碼? 我必須在一個build.gradle文件中編寫腳本。 只有一個build.gradle文件。我只能使用這個文件。

buildscript { 
    ext { 
    springBootVersion = '1.5.7.RELEASE' 
    } 
    repositories { 
    mavenCentral() 
     maven { url 'http://repo.spring.io/plugins-release'} 
     maven { url "https://plugins.gradle.org/m2/" } 

    } 
    dependencies { 
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
     classpath 'io.spring.gradle:propdeps-plugin:0.0.9.RELEASE' 
     classpath "com.moowork.gradle:gradle-node-plugin:1.2.0" 
    } 
} 

subprojects { 
    apply plugin: 'java' 
    apply plugin: 'eclipse' 

    group = 'com.example' 
    version = '0.0.1-SNAPSHOT' 
    sourceCompatibility = 1.8 

    repositories { 
     mavenCentral() 
    } 
} 


project('api') { 
    apply plugin: 'org.springframework.boot' 

    apply plugin: 'war' 
    apply plugin: 'propdeps' 

    dependencies { 
     compile project(':web') 

    compile('org.springframework.boot:spring-boot-starter-web') 
    runtime('org.springframework.boot:spring-boot-devtools') 
    compileOnly('org.projectlombok:lombok') 
    testCompile('org.springframework.boot:spring-boot-starter-test') 

     optional('org.springframework.boot:spring-boot-configuration-processor') 
    } 

    compileJava.dependsOn(processResources) 
} 

project('web') { 
    apply plugin: 'com.moowork.node' 

    node { 
     version = '6.11.4' 
     npmVersion = '3.10.10' 
     download = true 
     distBaseUrl = 'https://nodejs.org/dist' 
    } 

    task nodeBuild(type: NpmTask) { 
     args = ['run', 'build'] 
    } 

    jar { 
     from ("dist/") 
     into ("${rootProject.project('api').projectDir}/src/main/resources/") 
     includeEmptyDirs = true 
    } 
    clean { 
     delete 'dist/' 
    } 

    nodeBuild.dependsOn(npm_install) 
    build.dependsOn(nodeBuild) 
} 

回答

3

看看Gradle Node Plugin

包括在您的構建依賴:

buildscript { 
    ... 

    dependencies { 
    classpath "com.moowork.gradle:gradle-node-plugin:1.1.1" 
    } 
} 

應用插件:

apply plugin: 'com.moowork.node' 

配置以滿足您的項目結構:

node { 
    version = '6.10.2' 
    npmVersion = '3.10.6' 
    download = true 
    workDir = file("${project.buildDir}/node") 
    nodeModulesDir = file("${project.projectDir}") 
} 

提供一個搖籃任務運行NPM:

task build(type: NpmTask) { 
    args = ['run', 'build'] 
} 
build.dependsOn(npm_install) 

你可以在Gradle build here中找到一個集成了Angular應用程序的工作示例。

相關問題