HHH-15707 - Fix Gradle plugin with Kotlin 1.7.0 or higher

Since Kotlin version 1.7.0 the KotlinCompile task no longer extends
Gradle's AbstractCompile.

This commit updates Hibernate Gradle enhancement plugin to not cast to
AbstractCompile and instead use reflection to invoke the
"getDestinationDirectory" method.

It also updates the Kotlin version on used to test the Gradle
enhancement (but remains backwards compatible with previous Kotlin
versions).
This commit is contained in:
Tomas Cerskus 2023-02-04 19:17:32 +00:00 committed by Christian Beikov
parent 0e6c1110fa
commit 07529c309d
1 changed files with 13 additions and 7 deletions

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.orm.tooling.gradle; package org.hibernate.orm.tooling.gradle;
import java.lang.reflect.Method;
import java.util.Set; import java.util.Set;
import org.gradle.api.Action; import org.gradle.api.Action;
@ -69,21 +70,26 @@ public class HibernateOrmPlugin implements Plugin<Project> {
for ( String language : languages ) { for ( String language : languages ) {
final String languageCompileTaskName = sourceSet.getCompileTaskName( language ); final String languageCompileTaskName = sourceSet.getCompileTaskName( language );
final AbstractCompile languageCompileTask = (AbstractCompile) project.getTasks().findByName( languageCompileTaskName ); final Task languageCompileTask = project.getTasks().findByName( languageCompileTaskName );
if ( languageCompileTask == null ) { if ( languageCompileTask == null ) {
continue; continue;
} }
//noinspection Convert2Lambda //noinspection Convert2Lambda
languageCompileTask.doLast( new Action<>() { languageCompileTask.doLast(new Action<>() {
@Override @Override
public void execute(Task t) { public void execute(Task t) {
final DirectoryProperty classesDirectory = languageCompileTask.getDestinationDirectory(); try {
final ClassLoader classLoader = Helper.toClassLoader( sourceSet, project ); final Method getDestinationDirectory = languageCompileTask.getClass().getMethod("getDestinationDirectory");
final DirectoryProperty classesDirectory = (DirectoryProperty) getDestinationDirectory.invoke(languageCompileTask);
EnhancementHelper.enhance( classesDirectory, classLoader, ormDsl, project ); final ClassLoader classLoader = Helper.toClassLoader(sourceSet, project);
EnhancementHelper.enhance(classesDirectory, classLoader, ormDsl, project);
} }
} ); catch (Exception e) {
throw new RuntimeException(e);
}
}
});
} }
} ); } );
} }