【问题标题】:Include distribution folder of frontend build in the JAR with Gradle使用 Gradle 在 JAR 中包含前端构建的分发文件夹
【发布时间】:2018-05-09 02:51:52
【问题描述】:

我是一名 gradle 初学者,我正在努力将前端分发构建文件夹包含在后端 jar 中(我使用 Spring Boot,前端是一个离子应用程序)。在 backend.gradle 中,我配置了应该包含 frontend-build 文件夹(称为 www)的 jar-Task 到后端的 build 文件夹中。 jar 任务运行完成,但所需的工件不存在于 backend-build 文件夹中,因此不存在于最终的 jar 中。很高兴得到任何帮助。

项目结构:

project
build.gradle
settings.gradle
backend
--> backend.gradle
frontend
--> frontend.gradle

settings.gradle

include 'backend'
include 'frontend'

rootProject.children.each {
    it.buildFileName = it.name + '.gradle'
}

build.gradle

allprojects {

    buildscript {
        repositories {
            mavenCentral()
        }
    }

    apply plugin: 'idea'

    repositories {
        mavenCentral()
    }    
}

前端.gradle

plugins {
  id "com.moowork.node" version "1.2.0"
}   

task clean(dependsOn: 'npm_run_clean') {
}

task build(dependsOn: 'npm_run_build') {
}

backend.gradle

buildscript {
    ext {
        springBootVersion = '1.5.8.RELEASE'
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

group = 'ch.renewinkler'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8


jar {
    from('frontend/www') {
        into('public')
    }
}

processResources.dependsOn(':frontend:build')

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    你需要告诉 gradle jar 任务依赖于前端的构建任务,否则它可能会在构建任务之前运行 jar 文件,因此 jar 中没有任何内容。

    使用项目名称而不是使用绝对路径来引用项目也是一个更好的主意:

    jar {
        dependsOn(':frontend:build')
    
        into('public') {
            from "${project(':frontend').projectDir}/www"
        }
    }
    

    【讨论】:

    • 感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2015-08-11
    • 2012-10-15
    • 2011-04-15
    • 2022-01-24
    • 2021-10-19
    • 2023-01-31
    相关资源
    最近更新 更多