How to merge client and server builds into one jar with gradle?

◇◆丶佛笑我妖孽 提交于 2021-02-11 17:35:43

问题


I have a project with the following structure:

web-client/     # Angular Client
  build/
  build.gradle  

server/         # Spring Boot Application
  build/
  build.gradle  

build.gradle    # The "parent" project. Works on web-client and server

The parent is supposed to copy the compiled web-application into server/build/classes/static such that it will be copied to /BOOT-INF/classes/ of the final jar where it will be served by the Spring Boot server.

Everything is working so far except the last part. Those files are nor copied into the final jar and I think it's because it got already build at the time where the copying task is executed.

This is the script which I am currently using:


task buildWebApp {
    outputs.dir('mobile-client/build')
    dependsOn ':mobile-client:buildWebApp'
}

task copyWebApp {
    doFirst {
        copy {
            from 'mobile-client/build'
            into 'server/build/classes/static'
        }
    }
    dependsOn tasks.buildWebApp
}

# assemble.dependsOn copyWebApp
build.dependsOn copyWebApp

How can I make sure that those files from mobile-client/build end up in the final jar of server?


回答1:


Cannot guarantee its current functionality, but this I used in one of my projects a few years ago. I did use separate gradle submodule for building Frontend and then separate module for Backend where I included Frontend as a JAR:

root gradle project -> frontend
                    -> backend

Frontend build.gradle (builds frontend JAR with /static/**)

apply plugin: "com.moowork.node"
apply plugin: 'java'

node {
  version = '8.9.3'
  download = true
}

def webResources = "$buildDir/web-resources/main"

sourceSets {
  main {
    output.dir(webResources, builtBy: 'buildWeb')
  }
}

task webInstall(type: NpmTask) {
  args = ['install']
}

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

build.dependsOn buildWeb

Backend build.gradle

apply plugin: 'spring-boot-gradle-plugin'
apply plugin: 'idea'

dependencies {
  compile project(':frontend')
}


来源:https://stackoverflow.com/questions/65645773/how-to-merge-client-and-server-builds-into-one-jar-with-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!