Gradle: Overwrite file in WAR - gradle

I'm trying to replace a file in WAR file generated with Gradle.
Files structure:
- nodes
- staging
- localConfig.yml
- logback.groovy
- grails-app
- conf
- application.yml
- logback.groovy
I want to copy files from nodes/staging to WEB-INF/classes in the final WAR when I execute gradle script with parameter -Pnode=staging.
This is my approach:
war {
if (project.hasProperty('node')) {
from("stacks/${project.node}") {
include('localConfig.yml')
include('logback.groovy')
into('WEB-INF/classes')
}
}
}
This gradle script will copy localConfig.yml to WEB-INF/classes, however the logback.groovy is not replaced.
How can I set up gradle to replace duplicate files instead of keeping the original ones?

I think you'll need to use the classpath method to add to WEB-INF/classes
eg:
war {
if (project.hasProperty('node')) {
classpath "stacks/${project.node}/localConfig.yml", "stacks/${project.node}/logback.groovy"
}
}
Or perhaps
war {
if (project.hasProperty('node')) {
classpath fileTree("stacks/${project.node}")
}
}

Related

Gradle - Copy folder into jar

In my gradle.build I have the following code in order to copy the content of a folder into the jar during the build process
sourceSets {
main {
resources {
srcDirs = ["build/swagger-ui-myapp/"]
}
output.resourcesDir = "build/resources/main/swagger-ui-myapp"
}
}
The content of the folder ends up in the BOOT-INF/classes/ folder in the jar. But I don't want the content of the folder in that directory, I want the files to be inside the original folder, so I want them inside BOOT-INF/classes/swagger-ui-myapp.
How can I achieve this?
I wouldn't change the resources to do that. instead, I would just configure the bootJar task:
bootJar {
dependsOn(taskWhichGeneratesTheBuildSwaggerUiMyapp)
bootInf {
into("classes/swagger-ui-myapp") {
from("$buildDir/swagger-ui-myapp")
}
}
}

Gradle jar doLast

How to make a copy in the resources/app folder. After the other module is compiled
jar {
dependsOn(project(':portal-frontend').tasks.findByName("compileGwt"))
into("app") {
File configFile = file(project(':portal-frontend').tasks.findByName("compileGwt").buildDir)
println configFile.listFiles()
from project(':portal-frontend').tasks.findByName("compileGwt").buildDir
//from compileGwt.buildDir
}
}
}
I have 2 submodule portal-backend and portal-frontend
portal-backend - spring boot project
portal-frontend - gwt project
So I want. When portal-frontend compile. Copy build resource from portal-frontend to portal-ackend
Something like this https://gist.github.com/marcokrikke/5481001
But I have multimodule structure

How to include all files from jar into war using Gradle

I was trying plugins:
io.freefair.war-overlay - but it just overlay one war to another,
waroverlay - it has appropriate option "includeWarJars true" but it
does't works for me.
Currently I'm trying write script:
dependencies {
/* jar, his files I would like to include in war */
compile project(':my_jar_1')
}
war {
/* Step 1. Copy all from my_jar_1 into WEB-INF/classes */
into('WEB-INF/classes') {
from configurations.compile
.filter { it.name.startsWith("my_jar_1") }
.collect { zipTree(it).matching { exclude 'META-INF/**/*.*' }}
}
/* Step 2. Deleting jar from war WEB-INF/lib. I got stuck here. It println, but doesn't delete jar */
doLast {
zipTree('build/libs/war_1-0.0.0.war').files.each {
if (it.path.endsWith('.jar')) {
delete it
println 'DELETED ' + it.path
}
}
}
}
Could somebody tell me how to make it work?
Or maybe smb know more elegant solution?
Also I was trying to declare my own configuration
configurations { overlay }
dependencies {
overlay project(':my_jar_1')
}
war {
into('WEB-INF/classes') {
from configurations.overlay
...
But it shows error
FAILURE: Build failed with an exception.
What went wrong: Failed to capture snapshot of input files for task 'war' property 'rootSpec$1$1' during up-to-date check.
Failed to create MD5 hash for file '/home/user/projects/OveralJarToWar/my_jar_1/build/libs/my_jar_1-1.0-SNAPSHOT.jar'.
The content of WEB-INF/lib and WEB-INF/classes is configured by single property classpath of war task. According to documentation:
Any JAR or ZIP files in this classpath are included in the WEB-INF/lib directory. Any directories in this classpath are included in the WEB-INF/classes directory
So, in your case, the classpath should be modified as follow
war {
def myJar = project(':my_jar_1').jar.outputs
def myClassesAndResources = project(':my_jar_1').sourceSets.main.output
classpath = classpath - myJar + myClassesAndResources
}

Gradle JAR - not exporting package

I'm creating a jar file using gradle that contains a single class file. However, when I go to use my jar file I get:
unable to resolve class pvx.core.util.DesEncrypter
on the import statement (running from command line or in eclipse) If I create a jar file for the class from the command line everything is okay. The only difference that I could detect between the 2 jar files - is the one from the command line has a MANIFEST.MF with permissions of 755 and the gradle generated has permissions of 644.
task utilJar (dependsOn:classes, type: Jar) {
archiveName "DesEncrypter-${version}.jar"
manifest { attributes "Created-By": "1.8.0_60 (Oracle Corporation)" }
into ('.') {
from "$buildDir/classes/main"
include "pvx/core/util/DesEncrypter.class"
}
}
It should rather be:
task utilJar(dependsOn:classes, type: Jar) {
archiveName "DesEncrypter-${version}.jar"
manifest { attributes "Created-By": "1.8.0_60 (Oracle Corporation)" }
from("$buildDir/classes/main") {
include "pvx/core/util/DesEncrypter.class"
}
}

Gradle: ear deploymentDescriptor - Exclude module from application.xml

I have the following in my gradle build file. My problem is that log4j.properties is being added as an ejb module in application.xml, despite my attempt to remove it from the xml per the thread here:
http://forums.gradle.org/gradle/topics/ear_plugin_inserts_unneeded_ejb_modules_in_the_application_xml_ear_descriptor
apply plugin: 'ear'
ear {
deploymentDescriptor {
applicationName = 'ourapp'
displayName = 'ourapp'
initializeInOrder = true
//This doesn't work:
withXml { xml ->
xml.asNode().module.removeAll { it.ejb.text.equals('log4j.properties') }
}
}
//Add log4j.properties to ear root
from('../../lib/log4j.properties', 'log4jProperties')
}
dependencies
{
deploy 'javax:javax.jnlp:1.2'
deploy 'com.oracle:ojdbc6:1.6.0'
earlib 'org.apache:apache-log4j:1.2.16'
}
How can I get the gradle to exclude log4j.properties from application.xml?
EDIT
This is causing a failure to start up in my application server (JBoss 6.0.0) because it doesn't know what to do with log4j.properties. I can work around it by manually creating my application.xml file, but that makes for another thing that has to be maintained. Any assistance would be welcome!
This code works for me. You need to protect the 'remove' method with an 'if' block because the closure is called multiple times. I found that the first time its called, log4jNode is set to 'null'.
withXml {
def log4jNode = asNode().module.find { it.ejb.text() == 'log4j.properties' }
if (log4jNode) {
asNode().remove( log4jNode )
}
}

Resources