From 577bd4752aa4dcd63aaa17fd7313dc9a2b5b9d84 Mon Sep 17 00:00:00 2001 From: barreiro Date: Tue, 29 Sep 2015 04:19:32 +0100 Subject: [PATCH] HHH-10145 - [maven plugin] Include dependencies in loader classloader (cherry picked from commit 080fcd90821cc71664ed10a6453c651c5c57d00c) --- libraries.gradle | 2 ++ .../hibernate-enhance-maven-plugin.gradle | 4 +++ .../orm/tooling/maven/MavenEnhancePlugin.java | 31 ++++++++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/libraries.gradle b/libraries.gradle index d0a461a35f..41da312371 100644 --- a/libraries.gradle +++ b/libraries.gradle @@ -68,6 +68,8 @@ ext { java16_signature: 'org.codehaus.mojo.signature:java16:1.0@signature', //Maven plugin framework + maven_core: 'org.apache.maven:maven-core:3.0.5', + maven_artifact: 'org.apache.maven:maven-artifact:3.0.5', maven_plugin: 'org.apache.maven:maven-plugin-api:3.0.5', maven_plugin_tools: 'org.apache.maven.plugin-tools:maven-plugin-annotations:3.2', diff --git a/tooling/hibernate-enhance-maven-plugin/hibernate-enhance-maven-plugin.gradle b/tooling/hibernate-enhance-maven-plugin/hibernate-enhance-maven-plugin.gradle index 3bd6f19157..d605dc0e2e 100644 --- a/tooling/hibernate-enhance-maven-plugin/hibernate-enhance-maven-plugin.gradle +++ b/tooling/hibernate-enhance-maven-plugin/hibernate-enhance-maven-plugin.gradle @@ -22,12 +22,16 @@ processResources { } dependencies { + compile( libraries.maven_core ) { transitive = false } + compile( libraries.maven_artifact ) { transitive = false } compile( libraries.maven_plugin ) { transitive = false } compile( libraries.maven_plugin_tools ) { transitive = false } compile( project(':hibernate-core') ) { transitive = false } compile( libraries.jpa ) { transitive = false } compile( libraries.javassist ) { transitive = false } compile 'org.codehaus.plexus:plexus-utils:3.0.1' + runtime( libraries.maven_core ) + runtime( libraries.maven_artifact ) runtime( libraries.maven_plugin ) runtime( libraries.maven_plugin_tools ) runtime( project(':hibernate-core') ) diff --git a/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java b/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java index bf4cd476b7..6b773e49a1 100644 --- a/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java +++ b/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java @@ -17,7 +17,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -25,6 +24,7 @@ import javassist.ClassPool; import javassist.CtClass; import javassist.CtField; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -32,6 +32,8 @@ import org.apache.maven.plugins.annotations.Execute; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.bytecode.enhance.spi.EnhancementContext; @@ -43,7 +45,7 @@ import org.hibernate.bytecode.enhance.spi.Enhancer; * @author Jeremy Whiting * @author Luis Barreiro */ -@Mojo(name = "enhance", defaultPhase = LifecyclePhase.COMPILE) +@Mojo(name = "enhance", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) @Execute(goal = "enhance", phase = LifecyclePhase.COMPILE) public class MavenEnhancePlugin extends AbstractMojo { @@ -85,7 +87,7 @@ public class MavenEnhancePlugin extends AbstractMojo { File root = new File( this.dir ); walkDir( root ); - final ClassLoader classLoader = toClassLoader( Arrays.asList( root ) ); + final ClassLoader classLoader = toClassLoader( Collections.singletonList( root ) ); EnhancementContext enhancementContext = new DefaultEnhancementContext() { @Override @@ -147,7 +149,7 @@ public class MavenEnhancePlugin extends AbstractMojo { for ( File file : runtimeClasspath ) { try { urls.add( file.toURI().toURL() ); - getLog().debug( "Adding root " + file.getAbsolutePath() + " to classpath " ); + getLog().debug( "Adding classpath entry for classes root " + file.getAbsolutePath() ); } catch (MalformedURLException e) { String msg = "Unable to resolve classpath entry to URL: " + file.getAbsolutePath(); @@ -158,6 +160,27 @@ public class MavenEnhancePlugin extends AbstractMojo { } } + // HHH-10145 Add dependencies to classpath as well - all but the ones used for testing purposes + MavenProject project = ( (MavenProject) getPluginContext().get( "project" ) ); + if ( project != null ) { + for ( Artifact a : project.getDependencyArtifacts() ) { + if ( !Artifact.SCOPE_TEST.equals( a.getScope() ) ) { + try { + urls.add( a.getFile().toURI().toURL() ); + getLog().debug( "Adding classpath entry for dependency " + a.getId() ); + } + catch (MalformedURLException e) { + String msg = "Unable to resolve URL for dependency " + a.getId() + " at " + a.getFile() + .getAbsolutePath(); + if ( failOnError ) { + throw new MojoExecutionException( msg, e ); + } + getLog().warn( msg ); + } + } + } + } + return new URLClassLoader( urls.toArray( new URL[urls.size()] ), Enhancer.class.getClassLoader() ); }