injecteer
injecteer

Reputation: 20699

Replace a resource file in a WAR file in Gradle 3.5

I scanned the whole internet and found lots of answers, but neither worked for me properly.

I have the 2 files under

src/main/resources/
                  cluster.xml
                  a/
                    cluster.xml

In my war file I want to have the later.

So I tried:

war {
  from( 'src/main/resources' ){
    exclude 'cluster.xml'
    include 'a/cluster.xml'
    into 'WEB-INF/classes'
  }
}

and it put 2 copies of a/cluster.xml into WEB-INF/classes/a and a single cluster.xml into WEB-INF/classes.

I tried also using webInf{} with no success either.

UPDATE:

To make it a bit clear. I have a Grails 3.x / Springboot app. It has the default gradle task bootRun which runs the app in development mode w/o building the war-file. In this case the file resources/cluster.xml is picked from the classpath and the app works like charm.

For the war task I want to replace this single file, without breaking the bootRun task and keeping the whole config as simple as possible.

Upvotes: 1

Views: 2019

Answers (3)

nickb
nickb

Reputation: 59699

To only modify the WAR, you'll need to access the rootSpec and perform some logic against the cluster.xml files.

war {
    rootSpec.includeEmptyDirs = false
    rootSpec.filesMatching('**/cluster.xml') { details ->
       if (details.sourcePath == "cluster.xml") {
           details.exclude()
       }
       if (details.sourcePath == "a/cluster.xml") {
           details.path = "WEB-INF/classes/cluster.xml"
       }
    }
}

Upvotes: 3

lance-java
lance-java

Reputation: 27976

Rather than relying on exclude which can be confusing, have you considered combining multiple FileTrees together? I much prefer adding two roots together rather than hacking excludes and moves within a single root

eg

- src/main/resources - Common stuff goes here
- src/a/resources/cluster.xml - Version A of cluster.xml
- src/b/resources/cluster.xml - Version B of cluster.xml

Once you've separated into different directories, you can add together the FileTrees that you need in a given situation. IMHO this is much better than using an exclude

Upvotes: 1

ToYonos
ToYonos

Reputation: 16833

Try this :

sourceSets.main.resources.exclude '**/cluster.xml'

war {
  from( 'src/main/resources' ){
    exclude 'cluster.xml'
    include 'a/cluster.xml'
    into 'WEB-INF/classes'
  }
}

Upvotes: 2

Related Questions