8

I have the following and thought it was 'adding' to my sourceSet but actually just modified it..

sourceSets {
    test {
        resources {
            srcDirs = ["src/main/java"]
            includes = ["**/*.html"]
        }
    }
}

What I really want is both src/test/resources/** and the above as well. I don't want to exclude any files from src/test/resources though and the above is only including html from any directories I put there.

thanks, Dean

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
  • Try += operator instead of plain = – Jk1 Jul 15 '16 at 18:05
  • @Jk1 I am not sure I undertsand. srcDirs += ["src/main/java"] ...ok, but when I do includes = ["**/*.html"] which would start excluding other stuff in src/test/resources that I want used, right? I also don't want includes += ["**/*.html"] as that would imply **/* + **/*.html which doesn't seem useful I though? – Dean Hiller Aug 18 '16 at 00:16
  • @Jk1 perhaps you could elaborate with an example though? – Dean Hiller Aug 18 '16 at 00:16
  • @DeanHiller so, just to clarify, you want all html files from anywhere, aswell as everything below src/main/java and everything below src/test/resources? – Ben Green Aug 22 '16 at 14:10

5 Answers5

6

The following will illustrate the technique using main (so it can be verified):

apply plugin: 'java'

sourceSets {
    myExtra {
        resources {
            srcDirs "src/main/java"
            includes = ["**/*.html"]
        }
    }
    main {
        resources {
            source myExtra.resources
        }
    }
}

Proof of concept via the command-line:

bash$ ls src/main/java
abc.html
xyz.txt

bash$ ls src/main/resources/
def.html
ijk.txt

bash$ gradle clean jar
bash$ jar tf build/libs/myexample.jar
META-INF/
META-INF/MANIFEST.MF
abc.html
def.html
ijk.txt

In your case, change main to test. This answer was discovered via the Gradle doc for SourceDirectorySet. Interestingly, for 3.0, it contains a TODO:

TODO - configure includes/excludes for individual source dirs

which implies that this work-around (via this method) is probably necessary.

Michael Easter
  • 23,733
  • 7
  • 76
  • 107
  • 1
    grrrrrr, shucks @Michael Easter, it appeared to work great but broke the ./gradlew eclipse command with duplicate src/main/resources entries. It nearly worked. The gradle plugin still works but the gradle plugin is broken in that it doesn't port the -parameters setting to make sure params are compiled into the class files....just my luck :(. The answer works for pure gradle though at least. Anyone else, be careful as it may break your other stuff until gradlew eclipse is fixed. – Dean Hiller Sep 04 '16 at 04:36
2

I got your point. I tried this and it worked . Please take a look into it:

sourceSets {
    test {
        resources {
            srcDirs = ["src/main/java"]
            includes = ["**/*.html"]
        }
    }
}

sourceSets.test.resources.srcDir 'src/test/resources'

Add these in build.gradle.

Jince Martin
  • 301
  • 1
  • 9
  • 18
  • @VyacheslavShvets what is the error you are getting? – Jince Martin Aug 26 '16 at 11:07
  • I take that back. This solution does NOT work as it ends up only including *.html files from src/test/resources. I wanted src/test/resources/**** and src/main/java/**/*.html but this solution does not do that. – Dean Hiller Aug 27 '16 at 17:30
1

I was thinking whether or not to post this answer. So that if you are not satisfied with the previous answer, try the following hacky way (probably it will work with eclipse command):

apply plugin: 'java'

ConfigurableFileTree.metaClass.getAsSource = {
  def fileTrees = delegate.asFileTrees
  fileTrees.metaClass.getSrcDirTrees = {
    return delegate as Set
  }
  fileTrees as SourceDirectorySet
}

sourceSets {
  main {
    resources {
      srcDirs = []  // cleanup first
      source fileTree('src/main/java').include('**/*.html').asSource
      source fileTree('src/main/resources').asSource
    }
  }
}
Vyacheslav Shvets
  • 1,735
  • 14
  • 23
1

Using srcDir may be what you want. Here's an example:

sourceSets {
    main {
        resources {
            srcDir "src/main/resources"
            exclude "file-to-be-excluded"
            include "file-to-be-included"

            srcDir "src/main/java"
            include "**/*.html"

            srcDir "image-folder-in-root"
            include "**/*.png"
            include "**/*.jpg"
            exclude "**/*.xcf"
        }
    }
}
Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29
0

Not exactly what you asked for, but it could be helpful for someone who finds this question:

I only wanted to have the test resources next to the sources. So I only need to exclude the sources. In your case, perhaps you could exclude those which you would mind getting in the JAR and/or classpath.

None of the other answers worked for me, and this did work:

sourceSets {
    test {
        resources {
            srcDirs += "src/test/kotlin"
            excludes = ["**/*.kt"]
        }
    }
}

Gradle 6.3.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277