【问题标题】:Grails assets not work in deployed .WARGrails 资产在已部署的 .WAR 中不起作用
【发布时间】:2017-03-13 03:49:01
【问题描述】:

我正在通过 Intellij Idea 2016.3 运行应用程序,并且一切正常,当我生成 .WAR (gradlew assemble) 文件时,资产未加载到正在运行的应用程序中并且应用程序无法正常工作。

Grails 3.1.14 JDK 1.8.121

那是我的 build.gradle

    buildscript {
    ext {
        grailsVersion = project.grailsVersion
    }
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "gradle.plugin.com.craigburke.gradle:bower-installer-gradle:2.5.1"
        classpath "org.grails.plugins:hibernate4:5.0.5"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.8.2"
        classpath "org.grails.plugins:views-gradle:1.0.12"
    }
}

    version "1.0.18"
    group "veedy"

    apply plugin:"eclipse"
    apply plugin:"idea"
    apply plugin:"war"
    apply plugin:"org.grails.grails-web"
    apply plugin:"org.grails.grails-gsp"
    apply plugin:"com.craigburke.bower-installer"
    apply plugin:"asset-pipeline"
    apply plugin:"org.grails.plugins.views-json"
    apply plugin: 'spring-boot'


    springBoot {
        executable = true
    }

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

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

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

    dependencies {
        assets "com.craigburke.angular:angular-template-asset-pipeline:2.2.6"
        assets "com.craigburke.angular:angular-annotate-asset-pipeline:2.4.0"
        assets "com.craigburke:js-closure-wrap-asset-pipeline:1.2.0"
        compile "org.springframework.boot:spring-boot-starter-logging"
        compile "org.springframework.boot:spring-boot-autoconfigure"
        compile "org.grails:grails-core"
        compile "org.springframework.boot:spring-boot-starter-actuator"
        compile "org.springframework.boot:spring-boot-starter-tomcat"
        compile "org.grails:grails-plugin-url-mappings"
        compile "org.grails:grails-plugin-rest"
        compile "org.grails:grails-plugin-codecs"
        compile "org.grails:grails-plugin-interceptors"
        compile "org.grails:grails-plugin-services"
        compile "org.grails:grails-plugin-datasource"
        compile "org.grails:grails-plugin-databinding"
        compile "org.grails:grails-plugin-async"
        compile "org.grails:grails-web-boot"
        compile "org.grails:grails-logging"
        compile "org.grails.plugins:cache"
        compile "org.grails:grails-plugin-gsp"
        compile "org.grails.plugins:hibernate4"
        compile "org.hibernate:hibernate-ehcache"
        compile "org.grails.plugins:views-json:1.0.12"
        console "org.grails:grails-console"
        profile "org.grails.profiles:angular:3.1.6"
        runtime "com.h2database:h2"
        runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.8.2"
        testCompile "org.grails:grails-plugin-testing"
        testCompile "org.grails.plugins:geb"
        testCompile "org.grails:grails-datastore-rest-client"
        testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
        testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"


        //custom
        compile 'org.grails.plugins:spring-security-core:3.0.3'
        compile "org.grails.plugins:mail:2.0.0.RC4"
        compile "org.grails.plugins:grails-spring-websocket:2.3.0"
        runtime 'mysql:mysql-connector-java:5.1.36'
    }

    task wrapper(type: Wrapper) {
        gradleVersion = gradleWrapperVersion
    }
    bower {
        'angular'('1.4.x') {
            source 'angular.js'
        }
        'angular-resource'('1.4.x') {
            source 'angular-resource.js' >> '/angular/'
        }
        'angular-mocks'('1.4.x') {
            source 'angular-mocks.js' >> '/angular/'
        }
        'angular-bootstrap'('1.1.x') {
            source 'ui-bootstrap-tpls.js' >> '/angular/'
        }
        'bootstrap'('3.x.x') {
            source 'dist/css/bootstrap.css' >> '/bootstrap/'
        }
    }

    assets {
        minifyJs = true
        minifyCss = true
    }


    bootRun {
        // local.config.location is just a random name. You can use yours.
        jvmArgs = ['-Dlocal.config.location=app-config.yml']
    }

和我的

【问题讨论】:

  • 所以尝试修复它!或者至少提供与细节相关的更多信息。例如。资产管道插件版本或其使用示例。
  • 感谢您的帮助。这些是我的依赖: classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.13.1" apply plugin: "com.bertramlabs.asset-pipeline" runtime "com.bertramlabs.plugins:asset-pipeline-圣杯:2.13.1"

标签: grails intellij-idea


【解决方案1】:

就我而言,不管有没有war,它都可以在build.gradle 中使用低于sn-ps 的情况下正常工作。

基本上,需要提到资产管道的 gradle 插件。此外,在 dependencies 中将其更改为 compile 而不是 runtime

buildscript {
    ext {
       ...
    }
    repositories {
        ...
    }
    dependencies {
        ...
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.10.2"
        ...
    }
}
apply plugin:"asset-pipeline"

更新

在我的 Grails 应用程序中,我有以下代码:

... \grails-app\assets\javascripts\app\app.js

//= require_self
//= require /angular/angular.js
//= require /angular/lodash.js
//= require /angular/ui-bootstrap-tpls-2.3.1.js
//= require /app/directives/directives.js

'use strict';

var app = angular
  .module('myapp', [
    'ui.bootstrap', 'directives'
  ]);

... \grails-app\assets\javascripts\app\directives\directives.js

//= require_self

angular.module("directives",[])
  .directive('bindUnsafeHtml',['$compile', function($compile) {...}]);

index.gsp

 <asset:javascript src="app/app.js" />

请注意:像 //= require_self 这样的 cmets 对模块依赖很重要。

Gradle Angular Asset Pipeline在例子中提到了这一点。

【讨论】:

  • 顺便说一句,我正在使用 grails 3.1.7
  • 而不是运行时尝试使用:compile "com.bertramlabs.plugins:asset-pipeline-grails:2.8.2"
  • 感谢您的帮助,我进行了此更改,但仍然没有成功。
  • 很抱歉听到问题存在。我看到您将资产管道用于 angularjs 的东西。在继续 angular-template-asset-pipeline 之前,我想尝试只包含简单的 JS 文件。 link 它说你需要正确使用 required 的东西。请检查,在您的 app.js 中,您应该有类似以下代码的内容。 //= require /angular/angular //= require /angular/angular-route //= require_self (1) //= require_tree /my-app/app-section/templates
  • 对不起,我不明白你的建议是什么。我的 app.js 有这个选项。
【解决方案2】:

删除 gradle.build 中的 Bower 调用解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 2023-04-04
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    相关资源
    最近更新 更多