Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

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

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?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...