如何告诉 Grails 使用 Gradle 解决依赖关系

Posted

技术标签:

【中文标题】如何告诉 Grails 使用 Gradle 解决依赖关系【英文标题】:How to tell Grails to use Gradle for dependencies resolution 【发布时间】:2013-06-25 21:22:59 【问题描述】:

我正在尝试使用 gradle 设置一个 grails 项目,但我遇到了一个问题,我无法让 grails 使用 gradle 来解决依赖关系。如果我在 gradle 构建文件中配置依赖项并运行 gradle grails-run-app,它总是报告找不到依赖项 jar 中的类。

当我将依赖项剪切并粘贴到 grails BuildConfig.groovy 中时,一切正常。

我在这里粘贴我的 build.gradle 文件,有什么建议吗?

apply plugin: 'grails'
apply plugin: 'java'
apply plugin: 'jetty'

version "1.0-SNAPSHOT"

buildscript 
    repositories 
        mavenCentral()
        mavenRepo urls: 'http://repository.jboss.org/nexus/content/groups/public/'
    
    dependencies 
        classpath 'com.connorgarvey.gradle:gradle-grails-wrapper:1.0'
    


grails 
    version '2.2.3'


repositories 
    mavenLocal()
    mavenCentral()
    mavenRepo urls: 'http://repository.jboss.org/nexus/content/groups/public/'




dependencies 
    compile 'org.modeshape.bom:modeshape-bom-embedded:3.3.0.Final'
    compile 'postgresql:postgresql:9.1-901.jdbc4'
    compile 'javax.jcr:jcr:2.0'
    compile 'org.modeshape:modeshape-jcr:3.3.0.Final'

【问题讨论】:

【参考方案1】:

我建议改用grails-gradle-plugin。

更新的答案、清理和使用引导范围以将 Tomcat jar 从战争中排除。

一般信息

我关注了 Luke Daley(又名 alkemist)on Youtube at gr8conf 2013 的演讲。我能够创建一个小型 POC,并且 Gradle 似乎可以在 Grails 2.2.3 上正常工作。

Gradle 构建文件

buildscript 
  repositories 
    mavenCentral()
    maven  url 'http://repository.jboss.org/maven2/' 
    maven  url 'http://repo.grails.org/grails/repo' 
    maven  url 'http://repo.grails.org/grails/plugins' 
    maven  url 'http://repository.springsource.com/maven/bundles/release' 
    maven  url 'http://repository.springsource.com/maven/bundles/external' 
    maven  url 'http://repository.springsource.com/maven/libraries/release' 
    maven  url 'http://repository.springsource.com/maven/libraries/external' 
  

  dependencies 
    classpath 'org.grails:grails-gradle-plugin:2.0.0-SNAPSHOT',
    'org.grails:grails-bootstrap:2.2.3' 
  


version='0.0.1'

apply plugin: 'grails'

repositories 
  mavenCentral()
  maven  url 'http://repository.jboss.org/maven2/' 
  maven  url 'http://repo.grails.org/grails/repo' 
  maven  url 'http://repo.grails.org/grails/plugins' 
  maven  url 'http://repository.springsource.com/maven/bundles/release' 
  maven  url 'http://repository.springsource.com/maven/bundles/external' 
  maven  url 'http://repository.springsource.com/maven/libraries/release' 
  maven  url 'http://repository.springsource.com/maven/libraries/external'   


grails 
  grailsVersion '2.2.3'
  version '2.2.3'


configurations 
  all 
    exclude module: 'commons-logging'
    exclude module: 'xml-apis'
  
  test 
    exclude module: 'groovy-all'
  
  compile 
    exclude module: 'hibernate'
  


dependencies 
  compile( "org.grails:grails-crud:$grails.grailsVersion",
           'org.grails:grails-gorm:1.3.7')

  bootstrap "org.grails:grails-plugin-tomcat:$grails.grailsVersion"

【讨论】:

【参考方案2】:

发短信.... ;) 开玩笑。

您必须在 “类路径”配置。您还应该添加任何 Grails 你需要的文物。 'grails-crud' 和 'grails-gorm' 会给你 标准 Grails Web 应用程序所需的一切。

看看plugin docs。

【讨论】:

感谢您的回复,我没有使用 gradle-grails-plugin,因为该插件不适用于 gradle 1.6 和 grails 2.2.3。它总是报告我的 grails 版本是 2.2.3,需要的版本是 1.4.3 请升级 grails。所以我转向 gradle-grails-wrapper。 详细错误信息。应用程序需要 grails 版本 [2.2.3],但 GRAILS_HOME 是版本 [1.3.4] - 如果此 Grails 版本比您的应用程序预期的版本新,请使用正确的 Grails 版本或运行“grails upgrade”。 @user2478308 您是否在类路径变量中将 GRAILS_HOME 设置为 1.3.4?

以上是关于如何告诉 Grails 使用 Gradle 解决依赖关系的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Grails 3 Gradle 项目中控制 H2 驱动程序版本?

Gradle 3.5 搭建 Grails 3.0 项目配置

Gradle / Grails 应用程序

使用gradle bootRun启动时,redirect()无法在Grails中解析@info.app.name @

用于 Grails 2.0 的 Grails/Gradle 插件

Grails & gradle:插件管理