How can I declare an exclusion on my source folder?

I am converting a large multi-project setup from an Ant-based build to use Gradle. One of the sub-projects is a library used in some GWT-based applications. This particular library project takes advantage of GWT’s super-source feature. As such I need to exclude a directory that exists inside the source folder from being seen by Eclipse.

Based on this I have tried:

apply plugin: 'java'
apply plugin: 'eclipse'

sourceSets {
    main {
        java {
            srcDirs = ['source', 'source-gen']
            exclude 'com/mycompany/project/gwt/**'
        }
    }
}

dependencies {
    ...
}

However, when I import this into Eclipse, BuildShip ignores the exclude.

I tried appending **/*.java and prepending source/ but that made no difference. I tried setting up a different configuration and sourceSet and using eclipse.classpath.minusConfigurations but got an error about nesting directories.

This is what I would expect to see:

<classpath>
	<classpathentry excluding="com/mycompany/project/gwt/**" kind="src" path="source"/>
        ...

But I am actually getting is:

<classpath>
	<classpathentry kind="src" path="source"/>
        ...

Eclipse Neon.2 (4.6.2)
BuildShip 2.0.2
Gradle 3.5

What am I doing wrong?

Thanks in advance,
Dave

I managed to fix my own problem. I added the following to my build.gradle:


eclipse {
    classpath {
        file {
            whenMerged {
                // Find the "source" entry and add the exclude for the folder containing our super-source code
                def src = entries.find { it.path == 'source' }
                src.each {
                	it.excludes = ["com/mycompany/project/gwt/**"]
                }
            }
        }
    }
}

This adds the necessary exclusion where I need it. It feels very inelegant though. If anybody knows of a more declarative way to achieve the same end, I would much appreciate it.

Your initial snippet should work. @donat can you see if you can reproduce this problem?

I’ve done some testing and the functionality seems working. I’ve created a basic project with the following structure

➜  test-source-excludes tree
.
├── build.gradle
├── source
└── source-gen

The content of build.gradle:

apply plugin: 'java'

sourceSets {
    main {
        java {
            srcDirs = ['source', 'source-gen']
            exclude 'com/mycompany/project/gwt/**'
        }
    }
}

When I import the the project with Buildship (using Gradle version 3.5), then the following .classpath file is generated:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry excluding="com/mycompany/project/gwt/**" kind="src" path="source"/>
	<classpathentry excluding="com/mycompany/project/gwt/**" kind="src" path="source-gen"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
	<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
	<classpathentry kind="output" path="bin"/>
</classpath>

@sykesd any idea what can be different in your project configuration?

@donat Thank you so much for taking the time to setup the test project and look at this. Using your test project as a starting point I was able to pinpoint the source of my problem.

In the full scale build.gradle file, I was also declaring a resources section in the sourceSets. The resources overlap the java, i.e. they are in the same directory structure mixed in with the .java source files (this is due to history - if we started again we would probably separate this out). I was not declaring the exclusion in the resources section. So I had:

sourceSets {
    main {
        java {
            srcDirs = ['source', 'source-gen']
            exclude 'com/franke/awe/cubicle/gwt/**'
        }
        resources {
            srcDirs = ['source', 'source-gen']
        }
    }
}

But what I really need is:

sourceSets {
    main {
        java {
            srcDirs = ['source', 'source-gen']
            exclude 'com/franke/awe/cubicle/gwt/**'
        }
        resources {
            srcDirs = ['source', 'source-gen']
            exclude 'com/franke/awe/cubicle/gwt/**'
        }
    }
}

With this in place it works as expected. Sorry I did not include this information in my initial post. At the time I assumed that the resources section would be treated separately. Thinking back on it now, I can see why it also makes sense that it is not.

Thanks again for your help!

1 Like

No problem. I’m glad that you could resolve the issue.