如何将“非 mavenized"jar 依赖项添加到 grails 项目 (Grails 3.x) [英] How to add a “non-mavenized” jar dependency to a grails project (Grails 3.x)

查看:22
本文介绍了如何将“非 mavenized"jar 依赖项添加到 grails 项目 (Grails 3.x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找有关如何将本地/非 Maven jar 文件添加到我的 Grails 3.x 项目的文档和代码示例?

我找到了单独的线程 如何在 grails 中添加非 maven jar - 但这只是 grails 2.3,文件结构和配置在 3.x 中进行了大修.

任何帮助和(尤其是)代码示例都会很棒!.jar在本地项目目录下,打算和.war一起打包部署.

此外,一旦我添加了依赖项,我应该能够从控制器调用它的方法吗?服务文件?或者我是否需要将它们也包括在内?

谢谢!

解决方案

Grails 3 使用 Gradle,因此 Grails 没有特别针对包含本地 jar 的内容.就像将文件依赖项添加到 build.gradle 文件的 dependencies 块一样简单.

根据 Gradle 文件依赖性文档:><块引用>

要添加一些文件作为配置的依赖项,您只需将文件集合作为依赖项传递:

依赖项{...编译文件('libs/a.jar', 'libs/b.jar')//或者编译文件树(目录:'libs',包括:'*.jar')}

上面的例子展示了两种包含存在于本地libs/目录中的jar的方法;你可以做任何一个/或.jar 可以位于文件系统上的任何位置,只需确保指向正确的路径即可.

要在您的应用程序中使用依赖项中的类,您将像往常一样将它们包含在您的服务、控制器和所有其他类中.假设 libs/a.jar 有一个 org.example.Something 类,你可以像这样在 Grails 类的顶部添加一个导入:

import org.example.Something

I'm trying to find documentation and code samples on how to add a local / non-maven jar file to my Grails 3.x project?

I found the separate thread How to add a non-maven jar to grails - but that's only to grails 2.3, and the file structure and configuration has undergone a big overhaul in 3.x.

Any help and (especially) code samples would be wonderful! The .jar is in the local project directory, and I intend to package with the .war for deployment.

Additionally, once i add the dependency, should i just be able to call it's methods from the controller & service files? or do i need to include them in those as well?

thx!

解决方案

Grails 3 uses Gradle, so there's nothing Grails specific about including a local jar. It's as easy as adding a file dependency to the dependencies block of your build.gradle file.

Per the Gradle documentation on File Dependencies:

To add some files as a dependency for a configuration, you simply pass a file collection as a dependency:

dependencies {

    ...

    compile files('libs/a.jar', 'libs/b.jar')
    // or
    compile fileTree(dir: 'libs', include: '*.jar')
}

The above example shows two ways to include jars that exist in a local libs/ directory; you can do either/or. The jar(s) can be anywhere on the filesystem, just make sure you point to the correct path.

To use the classes from the dependency in your application, you'll include them in your services, controllers and all other classes like you normally would. Say libs/a.jar has a class org.example.Something, you'd add an import to the top of your Grails class like so:

import org.example.Something

这篇关于如何将“非 mavenized"jar 依赖项添加到 grails 项目 (Grails 3.x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆