Discussion:
Copy file into WEB-INF, fail if not exists
StormeHawke
2012-05-30 16:07:14 UTC
Permalink
Hi. I've got two questions that are closely related.

One: I'm currently using the following to copy a spring datasource xml file
into WEB-INF as a dependency of the war task:
[code]
task copyDatasource(type: Copy) {//copy our <datasource>.xml into
dpu-datasource.xml
from('datasource/')
into('src/main/webapp/WEB-INF/')
include(datasource + '.xml')
rename(datasource + '.xml', 'dpu-datasource.xml')
}
[/code]

This can be kind of annoying as it leaves the copied file in the WEB-INF dir
after the build, and can cause us to have bad builds if we have typo in the
-Pdatasource=filename when we invoke the build. Is there a way to
dynamically grab the file and directly insert it into the war at build time
without copying it to WEB-INF, such as this that we use for our jar files:

[code]
processResources {
from('/datasource') {
include(datasource + '.xml')
rename(datasource + '.xml', 'intellistat-datasource.xml')
}
}
[/code]
?

On a closely related note, gradle continues to run quietly if the file
called for doesn't exist. I don't know the reason for this, but it
certainly is unexpected behavior considering that if you try to copy a file
in JAVA that doesn't exist you get a big ugly FileNotFoundException. Which
is what we want and expect - If we have a typo, I don't want to accidentally
get a war file that is missing this crucial file (or, as is currently the
case using the above copyDatasource method, the previous build's leftover
file) - makes for annoying duplicate rebuild/reupload to server cycles. Is
this a gradle bug, or is this some kind of deliberate behavior? How do we
get around it?

Suggestions?

--
View this message in context: http://gradle.1045684.n5.nabble.com/Copy-file-into-WEB-INF-fail-if-not-exists-tp5709846.html
Sent from the gradle-user mailing list archive at Nabble.com.
Peter Niederwieser
2012-05-31 00:06:09 UTC
Permalink
Is there a way to dynamically grab the file and directly insert it into
the war at build time without copying it to WEB-INF?
Sure. See the War plugin chapter in the Gradle user guide:
http://gradle.org/docs/current/userguide/userguide_single.html#sec:customizing
On a closely related note, gradle continues to run quietly if the file
called for doesn't exist. I don't know the reason for this, but it
certainly is unexpected behavior considering that if you try to copy a
file in JAVA that doesn't exist you get a big ugly FileNotFoundException.
Is this a gradle bug, or is this some kind of deliberate behavior? How do
we get around it?
I think it is deliberate. One way to get around it is to use `Copy.eachFile
{}` to check which files are actually copied, and to compare that against
your expectations.

--
Peter Niederwieser
Principal Engineer, Gradleware
http://gradleware.com
Creator, Spock Framework
http://spockframework.org
Twitter: @pniederw

--
View this message in context: http://gradle.1045684.n5.nabble.com/Copy-file-into-WEB-INF-fail-if-not-exists-tp5709846p5709850.html
Sent from the gradle-user mailing list archive at Nabble.com.
StormeHawke
2012-05-31 16:35:48 UTC
Permalink
Post by Peter Niederwieser
Is there a way to dynamically grab the file and directly insert it into
the war at build time without copying it to WEB-INF?
http://gradle.org/docs/current/userguide/userguide_single.html#sec:customizing
Thanks, that was the tip I needed. I did the following:

<code>
war {
webInf {
from 'datasource'
include(datasource + '.xml')
rename(datasource + '.xml', 'iq-datasource.xml')

from('security/')
include(security + '.xml')
rename(security + '.xml', 'iq-security.xml')
}
}
</code>
Post by Peter Niederwieser
On a closely related note, gradle continues to run quietly if the file
called for doesn't exist. I don't know the reason for this, but it
certainly is unexpected behavior considering that if you try to copy a
file in JAVA that doesn't exist you get a big ugly FileNotFoundException.
Is this a gradle bug, or is this some kind of deliberate behavior? How do
we get around it?
I think it is deliberate. One way to get around it is to use `Copy.eachFile
{}` to check which files are actually copied, and to compare that against
your expectations.


So since I'm not actually using the Copy() functionality anymore, is there a
way to assert that the requested file exists in the war task? A flag I can
set? A hack I can implement in a doFirst clause?

--
View this message in context: http://gradle.1045684.n5.nabble.com/Copy-file-into-WEB-INF-fail-if-not-exists-tp5709846p5709852.html
Sent from the gradle-user mailing list archive at Nabble.com.
Peter Niederwieser
2012-06-04 21:57:01 UTC
Permalink
Post by StormeHawke
So since I'm not actually using the Copy() functionality anymore, is there
a way to assert that the requested file exists in the war task? A flag I
can set? A hack I can implement in a doFirst clause?
You can perform some assertions in a doFirst block. Something like:

war.doFirst {
assert file("datasource/${datasource}.xml").exists()
}

--
Peter Niederwieser
Principal Engineer, Gradleware
http://gradleware.com
Creator, Spock Framework
http://spockframework.org
Twitter: @pniederw



--
View this message in context: http://gradle.1045684.n5.nabble.com/Copy-file-into-WEB-INF-fail-if-not-exists-tp5709846p5709855.html
Sent from the gradle-user mailing list archive at Nabble.com.
StormeHawke
2012-06-04 22:28:26 UTC
Permalink
Post by Peter Niederwieser
war.doFirst {
assert file("datasource/${datasource}.xml").exists()
}
Thanks Peter, that works like a charm :)

~Brian

--
View this message in context: http://gradle.1045684.n5.nabble.com/Copy-file-into-WEB-INF-fail-if-not-exists-tp5709846p5709856.html
Sent from the gradle-user mailing list archive at Nabble.com.

Loading...