Gradle: Disable source compilation for particular folder

756 views
Skip to first unread message

Przemysław Wesołek

unread,
Nov 22, 2016, 8:18:38 AM11/22/16
to bndtools-users
In a Java project I use Immutables library which generates source code via annotation processing (AP). When Eclipse is configured with AP, it generates the code into a separate folder (e.g. .apt_generated). Everything works correctly, but Bndtools warns that this folder is not set as a source path for bnd:

Bndtools: Found source folder '.apt_generated' that is not on bnd's source path 'src'

So I modify the bnd.bnd file:

src: src,.apt_generated

and the warning is gone, and my generated sources land in bundle's OSGI-OPT/src.

However, when now I switch to the console and do a Gradle build, the bnd gradle plugin also passes the information about source files found .apt_generated to javac. Due to seeing the same classfile — once via compilation in .apt_generated, and secondly in effect of applying application processing of source files in src folder, the javac complains:

:annotation.processing.example:compileJava
/home/jest/work/eclipses-ws/apt_generated_bug/annotation.processing.example/bin/annotation/processing/example/ImmutableSampleInterface.java:14: error: duplicate class: annotation.processing.example.ImmutableSampleInterface
public final class ImmutableSampleInterface implements SampleInterface {
             
^
1 error
:annotation.processing.example:compileJava FAILED

FAILURE
: Build failed with an exception.

* What went wrong:
Execution failed for task ':annotation.processing.example:compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED


My question is, how do I configure the thing to achieve:

- compilation from Gradle
- compilation from Eclipse
- sources of annotation processing results in OSGI-OPT/src
- no warning in Bndtools about source path not being added to bnd

From my perspective, it is the Gradle plugin that should be blamed (or instructed better), since bnd itself doesn't compile anything, just copies source folders' contents into OSGI-OPT/src.

Regards,
Przemek

BJ Hargrave

unread,
Nov 22, 2016, 9:23:49 AM11/22/16
to bndtool...@googlegroups.com
For Bnd Workspace model build, the Bnd Gradle plugin configures the Gradle project using the information in the bnd.bnd file for many things including source folders. This is for fidelity to Bndtools so they both have the same source folders, output folders, build path, etc.

For Bndtools, this means that the .classpath file and bnd.bnd must match with respect to source folders and output folders. I suspect the original warning from Bndtools is because you have different source folders in .classpath than in bnd.bnd. So you changed  bnd.bnd to match the source folders in .classpath. So of course the Bnd Gradle project should use the same folders as Bndtools. If you want to do something special here, you will need to write a build.gradle script for the project which modifies the source folders for your project.

See https://github.com/bndtools/bnd/blob/master/biz.aQute.bnd.gradle/src/aQute/bnd/gradle/BndPlugin.groovy#L111-L121. You will need your build.gradle file to set sourceSets.main to different source folders than what is in bnd.bnd (bndProject.getSourcePath()).

--
You received this message because you are subscribed to the Google Groups "bndtools-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
BJ

Przemysław Wesołek

unread,
Nov 23, 2016, 7:26:59 AM11/23/16
to bndtools-users
OK, after wrestling with bnd and Gradle, I concluded with a working build.gradle, which does exactly the same as Eclipse: annotation-processed source files land in .apt_generated, compilation is done only for files in src/, jar task packages bin/ content; what is strange and which I don't understand is how on Earth does bnd knows that the generated class files correspond to sources outside src/ (i.e. in .apt_generated), and correctly (!) bundles .apt_generated content in OSGI-OPT/src?! I'm blad, but surprised. :)

apply plugin: 'java'

sourceSets
{
  main
{
    java
{
      srcDirs
-= [ project.file('.apt_generated') ]
   
}
    resources
{
      srcDirs
-= [ project.file('.apt_generated') ]
   
}
 
}
}

compileJava
{
  options
.compilerArgs.addAll([ '-s', project.file('.apt_generated') ])
}

BJ Hargrave

unread,
Nov 23, 2016, 9:11:59 AM11/23/16
to bndtool...@googlegroups.com
Bnd will package the class files in the folder named by the `bin` property. This is normally the bin folder. I assume that however you compile the generated source in .apt_generated, it places the class files in the bin folder. (You do not mention how you accomplish this in the gradle build.)

Bnd will process the source from the folders names in the `src` property. Since your src property names both src and .apt_generated, and all the class files are in bin, it seems obvious that all the source files would be packages in OSGI-OPT/src.

--
You received this message because you are subscribed to the Google Groups "bndtools-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
BJ

Przemysław Wesołek

unread,
Nov 23, 2016, 4:20:28 PM11/23/16
to bndtools-users
Hm, this doesn't seem the case. I manually created a file in .apt_generated and it didn't land in the bundle.

BJ Hargrave

unread,
Nov 23, 2016, 4:45:42 PM11/23/16
to bndtool...@googlegroups.com
Of course what goes in the bundle depends upon the instructions in the bnd file. But it will look for classes in the configured bin folder since that is where the compiler puts them.

Przemysław Wesołek

unread,
Nov 24, 2016, 3:26:20 AM11/24/16
to bndtools-users
I thought that when I change Gradle's sourceSet it will also impact the jar task (i.e. if I remove .apt_generated from the sourceSet, it will not be added to OSGI-OPT/src).

Anyway, here's the workspace if anyone is interested in testing. Works for me. :)

https://github.com/jest/test-bnd-gradle-apt-generated-folder

Przemek

BJ Hargrave

unread,
Nov 24, 2016, 5:44:59 AM11/24/16
to bndtool...@googlegroups.com
It would impact the jar task expect for Bnd, the Bnd Gradle plugin replaces the jar task actions to use Bnd to build the bundle (which is the main point :-).

I looked at your repo and it contains the generate source files which seems wrong. I would have thought you would not commit build output to the source repo.

Przemysław Wesołek

unread,
Nov 28, 2016, 5:45:16 AM11/28/16
to bndtools-users
My repo contains the generated source files and it is OK — that's the way we manage the code. That's why I assume it is already there when the clean build starts.

Przemek
Reply all
Reply to author
Forward
0 new messages