Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the gradle way to filter a subset of files in src/main/webapp?

Tags:

gradle

war

I'm doing a maven conversion to gradle and want to see the opinions on the best way to perform the following. I currently have multiple files under src/main/webapp. Some need filtered one way and some need filtered in another.

Notionally under src/main/webapp I have a directory foo containing html and binaries and under webapp many other files including html. I want to filter just the foo/*.html files.

In my notional build.gradle I can either do:

war {
  eachFile {
    if(shouldFilter(it)) {
      it.filter(ReplaceTokens, tokens: [key: 'value'])
    }
  }
}

def shouldFilter(input) {
  input.path.contains('foo') && input.name.endsWith('.html')
}

or move each subset into its own directory that is not copied by default

war {
  from('src/main/foo-pre-filter') {
    into 'foo'
    include '*.html'
    filter(ReplaceTokens, tokens: [key: 'value'])
  }
}

Or is there another option I missed?

like image 663
Rob Spieldenner Avatar asked Mar 07 '12 20:03

Rob Spieldenner


People also ask

What is testImplementation in Gradle?

Configuration inheritance and composition For example the testImplementation configuration extends the implementation configuration. The configuration hierarchy has a practical purpose: compiling tests requires the dependencies of the source code under test on top of the dependencies needed write the test class.

What is FileTree in Gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.

What is flatDir in Gradle?

flatDir(configureClosure) Adds an configures a repository which will look for dependencies in a number of local directories. flatDir(args) Adds a resolver that looks into a number of directories for artifacts. The artifacts are expected to be located in the root of the specified directories.

What is Gradle doLast?

The doLast creates a task action that runs when the task executes. Without it, you're running the code at configuration time on every build. Both of these print the line, but the first one only prints the line when the testTask is supposed to be executed.


1 Answers

If I understand the question correctly, you can use filesMatching. Also, I would do it as part of the processResources task, as opposed to the war task. It would look something like this:

processResources {
    filesMatching('foo/*.html') {
        filter(ReplaceTokens, tokens: [key: 'value'])
    }
}

I realize the initial question was asked 2 years ago, so this probably won't help the asker, but perhaps it could help someone else in the future.

like image 107
ajk8 Avatar answered Oct 15 '22 19:10

ajk8