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