How can I exclude classes from a Grails 3 plugin - grails

I'm trying to convert a Grails 2 plugin to Grails 3 and I have trouble excluding a couple of domain classes that are used in tests only from the resulting jar.
The documentation states:
in your build.gradle you should exclude the compiled classes from the JAR file:
jar {
exclude "com/demo/**/**"
}
...but if I try this I get the following error from Gradle:
Could not find method jar() for arguments [build_64zizslf5a7zfo329yz5tdsoi$_run_closure1#566c53c] on root project '...'
I can post the entire stack trace but it doesn't look very helpful. I'm using Gradle 2.3, provided by the default Gradle wrapper that the create-plugin command generates. I also haven't made any other changes to build.gradle because my plugin doesn't require any external dependencies.

See the project at https://github.com/jeffbrown/excludesdemo. That includes 2 domain classes under https://github.com/jeffbrown/excludesdemo/tree/master/grails-app/domain.
The top level build.gradle file at https://github.com/jeffbrown/excludesdemo/blob/72896a3f88f617d530bbdde8976d5bfe8d1e820a/build.gradle#L73 contains the following:
jar {
exclude 'package1/**'
}
When you run ./gradlew jar you should see that the jar file generated at build/libs/excludesdemo-0.1-SNAPSHOT.jar does contain package2/Author.class and does not contain package1/Person.class.

Related

Grails Multi-Project: Running a Plugin's custom script

Followed the tutorial on multi-projects
Everything mostly works. Plugin controllers & domain classes load properly in the application. However, a problem occurs when trying to run a Plugin's custom script from the application's grails CLI.
For example:
If you set up the multi-project directory structure like this:
Project Root
Application Directory
Plugin Directory
settings.gradle
And ran this command from the Plugin Directory
grails create-script hello
You'd be able to access the script when running grails from the Plugin Directory, but not the Application's Directory.
Is there a way to get this to work properly? Do I need to use an alternative set up?
Also see Creating a Custom Script in Grails
A conventional grails 3 plugin is different than a plugin within a multi-project. It doesn't seem to be designed to compile a plugin such as grails scaffolding with custom commands.
For this reason, you should package the plugin manually using:
grails package-plugin
grails install
Now in the build.gradle, add this line to dependencies:
compile "<plugin-group>:<plugin-name>:<plugin-version>
Subsituting the appropriate information within the brackets <>.
You can find the plugin-group in the plugin's build.gradle
group "org.grails.plugins"
plugin-name you specified in the grails create-plugin command
grails create-plugin plugin-name
plugin-version is also found in the plugin's build.gradle
version "0.1"

How to add a “non-mavenized” jar dependency to a grails project (Grails 3.x)

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

Referencing external dependencies in GGTS by convention

How do I reference a dependency by convention within my project build path? Allow me to elaborate...
I'm using the Groovy/Grails Tool Suite (GGTS 3.0). I'm referencing the dependency as such in my BuildConfig.groovy:
dependencies {
compile 'tgt:jaxb-utils:1.0'
}
The referenced jar file is successfully pulled down from the Artifactory repo - I can find the jar file on my local file system in the Ivy cache. When I run any grails targets (grails compile, grails run-app, grails run-tests), it works fine - meaning my code that imports the classes from the jar has no problems. When I run grails war, the referenced jar file is packed into the constructed war.
However, and here is the crux of this post - the project build path does not reference this jar file by default or convention, so my java or groovy code that imports the classes from the jar file reports a unable to resolve class ... error.
One solution is to simply add the jar file to the lib folder and update the build path accordingly, or modify the build path to reference the jar file in the Ivy cache folder. However, I'd have to do this for any/all dependencies pulled down in this way. Any jars in the lib folder will get saved to the team source code repository, and that seems like wasted space since the grails commands work fine with just the dependency reference in BuildConfig.groovy.
Am I just being too idealistic (ie - difficult) here, or is there a better way to clean up the unable to resolve class ... errors in my IDE without having to manually add the dependent jar files to my lib folder and update my build path? Ideas?
Eclipse / STS / GGTS : If you have Grails plugin installed, you can do the following :
Right click on your project -> Grails Tools -> Refresh dependencies (or shortcut "Alt+G, R")

How to install gradle-grails-plugin?

Complete gradle nooby here.
I want to be able to execute grails build commands like "grails compile", "grails test-app" and so forth from gradle.
I found the grails-gradle-plugin from Peter Ledbrook at: https://github.com/grails/grails-gradle-plugin.
Cloning the repository I get some files and folders. In the readme file it says " include the required JARs via buildscript {} and 'apply' the plugin". The apply part sure I get but how do I add the JAR? And which jar? Do I need to use gradle on the build file in the folder of the downloaded plug-in and compile a jar? And ones I get the jar where do I place it and how do I include it in my projects build.gradle file?
I have a feeling this is going to be ridiculously easy but I just can't get it to work.
In Gradle, the jars are added to build script or to your application class path through dependencies closure e.g.
dependencies {
compile "org.grails:grails-crud:1.3.4"
compile "org.grails:grails-gorm:1.3.4"
compile "ch.qos.logback:logback-core:1.0.7"
compile "org.slf4j:slf4j-api:1.7.2"
}
compile is a name of one of the many configurations (there are also test, runtime etc.) and e.g. "org.grails:grails-crud:1.3.4" is a reference to a jar in one of the public repositories, which are also specified in your scripts in repositories closure.
You can read more about Gradle dependency management in http://gradle.org/docs/current/userguide/dependency_management.html.
For your Grails project you need to define a build.gradle file which looks similar to what is described in the README.
Though I tried today to just create a simple Grails project using that plugin and gradle init command and it didn't work. I have created an issue for that: https://github.com/grails/grails-gradle-plugin/issues/16.

How to add a "non-mavenized" jar dependency to a grails project

I'm a grails newby. I am using grails 2.0.3 from the command line (no IDE) on windows 7.
I am just running the 'hello world' example from the grails documentation.
I have a custom jar that is not in maven that I would like to use in my project. I added it to the lib folder and did a refresh-dependencies. I modified the controller to import a class from the jar. When I did 'run-app', the compiler complained that it could not find the class.
I saw several example online where people had to add a line to their BuildConfig.groovy like this: runtime 'httpclient:httpclient:3.0.1' . But since I'm not using maven or ivy, how do I get that jar on my classpath?
The example I am referring to above is http://grails.1312388.n4.nabble.com/External-Jar-in-Grails-td4388010.html
Grails 2.0-2.2
Ok, I finally figured out what I need to do. Suppose my jar is named 'mylib.jar'. I need to rename it to mylib-1.0.jar and move it to the lib directory. Then I need to add the following to BuildConfig.groovy:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
// runtime 'mysql:mysql-connector-java:5.1.16'
runtime 'mylib:mylib:1.0'
}
I guess that was obvious to everyone but me. Hope this helps someone else.
Grails 2.3-...
Just put a jar into the lib/. Do not modify BuildConfig.groovy (c) kaskelotti
if your jar is in lib folder and also entry in buildconfig.groovy and if you can't find jar in your build path then try to use below command.
grails compile --refresh-dependencies

Resources