繁体   English   中英

在Gradle中创建具有附加编译依赖性的生产配置

[英]Create production configuration with additional compile dependency in Gradle

我正在尝试为生产版本定义构建脚本。

下面是项目结构,所有项目都是java插件。

wrapper (parent)
|--frontend (child)
|  |--src
|  |  |--js (raw ES6 modules)
|  |  |--sass (raw)
|  |--build
|     |--lib
|     |  |--production-front.jar
|     |--dist
|        |--js (bundled)
|        |--css (compiled production)
|--backend (child) (spring boot)
   |--build
      |--lib
         |--RELEASE.jar

现在,这里发生的是backend默认情况下( sourceSets.main.resources.srcDirs )直接链接到:

  • 原始的 :frontent/src/js
  • 生成 :frontent/build/dist/css

这样,当您运行它时,默认情况下它将处于开发模式。 在这里,这意味着它将:

  • 使用生成的 scss-> css文件(这是资源,因此,例如,如果您运行后台gulp-sass每次更改scss都会对其进行编译,则css会更新并迅速发展,开发周期很快)。
  • 使用直接在浏览器(JSPM,SystemJS,Babel)中编译的原始 JS-因此,您只需要编辑:frontent/src/js并刷新页面。

好吧,尽管开发人员是爱人,但我还需要进行编译以进行生产。 上面提到的项目结构还显示了:frontend生成production-front.jar

这是带有我的注释的默认Java构建树。

在此处输入图片说明

编辑我需要进行依赖,将production-front.jar编译为RELEASE.jar并省略提及的附加资源。

请注意,我只需要ommit这些资源,在没有任何其他main.resources.srcDirs

解决此问题的正确方法是什么(不执行例如从.jar中删除开发资源,然后放入其他production-front.jar的任务)? 我不知道如何制作多个可以在这里工作的sourceSet或配置。

在过去一个星期对Gradle进行了非常深入的学习之后(创建这个主题是因为我快要死了),我终于找到了非常令人满意的解决方案。

我想分享一下我的目标和最终解决方案:

  • 具有最小的build.gradle
  • 有可能随着build --continuous而发展- build --continuous
  • 做到这一点,以使eclipse插件(可能还包括其他IDE)可以使用其自己的构建系统完美地反映纯命令行Gradle开发。

这是一个多项目,其中一个是带有DevTools的Spring Boot应用程序。

wrapper (parent)
|--frontend (child)
|  |--src-client
|  |  |--static
|  |  |  |--img (images)
|  |  |  \--js (raw ES6 modules)
|  |  \--sass (raw, note not in static folder)
|  \--build
|     |--lib
|     |  \--front.jar
|     |--dist
|     |  |--js (bundled, minimized)
|     |  \--css (compiled production, minimized)
|     \--dev
|        \--css (compiled dev, compact readable format)
\--backend (child) (spring boot)
   |--src
   |  |--main/java
   |  |--main/resources
   |  \--test/java
   \--build
      \--lib
         \--application.jar

正如我所描述的,目标是:

  • 使bootRun可以使用js和已编译的 CSS的原始资源运行,也可以使用backend main包含的所有资源运行。
  • 使bootJar依赖于已编译的front.jar而不是dev中使用的所有内容(上一点),编译到生产环境中。

我已经使用了配置,sourceSets和bootRun属性的组合(很多时间)。

以下是文件(向下精简):

包装器/ build.gradle

wrapper.gradleVersion '5.0-milestone-1'

allprojects {
    apply plugin: 'eclipse'
}

包装器/前/ build.gradle

plugins {
    id 'java' // possibly use java-base or just custom zip task, since client doesn't actually compile java
}

jar {
    dependsOn buildProduction // task that compiles my stuff into build/dist
    baseName 'front'
    classifier 'SNAPSHOT-' + new Date().format('yyyyMMddHHmmss')
    from(buildDir.absolutePath + '/dist') {
        into 'static'
    }
}

// Note there is a lot of other tasks here that actually compile my stuff, like gulp-sass and JSPM bundling with babel transpiler.

包装器/后端/ build.gradle

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.0.RC1'
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 10 // eclipse has (or had) problems with Java 11
targetCompatibility = 10

sourceSets {
    // 'java' plugin adds default main sourceSet
    dev {
        resources.srcDirs = [
            project(':front').projectDir.absolutePath + '/src-client',
            project(':front').buildDir.absolutePath + '/dev'
        ]
    }
}

bootJar {
    baseName 'application'
    classifier 'SNAPSHOT-' + new Date().format('yyyyMMddHHmmssSSS')
    // I used bootArchives since it was already there and my stuff fits description, you can also define your own configuration and extend runtime one.
    classpath configurations.bootArchives
}

bootRun {
    sourceResources sourceSets.dev // I make bootRun (dev) use dev sourceSet
}

dependencies {
    runtime 'org.springframework.boot:spring-boot-devtools'
    // Since bootArchives configuration is used only by bootJar (not bootRun), this will be only in final fat .jar
    bootArchives project(':front')
    ...
}

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

一些帮助的链接: http : //mrhaki.blogspot.com/2014/09/gradle-goodness-adding-dependencies.html

请注意,客户端的源文件夹称为src-client :这是在Eclipse中制作“ 完美镜像”的关键。 如果我们将其命名为src ,它将在backend eclipse中已由main使用,它将以名称clash呕吐(这可以通过配置eclipse插件来解决,但是再说一次-我们希望保持其简单性,而不使用IDE配置)。

最终结果是,通过gradle的连续构建,我们得到了非常不错的命令行开发,并且在eclipse中完全镜像了行为,默认情况下,其配置以相同的方式工作(但使用eclipse的builder而不是gradle的builder)。 我们还可以在backend项目中获得链接到front使用的源的漂亮文件夹。 同样可能适用于IDEA :)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM