Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include all src/test/resources/** AND src/main/java/**/*.html in the test sourceset in gradle?

Tags:

gradle

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

like image 691
Dean Hiller Avatar asked Jul 15 '16 17:07

Dean Hiller


People also ask

How do I add resources to SRC test?

Right click on maven project --->Click on Build Path ----->Click on New Source Folder. New source folder window will open, give the name to your folder example - src/test/source. click on Finish.

How do you run a test case using Gradle command?

You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.

Which command lets you generate the unit tests of a project developed with Gradle?

We can run our unit tests by using the command: gradle clean test.


3 Answers

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.

like image 80
Michael Easter Avatar answered Nov 16 '22 03:11

Michael Easter


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.

like image 28
Jince Martin Avatar answered Nov 16 '22 02:11

Jince Martin


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
    }
  }
}
like image 45
Vyacheslav Shvets Avatar answered Nov 16 '22 02:11

Vyacheslav Shvets