Excude all the unnecessary jar from war file - grails

I am a grails programmer and I want to remove the unnecessary jar files that are included in the war file. There are so many unnecessary jar files included in the war file. Is there any quick solution to do so? Also can you mention the jar files which are necessary to make and run the simple grails war.

You can exclude unnecessary jar from war by adding following lines in BuildConfig.groovy.
grails.war.resources = { stagingDir ->
delete(file:"${stagingDir}/WEB-INF/lib/jarFileName.jar")
}
Example : Removing hibernate core
grails.war.resources = { stagingDir ->
delete(file:"${stagingDir}/WEB-INF/lib/hibernate-core-3.3.1.GA.jar")
}

Related

How to exclude grails plugin jar from war file

Grails 3.2.5. Build.gradle has mail plugin:
compile "org.grails.plugins:mail:2.0.0.RC6"
In deploying a war to production I need to remove javax.mail-1.5.6.jar from WEB-INF/lib since that jar must be in the Tomcat lib when using a JNDI mail resource. So how do I keep the mail plugin but remove the offending jar file from the war? I knew how to do this in Grails 2.x. Via the gradle war task in build.gradle I have tried to exclude the file (doesn't work - the jar drifts in from a plugin somehow), and have tried to filter the file out. When I build the war I get two files - "app-0.1.war" and "app-0.1.war.original". The "original" file has the WEB-INF/lib/javax.mail jar filtered out, but the real, complete war still has it.
So how do I prevent that plugin jar from getting into the war file? Thanks.
One way to do it is with something like this:
war {
rootSpec.exclude '**/javax.mail*.jar'
}
(you may need to adjust depending on whether or not you want to also exclude the javax.mail-api jar file along with the javax.mail jar)
See https://github.com/jeffbrown/excludejar/blob/67734ac0c65cdbead468f1e65bcfc29041cd2279/build.gradle#L70-L72

Add additional files to Grails WAR file

I wanna add additional files, respectively test data file, to the generated WAR file. How can I modify the build.gradle to reach this goal? I have already tried to change the war task, e.g.
war {
from "testdata"
}
but no files were added to the war file. GRAILS 3.1.8 is used.
Here is example code - we use it to copy generated docs to war:
//copy documnetation to war if exist
war {
from('build/docs/manual') {
include '**/*'
into('docs')
}
}

two kind mail.jar with different version after build a war

i have a project where use this jar.
as i know... after build a war, at folder WEB-INF/lib must have :
mail-1.4.1.jar
activation-1.1.jar
mysql-connector-java-5.1.20.jar
but, in folder WEB-INF/lib i see two kind version of mail.jar
in that folder has mail-1.4.3.jar
why?
Run grails dependecy-report in your project, you will know which jar file or dependency is including these 2 different versions of mail then you just need to exclude it from one of the dependency in BuildConfig.groovy.
Something like following
compile('org.apache.kafka:kafka_2.10:0.8.2.1') {
excludes 'slf4j-log4j12'
}
compile("com.amazonaws:aws-java-sdk:1.9.13") {
excludes 'joda-time'
}

How do I add a JAR to the classpath of a Grails project only for test scope?

Normally, I'd declare a test dependency in BuildConfig.groovy for the test scope and know it'll only be in the classpath for testing, and not packaged into the deployed application:
test "org.seleniumhq.selenium:selenium-htmlunit-driver:2.27.0"
I have another JAR I'd like to include at test time, but it's not available in a maven repository, so I can't declare a dependency like I normally would.
How can I add the JAR to my project, check it in, but declare it only available at test time?
I'm currently working in Grails 2.1.1, but 2.3.x is in the future.
Thanks.
I'm pretty sure that all jars in the lib folder are compile scoped, so you cannot change it to test.
What you can do is exclude them in the build process, using the config grails.war.resources in BuildConfig.groovy:
// Remove the JDBC jar before the war is bundled
grails.war.resources = { stagingDir ->
delete(file:"${stagingDir}/WEB-INF/lib/jdbc2_0-stdext.jar")
}
This was taken from the Palmer's post.

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")

Resources