how to integrate with npm web app and spring boot with gradle?

Viewed 7651

Please Help me to integrate with node ui module and Spring boot using gradle.

I just want to deploy war file with web jar file.

My Project structure is like

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 module is rest-api java application.

And web module is npm node app.

I want to do this senario.

  1. If I type ./gradlew clean build

  2. then :web project compile first and make dist directory and then make jar file.

  3. And then :api project make war with this jar file. I will deploy api war to server.

Maybe above step is not right because I'm not good at it.

How should I make code to do it? I have to write script in one build.gradle file. There is only one build.gradle file. I have to use only this file.

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)
}
1 Answers

Have a look at the Gradle Node Plugin.

Include it in your build dependencies:

buildscript {
  ...

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

Apply the plugin:

apply plugin: 'com.moowork.node'

Configure it to fit your project structure:

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

Provide a Gradle task to run NPM:

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

You can find a working example with an Angular app integrated in a Gradle build here.

Related