且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为grails项目添加一个“非mavenized”jar依赖项(Grails 3.x)

更新时间:2023-10-05 23:50:40

Grails 3使用Gradle,所以Grails没有关于包含本地jar的具体内容。这很简单,只需将文件依赖项添加到 build.gradle 文件的依赖项块中即可。



根据文件相关性的 Gradle文档
$ b


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




 依赖关系{

...

编译文件('libs / a.jar','libs / b.jar')
//或
编译fileTree(dir:'libs',include:'* .jar')





$ b

上面的例子展示了两种包含存在于本地 libs / 目录;你可以做/或。 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