From 5a4efc1c580592bf90eb9161207f002b3174d5d6 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 7 Apr 2022 13:52:09 -0500 Subject: [PATCH] HHH-15193 - Fix Gradle plugin - forcing dependency on hibernate-core SNAPSHOT --- .../hibernate-gradle-plugin.gradle | 4 +- .../tooling/gradle/HibernateOrmPlugin.java | 26 +++---------- .../orm/tooling/gradle/HibernateOrmSpec.java | 39 ++++++++++++++++++- .../gradle/enhance/EnhancementTask.java | 11 +++--- .../metamodel/JpaMetamodelGenerationTask.java | 9 +++-- 5 files changed, 56 insertions(+), 33 deletions(-) diff --git a/tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle b/tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle index 1c3fd73fc3..0cd025717a 100644 --- a/tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle +++ b/tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle @@ -76,11 +76,11 @@ task publish { } processResources { + inputs.property( "orm-version", getVersion() ) filter( ReplaceTokens, tokens: [ 'hibernateVersion': getVersion() ] ) } - -tasks.withType( GroovyCompile ) { +tasks.withType( JavaCompile ) { options.encoding = 'UTF-8' } diff --git a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmPlugin.java b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmPlugin.java index 356334eb67..f00d69c07a 100644 --- a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmPlugin.java +++ b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmPlugin.java @@ -8,11 +8,7 @@ package org.hibernate.orm.tooling.gradle; import org.gradle.api.Plugin; import org.gradle.api.Project; -import org.gradle.api.artifacts.Configuration; import org.gradle.api.plugins.JavaPlugin; -import org.gradle.api.plugins.JavaPluginConvention; -import org.gradle.api.tasks.SourceSet; -import org.gradle.api.tasks.SourceSetContainer; import org.hibernate.orm.tooling.gradle.enhance.EnhancementTask; import org.hibernate.orm.tooling.gradle.metamodel.JpaMetamodelGenerationTask; @@ -28,25 +24,15 @@ public class HibernateOrmPlugin implements Plugin { project.getLogger().debug( "Adding Hibernate extensions to the build [{}]", project.getPath() ); final HibernateOrmSpec ormDsl = project.getExtensions().create( HibernateOrmSpec.DSL_NAME, HibernateOrmSpec.class, project ); - final Configuration hibernateOrm = project.getConfigurations().maybeCreate( "hibernateOrm" ); - project.getDependencies().add( - "hibernateOrm", - project.provider( () -> "org.hibernate.orm:hibernate-core:" + HibernateVersion.version ) - ); - project.getConfigurations().getByName( "implementation" ).extendsFrom( hibernateOrm ); - - final JavaPluginConvention javaPluginConvention = project.getConvention().findPlugin( JavaPluginConvention.class ); - assert javaPluginConvention != null; - - final SourceSetContainer sourceSets = javaPluginConvention.getSourceSets(); - final SourceSet mainSourceSet = sourceSets.getByName( SourceSet.MAIN_SOURCE_SET_NAME ); - - EnhancementTask.apply( ormDsl, mainSourceSet, project ); - JpaMetamodelGenerationTask.apply( ormDsl, mainSourceSet, project ); + EnhancementTask.apply( ormDsl, ormDsl.getSourceSetProperty().get(), project ); + JpaMetamodelGenerationTask.apply( ormDsl, ormDsl.getSourceSetProperty().get(), project ); project.getDependencies().add( "implementation", - project.provider( () -> "org.hibernate.orm:hibernate-core:" + ormDsl.getHibernateVersionProperty().get() ) + ormDsl.getHibernateVersionProperty().map( (ormVersion) -> Character.isDigit( ormVersion.charAt( 0 ) ) + ? "org.hibernate.orm:hibernate-core:" + ormVersion + : null + ) ); } } diff --git a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmSpec.java b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmSpec.java index fd238be553..1b349828ae 100644 --- a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmSpec.java +++ b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmSpec.java @@ -12,7 +12,9 @@ import org.gradle.api.Action; import org.gradle.api.Project; import org.gradle.api.plugins.ExtensionAware; import org.gradle.api.plugins.ExtensionContainer; +import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.provider.Property; +import org.gradle.api.tasks.SourceSet; import org.hibernate.orm.tooling.gradle.enhance.EnhancementSpec; import org.hibernate.orm.tooling.gradle.metamodel.JpaMetamodelGenerationSpec; @@ -26,6 +28,8 @@ public abstract class HibernateOrmSpec implements ExtensionAware { public static final String DSL_NAME = HIBERNATE; private final Property hibernateVersionProperty; + private final Project project; + private final Property sourceSetProperty; private final Property supportEnhancementProperty; private final Property supportJpaMetamodelProperty; @@ -34,11 +38,15 @@ public abstract class HibernateOrmSpec implements ExtensionAware { @Inject - @SuppressWarnings( "UnstableApiUsage" ) public HibernateOrmSpec(Project project) { + this.project = project; + hibernateVersionProperty = project.getObjects().property( String.class ); hibernateVersionProperty.convention( HibernateVersion.version ); + sourceSetProperty = project.getObjects().property( SourceSet.class ); + sourceSetProperty.convention( mainSourceSet( project ) ); + supportEnhancementProperty = project.getObjects().property( Boolean.class ); supportEnhancementProperty.convention( true ); @@ -49,6 +57,15 @@ public abstract class HibernateOrmSpec implements ExtensionAware { jpaMetamodelDsl = getExtensions().create( JpaMetamodelGenerationSpec.DSL_NAME, JpaMetamodelGenerationSpec.class, this, project ); } + private static SourceSet mainSourceSet(Project project) { + return resolveSourceSet( SourceSet.MAIN_SOURCE_SET_NAME, project ); + } + + private static SourceSet resolveSourceSet(String name, Project project) { + final JavaPluginExtension javaPluginExtension = project.getExtensions().getByType( JavaPluginExtension.class ); + return javaPluginExtension.getSourceSets().getByName( name ); + } + public Property getHibernateVersionProperty() { return hibernateVersionProperty; } @@ -61,6 +78,26 @@ public abstract class HibernateOrmSpec implements ExtensionAware { hibernateVersionProperty.set( version ); } + public Property getSourceSetProperty() { + return sourceSetProperty; + } + + public void setSourceSet(String name) { + setSourceSet( resolveSourceSet( name, project ) ); + } + + public void setSourceSet(SourceSet sourceSet) { + sourceSetProperty.set( sourceSet ); + } + + public void sourceSet(String name) { + setSourceSet( resolveSourceSet( name, project ) ); + } + + public void sourceSet(SourceSet sourceSet) { + setSourceSet( sourceSet ); + } + public Property getSupportEnhancementProperty() { return supportEnhancementProperty; } diff --git a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/enhance/EnhancementTask.java b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/enhance/EnhancementTask.java index d56048936d..4d05bea375 100644 --- a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/enhance/EnhancementTask.java +++ b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/enhance/EnhancementTask.java @@ -33,16 +33,17 @@ import static org.hibernate.orm.tooling.gradle.HibernateOrmSpec.HIBERNATE; public class EnhancementTask extends DefaultTask { public static final String DSL_NAME = "hibernateEnhance"; - public static void apply(HibernateOrmSpec ormDsl, SourceSet mainSourceSet, Project project) { + public static void apply(HibernateOrmSpec pluginDsl, SourceSet mainSourceSet, Project project) { final EnhancementTask enhancementTask = project.getTasks().create( DSL_NAME, EnhancementTask.class, - ormDsl, + pluginDsl, mainSourceSet, project ); enhancementTask.setGroup( HIBERNATE ); enhancementTask.setDescription( "Performs Hibernate ORM enhancement of the project's compiled classes" ); + enhancementTask.onlyIf( (t) -> pluginDsl.getSupportEnhancementProperty().getOrElse( true ) ); final String compileJavaTaskName = mainSourceSet.getCompileJavaTaskName(); final Task compileJavaTask = project.getTasks().getByName( compileJavaTaskName ); @@ -55,10 +56,8 @@ public class EnhancementTask extends DefaultTask { private final DirectoryProperty outputDirectory; @Inject - @SuppressWarnings( "UnstableApiUsage" ) - public EnhancementTask(HibernateOrmSpec ormSpec, SourceSet mainSourceSet, Project project) { - this.enhancementDsl = ormSpec.getEnhancementSpec(); - + public EnhancementTask(HibernateOrmSpec pluginDsl, SourceSet mainSourceSet, Project project) { + enhancementDsl = pluginDsl.getEnhancementSpec(); javaCompileOutputDirectory = mainSourceSet.getJava().getDestinationDirectory(); outputDirectory = project.getObjects().directoryProperty(); diff --git a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/metamodel/JpaMetamodelGenerationTask.java b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/metamodel/JpaMetamodelGenerationTask.java index 580d8cd577..df4d142f02 100644 --- a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/metamodel/JpaMetamodelGenerationTask.java +++ b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/metamodel/JpaMetamodelGenerationTask.java @@ -297,7 +297,7 @@ public class JpaMetamodelGenerationTask extends DefaultTask { } @SuppressWarnings( "UnstableApiUsage" ) - public static void apply(HibernateOrmSpec ormDsl, SourceSet mainSourceSet, Project project) { + public static void apply(HibernateOrmSpec pluginDsl, SourceSet mainSourceSet, Project project) { final String mainCompileTaskName = mainSourceSet.getCompileJavaTaskName(); final JavaCompile mainCompileTask = (JavaCompile) project.getTasks().getByName( mainCompileTaskName ); final Task compileResourcesTask = project.getTasks().getByName( "processResources" ); @@ -305,13 +305,14 @@ public class JpaMetamodelGenerationTask extends DefaultTask { final JpaMetamodelGenerationTask genTask = project.getTasks().create( DSL_NAME, JpaMetamodelGenerationTask.class, - ormDsl, + pluginDsl, mainSourceSet, mainCompileTask, project ); genTask.setGroup( HIBERNATE ); genTask.setDescription( "Generates the JPA 'static metamodel'" ); + genTask.onlyIf( (t) -> pluginDsl.getSupportJpaMetamodelProperty().getOrElse( true ) ); genTask.dependsOn( mainCompileTask ); genTask.dependsOn( compileResourcesTask ); @@ -324,8 +325,8 @@ public class JpaMetamodelGenerationTask extends DefaultTask { genTask.finalizedBy( compileJpaMetamodelTask ); mainCompileTask.finalizedBy( compileJpaMetamodelTask ); compileJpaMetamodelTask.dependsOn( genTask ); - compileJpaMetamodelTask.source( project.files( ormDsl.getJpaMetamodelSpec().getGenerationOutputDirectory() ) ); - compileJpaMetamodelTask.getDestinationDirectory().set( ormDsl.getJpaMetamodelSpec().getCompileOutputDirectory() ); + compileJpaMetamodelTask.source( project.files( pluginDsl.getJpaMetamodelSpec().getGenerationOutputDirectory() ) ); + compileJpaMetamodelTask.getDestinationDirectory().set( pluginDsl.getJpaMetamodelSpec().getCompileOutputDirectory() ); compileJpaMetamodelTask.setClasspath( project.getConfigurations().getByName( "runtimeClasspath" ).plus( mainSourceSet.getRuntimeClasspath() ) );