1

I'm using gradle to build a Spring Boot application, and I would like to have the application.properties file removed from the war, because that will be loaded externally (this is running in a tomcat container, not embedded).

I've looked around StackOverflow and the Gradle docs to try to figure out what to do, but I don't know which phase to tie into, and if I exclude the file before or after the war is created. There also seem to be multiple ways of dealing with files.

I believe Maven uses packagingExcludes for the equivalent.

mnd
  • 2,709
  • 3
  • 27
  • 48
  • Why are you trying to remove the `application.properties`? You can in any case overwrite them by specifying an external location when you run it in production. Or do you want it to fail if properties can't be found? Cause even if you remove them I'm pretty sure your app will start and just use default values instead. – daniel.eichten Aug 07 '15 at 07:58
  • As far as I know, the ability to overwrite the properties file with an external properties file is only supported in the standalone/embedded Tomcat, where you just run the application as a far jar (or war). But when running in an external container, that functionality is lost. But if you know of a way to do this in Tomcat, I'd be interested in hearing how. Thanks. – mnd Aug 07 '15 at 14:29
  • I can't confirm/refute that external config is not working in an external container. What is not working as of the documentation (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files) is to use command line or environment params/args. But I also don't see a reason why external configs should not work. Did you try to set `spring.config.location` through JNDI `java:comp/env` as explained in the documentation? – daniel.eichten Aug 07 '15 at 14:48

2 Answers2

1

Although I was not able to prevent a file from being added to the war, I was able to remove a file after the war was created - thanks in part to a tip from this question: Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?

In my build.gradle file I appended the war command with an exec command so that I could run a command after the war file had been created. The command will remove the application.properties file from the war. This is what the task extension looks like:

war << {
    exec {
        workingDir 'build/libs'
        commandLine 'zip', '-d', "${appName}-${appVersion}.war", 'WEB-INF/classes/application.properties'
    }
}

In short, it changes the working directory to the location that gradle places the war, and then uses the zip command to remove a file from the war.

Community
  • 1
  • 1
mnd
  • 2,709
  • 3
  • 27
  • 48
  • This is the only thing that worked on an older project using Gradle 3.5. The "rootSpec.exclude" that other answers talk about was probably bugged in older Gradle versions - would not even touch WEB-INF files. – Valters Vingolds Jul 01 '21 at 08:53
1

You also have the option that if you don’t care for these properties files to be copied from the src/main/resources to the build/resources/main folder (not just excluded when build/resources/main is copied to the War), you could use:

In build.gradle file;

processResources {
    exclude('application.properties')
}
Chamith
  • 69
  • 10