How to add static files to jar using Gradle build in Spring Boot + Angular 2 project How to add static files to jar using Gradle build in Spring Boot + Angular 2 project heroku heroku

How to add static files to jar using Gradle build in Spring Boot + Angular 2 project


Try a different approach. Instead of manually copying the resources, tell Gradle that when it processes resources for the JAR, also take into consideration what is in frontend/dist/:

processResources {    from ('frontend/dist/') {        into 'public'    }}

This should result in a JAR containing a public/ directory, with the contents of frontend/dist/ inside of it.


Gradle configuration for Spring Boot 1.5\2.x + Angular 2-6

Angular in sub-folder frontend

Frontend module

Crate build.gradle:

plugins {  id "com.moowork.node" version "1.2.0"}node {  version = '8.11.3'  npmVersion = '5.6.0'  download = true  workDir = file("${project.buildDir}/node")  nodeModulesDir = file("${project.projectDir}")}task build(type: NpmTask) {  args = ['run', 'build']}build.dependsOn(npm_install)

Note for Angular 6

Update outputPath value in angular.json to 'dist'

Backend module

Edit build.gradle for backend module:

Spring Boot 2.X:

bootJar {    archiveName = "yourapp.jar"    mainClassName = 'com.company.app.Application'    from('frontend/dist') {        into 'static'    }}

Spring Boot 1.5.X:

jar {    archiveName = "yourapp.jar"    manifest {        attributes 'Main-Class': 'com.company.app.Application'    }    from('frontend/dist') {        into 'static'    }    from {        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }    }}

Finally execute bootRepackage or bootJar task and check results in builds/libs


Assume that front end is located at the following folder: src/main/webapp/fe-ui/, the following solution for the Spring Boot version 2.1.1.RELEASE could be considered:

bootJar {    baseName = 'jar-name'    version = '0.1.0'    from('src/main/webapp/fe-ui/build') {        into 'public'    }}task installFeDependencies(type: NpmTask) {    args = ['install']}task buildFe(type: NpmTask) {    args = ['run', 'build']    dependsOn installFeDependencies}compileJava {    dependsOn buildFe}

Running gradlew build will install, build front end as well as will invoke bootJar. The latter will package built front end bundle.