使用内联插件运行Grails 3项目

时间:2015-12-26 10:14:30

标签: grails gradle groovy grails-plugin grails-3.0

我几乎可以肯定这是一个愚蠢的问题,但我不知道答案:)我正在将我的项目从Grails 2.5升级到3并尝试前往掌握新的内联插件结构,旧的方式工作正常,但努力让新的方式工作。

我的目录结构如下:


    /
    /settings.gradle
    /myApp             #contains a grails 3 application
    /myPlugin1         #contains a grails 3 plugin
    /myPlugin2         #contains a grails 3 plugin

/settings.gradle包含:


    include 'myPlugin1', 'myPlugin2'
    project(':myPlugin1').projectDir = new File('myPlugin1')
    project(':myPlugin2').projectDir = new File('myPlugin2')

/ myApp中的build.gradle包含:


    compile project(":myPlugin1"), project(":myPlugin2")

上述情况,据我所知是正确的,因为当我运行' gradle build'从根目录它成功构建插件。但是,当我跑


    grails run-app

在myApp目录中,我收到以下错误:


    FAILURE: Build failed with an exception.

    * Where:
    Build file '/Users/donald/myApp/build.gradle' line: 76

    * What went wrong:
    A problem occurred evaluating root project 'myApp'.
    > Project with path ':myPlugin1' could not be found in root project 'myApp'.

    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

    BUILD FAILED

    Total time: 1.572 secs
    | Error Error initializing classpath: Project with path ':myPlugin1' could not be found in root project 'myApp'. (Use --stacktrace to see the full trace)

所以问题是 - 如何让我的应用程序在我的内联插件的正确位置查找?我试过在myApp中创建一个settings.gradle,我得到其他错误:


    Plugin 'io.spring.dependency-management' is already on the script classpath. Plugins on the script classpath cannot be applied in the plugins {} block. Add  "apply plugin: 'io.spring.dependency-management'" to the body of the script to use the plugin.

提前致谢。

1 个答案:

答案 0 :(得分:0)

上下文

我在github上创建了一个多项目测试,以提供一个可行的解决方案。这个多项目测试是使用Grails 3.2.11和Java 1.7创建的。您可以从此处下载:https://github.com/esalomon/multi-project-test3

接下来,您将找到有关其创建的说明。

项目创建

1创建了多项目文件夹:

mkdir multi-project-test3
cd multi-project-test3

2 Grails应用程序和插件已创建:

grails create-app myApp
grails create-plugin myPlugin1
grails create-plugin myPlugin2

3 myApp的settings.gradle文件已删除:

rm myApp/settings.gradle

4使用下一个内容创建多项目的settings.gradle文件:

include 'myApp', 'myPlugin1', 'myPlugin2'
project(':myApp').name = 'myApp'

5 myApp的build.gradle文件使用内联插件引用进行更新:

grails {
    plugins {
        compile project(":myPlugin1")
        compile project(":myPlugin2")
    }
}

项目测试

创建三个控制器,每个Grails项目一个,以测试应用程序:

1创建了myApp的控制器:

cd myApp
grails create-controller myAppController

2使用下一个代码更新myApp的控制器:

package myapp

class AppController {

    def index() {
        render "[${new Date()}] This is the myApp's appController's index response."
    }
}

3创建了myPlugin1的控制器:

cd myPlugin1
grails create-controller myPlugin1Controller

4使用下一个代码更新myPlugin1的控制器:

package myplugin1

class Plugin1Controller {

    def index() {
        render "[${new Date()}] This is the myPlugin1's plugin1Controller's index response."
    }
}

5创建了myPlugin2的控制器:

cd myPlugin2
grails create-controller myPlugin2Controller

6 myPlugin2的控制器将使用下一个代码进行更新:

打包myplugin2

class Plugin2Controller {

    def index() {
        render "[${new Date()}] This is the myPlugin2's plugin2Controller's index response."
    }
}

项目执行

使用run-app命令执行myApp项目时,它会显示带有三个控制器的主Grails页面,每个Grails项目一个:

cd myApp
grails run-app
| Running application...
Grails application running at http://localhost:8080 in environment: development

结论

我希望这可以帮助别人,问候。