Gradle: How to exclude an entire directory except one file? - gradle

I am trying to exclude all code in package foo.bar except for class MyClass for a certain Gradle subproject.
I tried to add this in build.gradle:
sourceSets {
main {
java {
exclude '**/foo/bar/**'
include '**/foo/bar/MyClass'
}
}
}
But it does not work.
The related question gradle: Exclude all - except one file also does not work, because I am not trying to exclude everything, but only the contents of a certain package.

Related

Using gradle to programmatically add resource folder to test compile/runtime classpath

We have a master build script for 60+ components. The individual components do not have build.gradle files. What I'm trying to do is programmatically (in the master build.gradle) add a resource folder to certain projects. This resource folder contains a file which must be in the classpath when unit tests are ran. I'm trying to add this in the subprojects block like this:
subprojects { proj ->
...
// this is the folder I need in the test task classpath
def resdir = sprintf("%s\\resources", project(':Common').projectDir)
sourceSets {
test {
java {
srcDir 'test'
}
resources {
srcDirs = [resdir]
}
}
}
}
...
if(proj.name == "APROJECT"){
proj.tasks['test'].getClasspath().each {
logger.info "COMPILE CLASSPATH: {}", it
}
}
}
However, if I query the classpath of the test task (see above) I do not see the folder in the classpath. Additionally, of course, the test is failing because the folder is not in the classpath.
If I put the sourceSet update in a build.gradle in the component folder, it works as expected.
Am I doing something wrong? How can I get this folder into the classpath for testing?
I wasn't able to get this to work by dynamically updating the sourceSet, however, I was able to get it to work by adding the necessary resource path to the testCompile dependency. See this for adding a class folder to a dependency.
Update: It's still not an ideal solution since the "solution" only adds the class folder to the compile path, it doesn't treat it as a resource (e.g., copy it to the runtime class folder).
Update #2: It's actually working as expected. It turns out that different tests were referencing slightly different resource paths. Adding all resource paths dynamically as noted above works fine!

Intellij gradle package name incorrect error

I have just opened this (https://github.com/codetojoy/easter_eggs_for_gradle/tree/master/egg_StackOverflow_51685286) project in IntelliJ with the following files:
src/net/codetojoy/Foo.java
src/net/codetojoy/service/FooService.java
src/net/codetojoy/tests/FooServiceTestCase.java
and build.gradle contains a configuration for sourceSets like so:
sourceSets {
main {
java {
srcDir 'src'
exclude 'net/codetojoy/tests/*'
}
}
}
sourceSets.test.java.srcDir 'src/net/codetojoy/tests'
and On the FooServiceTestCase.java file, I am getting an error on the package line saying Package name 'net.codetojoy.tests' does not correspond to the file path.
I think it is because of the customised source and test set. But I am unsure how to fix it....
please help
The test source set definition needs to look more like the main one:
sourceSets {
main {
java {
srcDir 'src'
exclude 'net/codetojoy/tests/*'
}
}
test {
java {
srcDir 'src'
include 'net/codetojoy/tests/*
}
}
The reason the previous answer worked in command line but not the IDE is that for the Java compiler, the location of a source file is irrelevant. The package instruction is used to correctly place the produced class file, but the source file does not have to be in a matching directory structure. But usually an IDE performs this check.

Gradle: exclude specific directory from testRuntime classpath

One of the unit tests picks up a wrong resource file because there's another directory in the classpath where a file exists with the same name.
The unwelcome directory is part of 'rootProject.sourceSets.main.runtimeClasspath'. (there are many sub projects, with circular dependencies. Some of which these unit tests depend on hence the reason for using dependency this way)
How do I exclude such directory from testRuntime class path?
Here's something that worked.
Add 'runtimeClasspath -= ' entry in the build script.
sourceSets {
test {
java {
//...
}
resources {
//...
}
runtimeClasspath -= files("/dir/you/want/to/exclude")
}

Filter class files from compiled sourcesets to be added in a JAR in Gradle/Groovy

I'm trying to include the compiled .class files from Project1 into the jar for Project2 since my project structure requires it to be done. For that, in the build.gradle for Project2, I write :
jar {
from project(':Project1').sourceSets.main.output.classesDir
}
Which successfully does what I had to do. But, I now want to filter some of the classes that are added based on path and/or some pattern. For example, to include only delegate files, I tried this :
jar {
from project(':Project1').sourceSets.main.output.classesDir {
include '**/*Delegate*.class'
}
}
But unfortunately it doesn't work. Is there a way to achieve this in Gradle/Groovy?
Using Gradle 2.12, the following works for me (this is build.gradle for Project 2):
task myBuild(type: Jar) {
baseName "myjar"
from project(':Project1').sourceSets.main.output.classesDir,
{ include "**/*Delegate*.*" }
}
From the doc for Jar.from, note that it takes 2 arguments (hence, the comma is used).
Thanks Michael
Although I got my answer as well, I was just missing a parantheses. The correct and working code goes something like this :
jar {
from (project(':Project1').sourceSets.main.output.classesDir) {
include '**/*Delegate*.class'
}
}

Need a gradle jar task that copies classes normally and custom files

In my Gradle build, after the classes are compiled, I need to create a jar containing the classes in the typical location, but I also need the jar to include a set of other plain text files from a specific subdirectory of the project, and going into a different named folder in the jar.
So, for instance, at the root of the jar, I'll have the "com" folder that starts the classes tree, but I'll also have a "META-INF" folder which contains a custom subfolder name, containing the files copied from the project.
Update:
I tried adding the following:
jar {
from ("src/main/resources/yang") {
into ("META-INF/yang")
}
}
This comes close to working properly. It does copy all the files from that "from" folder" to the "into" folder in the resulting jar. However, the resulting jar ALSO has a "yang" folder containing the same files from "src/main/resources/yang". So, how do I prevent the default copying of that folder?
I might end up doing this as part of a custom plugin, but for now I'd like to see if I can configure a simple "jar" task to do this.
Update:
Based on the solution, the following worked:
jar {
exclude "yang"
from ("src/main/resources/yang") {
into ("META-INF/yang")
}
}
As far as I understood build.gradle can be modified in the following way:
apply plugin: 'java'
jar {
from('other') {
into('META-INF/other')
}
}
Full demo can be found here
UPDATE
This should work:
apply plugin: 'java'
jar {
from('src/main/resources/other') {
into('META-INF/other')
}
exclude 'other/**'
}

Resources