jayware / gradle-osgi-ds

Easy to use gradle plugin to generate Declarative Services XML files
Apache License 2.0
2 stars 3 forks source link

Plugin may throw ClassNotFoundException when processing test classes #3

Closed davidfra closed 7 years ago

davidfra commented 7 years ago

The plugin processes files from all compile tasks. This includes Test classes. This is because of this code block:

project.getTasks().withType(AbstractCompile.class).forEach(
    //...
    task.input.add(project.files(compileTask));
);

In my test cases I have a dependency to easymock which is declared as a testCompile dependency. This dependency is not added to the org.apache.felix.scrplugin.Project as the plugin only adds compile dependencies.

So I get this exception

Caused by: java.lang.NoClassDefFoundError: org/easymock/EasyMockSupport
    at org.apache.felix.scrplugin.helper.ClassScanner.scanSources(ClassScanner.java:144)
    at org.apache.felix.scrplugin.SCRDescriptorGenerator.execute(SCRDescriptorGenerator.java:146)
    at org.jayware.gradle.osgi.ds.GenerateDeclarativeServicesDescriptorsTask.generateDeclarativeServicesDescriptors(GenerateDeclarativeServicesDescriptorsTask.java:77)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)

This only happens when I call build (including tests), not when calling assemble (not including tests).

In my opinion, the best way would be to exclude test classes from the processing. Or let the user of the plugin configure which classes to add to the scanning.

For example:

apply plugin: 'osgi-ds'

osgiDs {
   processClassesDir = sourceSets.main.output.classesDir
}
davidfra commented 7 years ago
// A little hack to work around issue: 
def osgiDsTask = project.generateDeclarativeServicesDescriptors
java.lang.reflect.Field field = osgiDsTask.getClass().getSuperclass().getDeclaredField('input')
field.setAccessible(true)
List osgiDsInputs = field.get(osgiDsTask)
osgiDsInputs.clear()
osgiDsInputs.add(sourceSets.main.output)
field.setAccessible(false)