HHH-15193 - Fix Gradle plugin - forcing dependency on hibernate-core SNAPSHOT

This commit is contained in:
Steve Ebersole 2022-04-07 13:52:09 -05:00
parent 43d2274573
commit 5a4efc1c58
5 changed files with 56 additions and 33 deletions

View File

@ -76,11 +76,11 @@ task publish {
} }
processResources { processResources {
inputs.property( "orm-version", getVersion() )
filter( ReplaceTokens, tokens: [ 'hibernateVersion': getVersion() ] ) filter( ReplaceTokens, tokens: [ 'hibernateVersion': getVersion() ] )
} }
tasks.withType( JavaCompile ) {
tasks.withType( GroovyCompile ) {
options.encoding = 'UTF-8' options.encoding = 'UTF-8'
} }

View File

@ -8,11 +8,7 @@ package org.hibernate.orm.tooling.gradle;
import org.gradle.api.Plugin; import org.gradle.api.Plugin;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.plugins.JavaPlugin; 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.enhance.EnhancementTask;
import org.hibernate.orm.tooling.gradle.metamodel.JpaMetamodelGenerationTask; import org.hibernate.orm.tooling.gradle.metamodel.JpaMetamodelGenerationTask;
@ -28,25 +24,15 @@ public class HibernateOrmPlugin implements Plugin<Project> {
project.getLogger().debug( "Adding Hibernate extensions to the build [{}]", project.getPath() ); project.getLogger().debug( "Adding Hibernate extensions to the build [{}]", project.getPath() );
final HibernateOrmSpec ormDsl = project.getExtensions().create( HibernateOrmSpec.DSL_NAME, HibernateOrmSpec.class, project ); final HibernateOrmSpec ormDsl = project.getExtensions().create( HibernateOrmSpec.DSL_NAME, HibernateOrmSpec.class, project );
final Configuration hibernateOrm = project.getConfigurations().maybeCreate( "hibernateOrm" ); EnhancementTask.apply( ormDsl, ormDsl.getSourceSetProperty().get(), project );
project.getDependencies().add( JpaMetamodelGenerationTask.apply( ormDsl, ormDsl.getSourceSetProperty().get(), project );
"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 );
project.getDependencies().add( project.getDependencies().add(
"implementation", "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
)
); );
} }
} }

View File

@ -12,7 +12,9 @@ import org.gradle.api.Action;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.plugins.ExtensionAware; import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.plugins.ExtensionContainer; import org.gradle.api.plugins.ExtensionContainer;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Property; 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.enhance.EnhancementSpec;
import org.hibernate.orm.tooling.gradle.metamodel.JpaMetamodelGenerationSpec; 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; public static final String DSL_NAME = HIBERNATE;
private final Property<String> hibernateVersionProperty; private final Property<String> hibernateVersionProperty;
private final Project project;
private final Property<SourceSet> sourceSetProperty;
private final Property<Boolean> supportEnhancementProperty; private final Property<Boolean> supportEnhancementProperty;
private final Property<Boolean> supportJpaMetamodelProperty; private final Property<Boolean> supportJpaMetamodelProperty;
@ -34,11 +38,15 @@ public abstract class HibernateOrmSpec implements ExtensionAware {
@Inject @Inject
@SuppressWarnings( "UnstableApiUsage" )
public HibernateOrmSpec(Project project) { public HibernateOrmSpec(Project project) {
this.project = project;
hibernateVersionProperty = project.getObjects().property( String.class ); hibernateVersionProperty = project.getObjects().property( String.class );
hibernateVersionProperty.convention( HibernateVersion.version ); hibernateVersionProperty.convention( HibernateVersion.version );
sourceSetProperty = project.getObjects().property( SourceSet.class );
sourceSetProperty.convention( mainSourceSet( project ) );
supportEnhancementProperty = project.getObjects().property( Boolean.class ); supportEnhancementProperty = project.getObjects().property( Boolean.class );
supportEnhancementProperty.convention( true ); supportEnhancementProperty.convention( true );
@ -49,6 +57,15 @@ public abstract class HibernateOrmSpec implements ExtensionAware {
jpaMetamodelDsl = getExtensions().create( JpaMetamodelGenerationSpec.DSL_NAME, JpaMetamodelGenerationSpec.class, this, project ); 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<String> getHibernateVersionProperty() { public Property<String> getHibernateVersionProperty() {
return hibernateVersionProperty; return hibernateVersionProperty;
} }
@ -61,6 +78,26 @@ public abstract class HibernateOrmSpec implements ExtensionAware {
hibernateVersionProperty.set( version ); hibernateVersionProperty.set( version );
} }
public Property<SourceSet> 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<Boolean> getSupportEnhancementProperty() { public Property<Boolean> getSupportEnhancementProperty() {
return supportEnhancementProperty; return supportEnhancementProperty;
} }

View File

@ -33,16 +33,17 @@ import static org.hibernate.orm.tooling.gradle.HibernateOrmSpec.HIBERNATE;
public class EnhancementTask extends DefaultTask { public class EnhancementTask extends DefaultTask {
public static final String DSL_NAME = "hibernateEnhance"; 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( final EnhancementTask enhancementTask = project.getTasks().create(
DSL_NAME, DSL_NAME,
EnhancementTask.class, EnhancementTask.class,
ormDsl, pluginDsl,
mainSourceSet, mainSourceSet,
project project
); );
enhancementTask.setGroup( HIBERNATE ); enhancementTask.setGroup( HIBERNATE );
enhancementTask.setDescription( "Performs Hibernate ORM enhancement of the project's compiled classes" ); 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 String compileJavaTaskName = mainSourceSet.getCompileJavaTaskName();
final Task compileJavaTask = project.getTasks().getByName( compileJavaTaskName ); final Task compileJavaTask = project.getTasks().getByName( compileJavaTaskName );
@ -55,10 +56,8 @@ public class EnhancementTask extends DefaultTask {
private final DirectoryProperty outputDirectory; private final DirectoryProperty outputDirectory;
@Inject @Inject
@SuppressWarnings( "UnstableApiUsage" ) public EnhancementTask(HibernateOrmSpec pluginDsl, SourceSet mainSourceSet, Project project) {
public EnhancementTask(HibernateOrmSpec ormSpec, SourceSet mainSourceSet, Project project) { enhancementDsl = pluginDsl.getEnhancementSpec();
this.enhancementDsl = ormSpec.getEnhancementSpec();
javaCompileOutputDirectory = mainSourceSet.getJava().getDestinationDirectory(); javaCompileOutputDirectory = mainSourceSet.getJava().getDestinationDirectory();
outputDirectory = project.getObjects().directoryProperty(); outputDirectory = project.getObjects().directoryProperty();

View File

@ -297,7 +297,7 @@ public class JpaMetamodelGenerationTask extends DefaultTask {
} }
@SuppressWarnings( "UnstableApiUsage" ) @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 String mainCompileTaskName = mainSourceSet.getCompileJavaTaskName();
final JavaCompile mainCompileTask = (JavaCompile) project.getTasks().getByName( mainCompileTaskName ); final JavaCompile mainCompileTask = (JavaCompile) project.getTasks().getByName( mainCompileTaskName );
final Task compileResourcesTask = project.getTasks().getByName( "processResources" ); final Task compileResourcesTask = project.getTasks().getByName( "processResources" );
@ -305,13 +305,14 @@ public class JpaMetamodelGenerationTask extends DefaultTask {
final JpaMetamodelGenerationTask genTask = project.getTasks().create( final JpaMetamodelGenerationTask genTask = project.getTasks().create(
DSL_NAME, DSL_NAME,
JpaMetamodelGenerationTask.class, JpaMetamodelGenerationTask.class,
ormDsl, pluginDsl,
mainSourceSet, mainSourceSet,
mainCompileTask, mainCompileTask,
project project
); );
genTask.setGroup( HIBERNATE ); genTask.setGroup( HIBERNATE );
genTask.setDescription( "Generates the JPA 'static metamodel'" ); genTask.setDescription( "Generates the JPA 'static metamodel'" );
genTask.onlyIf( (t) -> pluginDsl.getSupportJpaMetamodelProperty().getOrElse( true ) );
genTask.dependsOn( mainCompileTask ); genTask.dependsOn( mainCompileTask );
genTask.dependsOn( compileResourcesTask ); genTask.dependsOn( compileResourcesTask );
@ -324,8 +325,8 @@ public class JpaMetamodelGenerationTask extends DefaultTask {
genTask.finalizedBy( compileJpaMetamodelTask ); genTask.finalizedBy( compileJpaMetamodelTask );
mainCompileTask.finalizedBy( compileJpaMetamodelTask ); mainCompileTask.finalizedBy( compileJpaMetamodelTask );
compileJpaMetamodelTask.dependsOn( genTask ); compileJpaMetamodelTask.dependsOn( genTask );
compileJpaMetamodelTask.source( project.files( ormDsl.getJpaMetamodelSpec().getGenerationOutputDirectory() ) ); compileJpaMetamodelTask.source( project.files( pluginDsl.getJpaMetamodelSpec().getGenerationOutputDirectory() ) );
compileJpaMetamodelTask.getDestinationDirectory().set( ormDsl.getJpaMetamodelSpec().getCompileOutputDirectory() ); compileJpaMetamodelTask.getDestinationDirectory().set( pluginDsl.getJpaMetamodelSpec().getCompileOutputDirectory() );
compileJpaMetamodelTask.setClasspath( compileJpaMetamodelTask.setClasspath(
project.getConfigurations().getByName( "runtimeClasspath" ).plus( mainSourceSet.getRuntimeClasspath() ) project.getConfigurations().getByName( "runtimeClasspath" ).plus( mainSourceSet.getRuntimeClasspath() )
); );