Can't get war.from to work

Hello,

I’m a newb to gradle. I’ve created a task to generate a war. However, I may not understand the sequence. I’m wondering if my from call is working and possibly getting overwritten. I’ve tried to change the locations of the call, I’ve also used different forms of syntax like from/into or into/from. I’ve used ant like criteria and standard a path nothing seems to work. see below.

task buildMyWar(type:War){
doFirst {
removeStuff
}

archiveBaseName = “myWar”
destinationDirectory = file(buildDir)
webAppDirName = file(buildDir)

from ‘war/javascript’
from(‘war/jsp’)
from(‘war/status’)
includes = [‘com.company.xxx/’]
rootSpec.exclude(’
/directory/’)
rootSpec.exclude(’/directory1/’)
rootSpec.exclude(’
/directory2/’)

classpath fileTree(‘war/WEB-INF/lib’)
classpath fileTree(‘lib/runtime’) // adds a file-set to the WEB-INF/lib dir.
webXml = file(‘war/WEB-INF/web.xml’)
}

any suggestions would be greatly appreciated.

Hi,

Instead of defining your own packaging task, it might be easier to use the war plugin and its own packaging task to achieve the same thing using conventions over configuration:

plugins {
	id 'war'
}

repositories {
	mavenCentral()
}

dependencies {
	runtimeOnly 'ch.qos.logback:logback-classic:1.2.3'
}

tasks.named('war', War).configure { War war ->
	war.archiveBaseName = 'myWar'
	war.from 'war'
}
me@laptop:/tmp/foobar$ unzip -t build/libs/myWar.war 
Archive:  build/libs/myWar.war
    testing: META-INF/                OK
    testing: META-INF/MANIFEST.MF     OK
    testing: WEB-INF/                 OK
    testing: WEB-INF/lib/             OK
    testing: WEB-INF/lib/logback-classic-1.2.3.jar   OK
    testing: WEB-INF/lib/logback-core-1.2.3.jar   OK
    testing: WEB-INF/lib/slf4j-api-1.7.25.jar   OK
    testing: jsp/                     OK
    testing: jsp/some.jsp             OK
    testing: javascript/              OK
    testing: javascript/some.js       OK
    testing: WEB-INF/web.xml          OK
    testing: status/                  OK
No errors detected in compressed data of build/libs/myWar.war.

Thank you for your response Pierre. I like your solution, but I wanted to have more than one task to package the war in 2 different ways. I was, however, able to get the from to work, but I had to specify the include directive. Thanks again !