【问题标题】:Less CSS is not compiled in Grails 3 when deploying as WAR部署为 WAR 时,Grails 3 中未编译较少的 CSS
【发布时间】:2025-11-22 23:40:01
【问题描述】:

我在以下环境中有一个 grails 应用程序:

| Grails 版本:3.0.9 | Groovy 版本:2.4.5 | JVM版本:1.8.0_60

我安装了以下插件:

com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0 com.bertramlabs.plugins:less-asset-pipeline:2.6.7

在我的本地开发环境中,我的 less 文件已成功编译,一切都按预期工作。但是,在部署到生产环境时,less 不会被预编译到 css 中(据我所知)。

我认为本地环境很好的假设是由于 less-asset-pipeline 插件默认为 less4j 编译器。

我已经尝试按照 Git repos 的以下文档进行操作:

https://github.com/bertramdev/asset-pipeline https://github.com/bertramdev/less-asset-pipeline

如果我运行 .gradelw assetCompile,我会看到资产管道包含常规 css 文件,但对较少文件的引用被忽略(或失败)。

这是 gradle 构建文件,我删除了一些条目以便更容易找到是否缺少任何内容:

buildscript {
    ext {
        grailsVersion = project.grailsVersion
    }
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }

        // Custom maven repo for the cloudinary plugin
        // maven { url "http://dl.bintray.com/infinit/infinit-opensource" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
    }
}

plugins {
    id "io.spring.dependency-management" version "0.5.2.RELEASE"
}

version "0.1"
group "mchq.admin"

apply plugin: "spring-boot"
apply plugin: "war"
apply plugin: "asset-pipeline"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"

ext {
    grailsVersion = project.grailsVersion
    gradleWrapperVersion = project.gradleWrapperVersion
}

assets {
    minifyJs = true
    minifyCss = true
}

repositories {
    mavenLocal()
    maven { url "https://repo.grails.org/grails/core" }
    maven { url "https://repo.grails.org/grails/repo" }

    // Custom maven repo for the cloudinary plugin
    maven { url "http://dl.bintray.com/infinit/infinit-opensource" }

    maven { url "https://repo1.maven.org/maven2" }
}

dependencyManagement {
    imports {
        mavenBom "org.grails:grails-bom:$grailsVersion"
    }
    applyMavenExclusions false
}

dependencies {
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    provided "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails:grails-dependencies"
    compile "org.grails:grails-web-boot"

    compile "org.grails.plugins:hibernate"
    compile "org.grails.plugins:cache"
    compile "org.hibernate:hibernate-ehcache"
    compile "org.grails.plugins:scaffolding"


    compile 'org.grails.plugins:spring-security-core:3.0.0.M1'

    compile "org.grails.plugins:mail:2.0.0.RC2"

    runtime 'mysql:mysql-connector-java:5.1.37'

    runtime "org.grails.plugins:asset-pipeline"

    compile 'com.bertramlabs.plugins:less-asset-pipeline:2.6.7'


    // Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.)
    testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'

    console "org.grails:grails-console"
}

task wrapper(type: Wrapper) {
    gradleVersion = gradleWrapperVersion
}

task stage() {
    dependsOn clean, war
}
tasks.stage.doLast() {
    delete fileTree(dir: "build/distributions")
    delete fileTree(dir: "build/assetCompile")
    delete fileTree(dir: "build/distributions")
    delete fileTree(dir: "build/libs", exclude: "*.war")
}
war.mustRunAfter clean

奇怪的是,如果我在构建文件的 assets {} 部分添加任何内容,终端内的所有构建都会失败,它只会挂起。

非常欢迎任何有关确保预编译较少文件的指针!

谢谢

【问题讨论】:

    标签: grails gradle less grails-3.0


    【解决方案1】:

    这就是你的build.gradle 对 less 插件的看法

    buildscript {
        dependencies {        
            classpath 'com.bertramlabs.plugins:less-asset-pipeline:2.6.7'
        }
    }
    dependencies {
        runtime 'com.bertramlabs.plugins:less-asset-pipeline:2.6.7'
    }
    

    您还应该获得最新版本的资产管道

    【讨论】:

    • 嗨詹姆斯,太棒了,现在似乎正在编译东西。我也许应该为此打开新线程,但是有没有办法从编译中排除资产子目录,同时仍然允许它们被打包到生产战中?我已经确定要添加到 build.grade 的 excludes 指令。
    • 另外,我自己的 .less 文件的编译似乎没有按资产管道指令的包含顺序处理文件。我的意思是,它正在编译一个buttons.less 文件,该文件引用我放在单独的variables.less 文件中的一个变量。由于首先编译了buttons.less,它还没有到达 variables.less 文件。这种能力是否超出了插件编译方式?
    • @RobinM 您需要排除所有不想单独编译的目录。就我个人而言,我通过将插件设置为排除以 _ 开头的目录下的任何内容来做到这一点。然后我将我的 bootstrap/fontawesome 重命名为该格式。要记住的重要一点是要特别包含任何字体文件,否则在该策略下它们将 404。
    • 完美。非常感谢您对此提供的帮助。你为我节省了大量的时间和挫败感。我采用了与您相同的命名约定,然后排除了所有导致编译问题的第 3 方附加组件,然后根据需要重新包含字体目录、jpg、png 文件。做梦。谢谢
    最近更新 更多