Replacing default processResources task


(c2665466) #1

I’m trying to replace the actions of the default processResources task, but somehow I can’t get it to work. I tried something like this:

processResources {
    actions = [];
    source = newSourceDirectory
      doLast {
        copy {
            ....
        }
    }
}

But the task is always skipped “as it has no source files”


(Benjamin Muschko) #2

What exactly are you trying to achieve?


(c2665466) #3

I am working on a project where the default processResources task won’t do, since we don’t use a single resources folder, but rather copy our resources from several folders, with some of the files they contain being excluded.

I achieved what I wanted by modifying the srcSets configuration and setting a resources folder. Then I overwrite all the actions of the default processResources task and set my own copy actions.


(Benjamin Muschko) #4

Do I understand this correctly that you solved your issue?

BTW: The task processResources is of type Copy so you don’t need to add an action that uses the copy method. You can simply add your copy definition (from, into) to the task.


(c2665466) #5

Yes, I solved the issue, thank you.

And thanks for the other tip, too, I’ll try that.


(Peter Niederwieser) #6

Instead of configuring the ‘processResources’ task, you could also configure ‘sourceSets.main.resources’, which is generally the preferred approach.


(c2665466) #7

I have to configure the processResources task, since by default the task copies all the files from the resources directory. But I need to exclude some of them.

As to only using “from” and “into” instead of the copy method: Is that possible, if I copy from several folders to several others? My task looks something like this now:

processResources = {
    actions = [];
    doLast {
        copy {
            from(sourceDir1) {
                exclude something
            }
            into(destDir)
        }
        copy {
            from(sourceDir2)
            into(destDir/subDir)
        }
    }
}

How would I do that without using the copy method?


#8

As Peter mentioned, you should be able to do what you want by configuring the sources to be copied, rather than configuring the task. The ‘processResources’ task doesn’t copy everything from a directory, it copies everything in ‘sourceSets.main.resources’, which by default is everything in the resources directory.

Take a look at http://www.gradle.org/docs/nightly/dsl/org.gradle.api.tasks.SourceSet.html and http://www.gradle.org/docs/nightly/javadoc/org/gradle/api/file/SourceDirectorySet.html to get an idea how you could add extra directories to the resources. Something like:

sourceSets {

main {

resources {

srcDirs “some/dir”, “some/other/dir”

exclude “/foo/

}

} }





Default excludes / includes in Jar task
(c2665466) #9

So far, it’s not possible to configure includes/excludes for individual sourceSets, according to the second link.

Let’s look at an example. Suppose my folder structure looks like this:

projectDir/
    dir/
        file1
        file2 // exclude this one
    someSubdir/
        dir/
            file1 // exclude this one
            file2
        dir2/
            file3

My destinationDir is supposed to look like:

destinationDir/
    dir/
        file1 //from first folder
        file2 //from second folder
    dir2/
        file3

Edit: I wrote some nonsense as to how I could possibly achieve this… let me fix this…


(c2665466) #10

If individual include/exclude patterns for the sourceSets were possible, I could do something like this:

sourceSets {
    main {
        resources {
            srcDir "." {
                include "/dir/"
                exclude "**/file2"
            }
            srcDir "someSubdir" {
                exclude "**/file1"
            }
        }
    }
}

But since individual include/excludes aren’t possible ATM, I can’t do that.

Is there a way to achieve what I want by simply modifying the sourceSets? Otherwise I’ll just stick to configuring the task itself.