From cf7bb0dcec89924d430bc4ae0f6cb267e9d77bc1 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Fri, 6 Sep 2013 13:14:36 -0500 Subject: [PATCH] HHH-8309 - Create hibernate-orm subprojects for maven and gradle plugins --- .../hibernate-gradle-plugin.gradle | 38 +----- .../enhance/plugins/EnhancePlugin.java | 62 --------- .../gradle/EnhancerTask.groovy} | 118 +++++++++--------- .../tooling/gradle/HibernatePlugin.java | 75 +++++++++++ .../gradle-plugins/enhance.properties | 1 - .../gradle-plugins/hibernate.properties | 1 + .../hibernate-maven-plugin.gradle | 0 .../plugins/HibernateEnhancementMojo.java | 2 +- settings.gradle | 2 +- 9 files changed, 141 insertions(+), 158 deletions(-) delete mode 100644 hibernate-gradle-plugin/src/main/groovy/org/hibernate/bytecode/enhance/plugins/EnhancePlugin.java rename hibernate-gradle-plugin/src/main/groovy/org/hibernate/{bytecode/enhance/plugins/EnhanceTask.groovy => tooling/gradle/EnhancerTask.groovy} (58%) create mode 100644 hibernate-gradle-plugin/src/main/groovy/org/hibernate/tooling/gradle/HibernatePlugin.java delete mode 100644 hibernate-gradle-plugin/src/main/resources/META-INF/gradle-plugins/enhance.properties create mode 100644 hibernate-gradle-plugin/src/main/resources/META-INF/gradle-plugins/hibernate.properties rename enhance-maven-plugin/enhance-maven-plugin.gradle => hibernate-maven-plugin/hibernate-maven-plugin.gradle (100%) rename enhance-maven-plugin/src/main/java/org/hibernate/bytecode/enhance/plugins/MavenEnhancePlugin.java => hibernate-maven-plugin/src/main/java/org/hibernate/bytecode/enhance/plugins/HibernateEnhancementMojo.java (99%) diff --git a/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle b/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle index 3d576e8941..39524e4b59 100644 --- a/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle +++ b/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle @@ -1,47 +1,11 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2013, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ apply plugin: 'groovy' apply plugin: 'maven' apply plugin: 'java' -repositories { - mavenCentral() -} - dependencies { compile( project(':hibernate-core') ) compile( libraries.jpa ) compile( libraries.javassist ) compile gradleApi() compile localGroovy() -} - -/* Available for testing locally. */ -install { - repositories.mavenInstaller { - pom.groupId = 'org.hibernate' - pom.artifactId = 'hibernate-gradle-plugin' - pom.version = '1.0' - } -} +} \ No newline at end of file diff --git a/hibernate-gradle-plugin/src/main/groovy/org/hibernate/bytecode/enhance/plugins/EnhancePlugin.java b/hibernate-gradle-plugin/src/main/groovy/org/hibernate/bytecode/enhance/plugins/EnhancePlugin.java deleted file mode 100644 index 5ccbeacfbe..0000000000 --- a/hibernate-gradle-plugin/src/main/groovy/org/hibernate/bytecode/enhance/plugins/EnhancePlugin.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2013, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.bytecode.enhance.plugins; - -import org.gradle.api.Plugin; -import org.gradle.api.Project; -import org.gradle.api.Task; -import org.gradle.api.plugins.BasePlugin; -import org.gradle.api.plugins.JavaBasePlugin; -import org.gradle.api.artifacts.Configuration; -import org.gradle.api.plugins.JavaPlugin; -import org.hibernate.bytecode.enhance.plugins.EnhanceTask; - -/** - * This plugin will add Entity enhancement behaviour to the build lifecycle. - * - * @author Jeremy Whiting - */ -@SuppressWarnings("serial") -public class EnhancePlugin implements Plugin{ - - public static final String ENHANCE_TASK_NAME = "enhance"; - public static final String HAPPENS_AFTER_ENHANCE_TASK_NAME = JavaPlugin.JAR_TASK_NAME; - - public void apply(Project project) { - project.getLogger().debug( "Applying enhance plugin to project." ); - configureTask( project ); - project.getLogger().debug( String.format( "DAG has been configured with enhance task dependent on [%s].", JavaPlugin.CLASSES_TASK_NAME ) ); - } - - private void configureTask(Project project) { - EnhanceTask enhanceTask = project.getTasks().create( ENHANCE_TASK_NAME, EnhanceTask.class ); - enhanceTask.setGroup(BasePlugin.BUILD_GROUP); - // connect up the task in the task dependency graph - Task classesTask = project.getTasks().getByName( JavaPlugin.CLASSES_TASK_NAME ); - enhanceTask.dependsOn( classesTask ); - - Task jarTask = project.getTasks().getByName( HAPPENS_AFTER_ENHANCE_TASK_NAME ); - jarTask.dependsOn( enhanceTask ); - } -} diff --git a/hibernate-gradle-plugin/src/main/groovy/org/hibernate/bytecode/enhance/plugins/EnhanceTask.groovy b/hibernate-gradle-plugin/src/main/groovy/org/hibernate/tooling/gradle/EnhancerTask.groovy similarity index 58% rename from hibernate-gradle-plugin/src/main/groovy/org/hibernate/bytecode/enhance/plugins/EnhanceTask.groovy rename to hibernate-gradle-plugin/src/main/groovy/org/hibernate/tooling/gradle/EnhancerTask.groovy index 4edbdb7f98..5f77c383e7 100644 --- a/hibernate-gradle-plugin/src/main/groovy/org/hibernate/bytecode/enhance/plugins/EnhanceTask.groovy +++ b/hibernate-gradle-plugin/src/main/groovy/org/hibernate/tooling/gradle/EnhancerTask.groovy @@ -21,98 +21,103 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.bytecode.enhance.plugins - -import org.hibernate.bytecode.enhance.spi.Enhancer -import org.hibernate.bytecode.enhance.spi.EnhancementContext +package org.hibernate.tooling.gradle + import javassist.ClassPool import javassist.CtClass import javassist.CtField -import javax.persistence.Transient -import javax.persistence.Entity -import java.io.FileInputStream -import java.io.FileOutputStream import org.gradle.api.DefaultTask -import org.gradle.api.plugins.Convention -import org.gradle.api.tasks.TaskAction import org.gradle.api.file.FileTree +import org.gradle.api.tasks.TaskAction +import org.hibernate.bytecode.enhance.spi.EnhancementContext +import org.hibernate.bytecode.enhance.spi.Enhancer + +import javax.persistence.Entity +import javax.persistence.Transient /** -* Plugin to enhance Entities using the context(s) to determine -* which to apply. -* @author Jeremy Whiting -*/ -public class EnhanceTask extends DefaultTask implements EnhancementContext { + * Gradle Task to apply Hibernate's bytecode Enhancer + * + * @author Jeremy Whiting + */ +public class EnhancerTask extends DefaultTask implements EnhancementContext { private ClassLoader overridden - public EnhanceTask() { + public EnhancerTask() { super() - setDescription('Enhances Entity classes for efficient association referencing.') + setDescription( 'Enhances Entity classes for efficient association referencing.' ) } - + @TaskAction - def enhance () { - logger.info( 'enhance task started') - ext.pool = new ClassPool(false) - ext.enhancer = new Enhancer(this) - FileTree tree = project.fileTree(dir: project.sourceSets.main.output.classesDir) + def enhance() { + logger.info( 'enhance task started' ) + ext.pool = new ClassPool( false ) + ext.enhancer = new Enhancer( this ) + FileTree tree = project.fileTree( dir: project.sourceSets.main.output.classesDir ) tree.include '**/*.class' - tree.each( - { File file -> + tree.each( { File file -> final byte[] enhancedBytecode; InputStream is = null; CtClass clas = null; try { - is = new FileInputStream(file.toString()) - clas =ext.pool.makeClass(is) - if (!clas.hasAnnotation(Entity.class)){ - logger.debug("Class $file not an annotated Entity class. skipping...") - } else { - enhancedBytecode = ext.enhancer.enhance( clas.getName(), clas.toBytecode() ); + is = new FileInputStream( file.toString() ) + clas = ext.pool.makeClass( is ) + // Enhancer already does this check to see if it should enhance, why are we doing it again here? + if ( !clas.hasAnnotation( Entity.class ) ) { + logger.debug( "Class $file not an annotated Entity class. skipping..." ) + } + else { + enhancedBytecode = ext.enhancer.enhance( clas.getName(), clas.toBytecode() ); } } catch (Exception e) { logger.error( "Unable to enhance class [${file.toString()}]", e ) return - } finally { - try { - if (null != is) is.close(); - } finally{} } - if (null != enhancedBytecode){ + finally { + try { + if ( null != is ) { + is.close() + }; + } + finally {} + } + if ( null != enhancedBytecode ) { if ( file.delete() ) { - if ( ! file.createNewFile() ) { - logger.error( "Unable to recreate class file [" + clas.getName() + "]") + if ( !file.createNewFile() ) { + logger.error( "Unable to recreate class file [" + clas.getName() + "]" ) } } else { - logger.error( "Unable to delete class file [" + clas.getName() + "]") + logger.error( "Unable to delete class file [" + clas.getName() + "]" ) } FileOutputStream outputStream = new FileOutputStream( file, false ) try { - outputStream.write( enhancedBytecode ) - outputStream.flush() + outputStream.write( enhancedBytecode ) + outputStream.flush() } finally { - try { - if (outputStream != null) outputStream.close() - clas.detach()//release memory - } - catch ( IOException ignore) { - } + try { + if ( outputStream != null ) { + outputStream.close() + } + clas.detach()//release memory + } + catch (IOException ignore) { + } } - } - }) - logger.info( 'enhance task finished') + } + } ) + logger.info( 'enhance task finished' ) } - + public ClassLoader getLoadingClassLoader() { if ( null == this.overridden ) { - return getClass().getClassLoader(); + return getClass().getClassLoader(); } else { - return this.overridden; + return this.overridden; } } @@ -130,11 +135,12 @@ public class EnhanceTask extends DefaultTask implements EnhancementContext { public boolean isLazyLoadable(CtField field) { return true; - } - + } + public boolean isCompositeClass(CtClass classDescriptor) { return false; - } + } + public boolean doDirtyCheckingInline(CtClass classDescriptor) { return false; } diff --git a/hibernate-gradle-plugin/src/main/groovy/org/hibernate/tooling/gradle/HibernatePlugin.java b/hibernate-gradle-plugin/src/main/groovy/org/hibernate/tooling/gradle/HibernatePlugin.java new file mode 100644 index 0000000000..d910269b59 --- /dev/null +++ b/hibernate-gradle-plugin/src/main/groovy/org/hibernate/tooling/gradle/HibernatePlugin.java @@ -0,0 +1,75 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.tooling.gradle; + +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.Task; +import org.gradle.api.plugins.BasePlugin; +import org.gradle.api.plugins.JavaPlugin; + +/** + * The Hibernate Gradle plugin. Adds Hibernate build-time capabilities into your Gradle-based build. + * + * @author Jeremy Whiting + * @author Steve Ebersole + */ +@SuppressWarnings("serial") +public class HibernatePlugin implements Plugin { + + public static final String ENHANCE_TASK_NAME = "enhance"; + + public void apply(Project project) { + applyEnhancement( project ); + } + + private void applyEnhancement(Project project) { + project.getLogger().debug( "Applying Hibernate enhancement to project." ); + + // few things... + + // 1) would probably be best as a doLast Action attached to the compile task rather than + // a task. Really ideally would be a task association for "always run after", but Gradle + // does not yet have that (mustRunAfter is very different semantic, finalizedBy is closer but + // will run even if the first task fails). The initial attempt here fell into the "maven" trap + // of trying to run a dependent task by attaching it to a task know to run after the we want to run after; + // which is a situation tailored made for Task.doLast + + // 2) would be better to allow specifying which SourceSet to apply this to. For example, in the Hibernate + // build itself, this would be best applied to the 'test' sourceSet; though generally speaking the + // 'main' sourceSet is more appropriate + + // for now, we'll just: + // 1) use a EnhancerTask + finalizedBy + // 2) apply to main sourceSet + + EnhancerTask enhancerTask = project.getTasks().create( ENHANCE_TASK_NAME, EnhancerTask.class ); + enhancerTask.setGroup( BasePlugin.BUILD_GROUP ); + + // connect up the task in the task dependency graph + Task classesTask = project.getTasks().getByName( JavaPlugin.CLASSES_TASK_NAME ); + enhancerTask.dependsOn( classesTask ); + classesTask.finalizedBy( enhancerTask ); + } +} diff --git a/hibernate-gradle-plugin/src/main/resources/META-INF/gradle-plugins/enhance.properties b/hibernate-gradle-plugin/src/main/resources/META-INF/gradle-plugins/enhance.properties deleted file mode 100644 index 33985e39da..0000000000 --- a/hibernate-gradle-plugin/src/main/resources/META-INF/gradle-plugins/enhance.properties +++ /dev/null @@ -1 +0,0 @@ -implementation-class=org.hibernate.bytecode.enhance.plugins.EnhancePlugin diff --git a/hibernate-gradle-plugin/src/main/resources/META-INF/gradle-plugins/hibernate.properties b/hibernate-gradle-plugin/src/main/resources/META-INF/gradle-plugins/hibernate.properties new file mode 100644 index 0000000000..b3fb7a0524 --- /dev/null +++ b/hibernate-gradle-plugin/src/main/resources/META-INF/gradle-plugins/hibernate.properties @@ -0,0 +1 @@ +implementation-class=org.hibernate.tooling.gradle.HibernatePlugin diff --git a/enhance-maven-plugin/enhance-maven-plugin.gradle b/hibernate-maven-plugin/hibernate-maven-plugin.gradle similarity index 100% rename from enhance-maven-plugin/enhance-maven-plugin.gradle rename to hibernate-maven-plugin/hibernate-maven-plugin.gradle diff --git a/enhance-maven-plugin/src/main/java/org/hibernate/bytecode/enhance/plugins/MavenEnhancePlugin.java b/hibernate-maven-plugin/src/main/java/org/hibernate/bytecode/enhance/plugins/HibernateEnhancementMojo.java similarity index 99% rename from enhance-maven-plugin/src/main/java/org/hibernate/bytecode/enhance/plugins/MavenEnhancePlugin.java rename to hibernate-maven-plugin/src/main/java/org/hibernate/bytecode/enhance/plugins/HibernateEnhancementMojo.java index 3b060f1b46..81336c821d 100644 --- a/enhance-maven-plugin/src/main/java/org/hibernate/bytecode/enhance/plugins/MavenEnhancePlugin.java +++ b/hibernate-maven-plugin/src/main/java/org/hibernate/bytecode/enhance/plugins/HibernateEnhancementMojo.java @@ -55,7 +55,7 @@ import org.apache.maven.plugins.annotations.Parameter; * @author Jeremy Whiting */ @Mojo(name = "enhance") -public class MavenEnhancePlugin extends AbstractMojo { +public class HibernateEnhancementMojo extends AbstractMojo { /** * The contexts to use during enhancement. diff --git a/settings.gradle b/settings.gradle index f09b30a853..d27e91356f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -11,7 +11,7 @@ include 'hibernate-proxool' include 'hibernate-ehcache' include 'hibernate-infinispan' include 'hibernate-gradle-plugin' -include 'enhance-maven-plugin' +include 'hibernate-maven-plugin' include 'documentation' include 'release'