Skip to content

Commit 577bd47

Browse files
barreirosebersole
authored andcommitted
HHH-10145 - [maven plugin] Include dependencies in loader classloader
(cherry picked from commit 080fcd9)
1 parent 32d32ec commit 577bd47

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

libraries.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ ext {
6868
java16_signature: 'org.codehaus.mojo.signature:java16:1.0@signature',
6969

7070
//Maven plugin framework
71+
maven_core: 'org.apache.maven:maven-core:3.0.5',
72+
maven_artifact: 'org.apache.maven:maven-artifact:3.0.5',
7173
maven_plugin: 'org.apache.maven:maven-plugin-api:3.0.5',
7274
maven_plugin_tools: 'org.apache.maven.plugin-tools:maven-plugin-annotations:3.2',
7375

tooling/hibernate-enhance-maven-plugin/hibernate-enhance-maven-plugin.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ processResources {
2222
}
2323

2424
dependencies {
25+
compile( libraries.maven_core ) { transitive = false }
26+
compile( libraries.maven_artifact ) { transitive = false }
2527
compile( libraries.maven_plugin ) { transitive = false }
2628
compile( libraries.maven_plugin_tools ) { transitive = false }
2729
compile( project(':hibernate-core') ) { transitive = false }
2830
compile( libraries.jpa ) { transitive = false }
2931
compile( libraries.javassist ) { transitive = false }
3032
compile 'org.codehaus.plexus:plexus-utils:3.0.1'
33+
runtime( libraries.maven_core )
34+
runtime( libraries.maven_artifact )
3135
runtime( libraries.maven_plugin )
3236
runtime( libraries.maven_plugin_tools )
3337
runtime( project(':hibernate-core') )

tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,23 @@
1717
import java.net.URL;
1818
import java.net.URLClassLoader;
1919
import java.util.ArrayList;
20-
import java.util.Arrays;
2120
import java.util.Collections;
2221
import java.util.List;
2322

2423
import javassist.ClassPool;
2524
import javassist.CtClass;
2625
import javassist.CtField;
2726

27+
import org.apache.maven.artifact.Artifact;
2828
import org.apache.maven.plugin.AbstractMojo;
2929
import org.apache.maven.plugin.MojoExecutionException;
3030
import org.apache.maven.plugin.MojoFailureException;
3131
import org.apache.maven.plugins.annotations.Execute;
3232
import org.apache.maven.plugins.annotations.LifecyclePhase;
3333
import org.apache.maven.plugins.annotations.Mojo;
3434
import org.apache.maven.plugins.annotations.Parameter;
35+
import org.apache.maven.plugins.annotations.ResolutionScope;
36+
import org.apache.maven.project.MavenProject;
3537

3638
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
3739
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
@@ -43,7 +45,7 @@
4345
* @author Jeremy Whiting
4446
* @author Luis Barreiro
4547
*/
46-
@Mojo(name = "enhance", defaultPhase = LifecyclePhase.COMPILE)
48+
@Mojo(name = "enhance", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
4749
@Execute(goal = "enhance", phase = LifecyclePhase.COMPILE)
4850
public class MavenEnhancePlugin extends AbstractMojo {
4951

@@ -85,7 +87,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8587
File root = new File( this.dir );
8688
walkDir( root );
8789

88-
final ClassLoader classLoader = toClassLoader( Arrays.asList( root ) );
90+
final ClassLoader classLoader = toClassLoader( Collections.singletonList( root ) );
8991

9092
EnhancementContext enhancementContext = new DefaultEnhancementContext() {
9193
@Override
@@ -147,7 +149,7 @@ private ClassLoader toClassLoader(List<File> runtimeClasspath) throws MojoExecut
147149
for ( File file : runtimeClasspath ) {
148150
try {
149151
urls.add( file.toURI().toURL() );
150-
getLog().debug( "Adding root " + file.getAbsolutePath() + " to classpath " );
152+
getLog().debug( "Adding classpath entry for classes root " + file.getAbsolutePath() );
151153
}
152154
catch (MalformedURLException e) {
153155
String msg = "Unable to resolve classpath entry to URL: " + file.getAbsolutePath();
@@ -158,6 +160,27 @@ private ClassLoader toClassLoader(List<File> runtimeClasspath) throws MojoExecut
158160
}
159161
}
160162

163+
// HHH-10145 Add dependencies to classpath as well - all but the ones used for testing purposes
164+
MavenProject project = ( (MavenProject) getPluginContext().get( "project" ) );
165+
if ( project != null ) {
166+
for ( Artifact a : project.getDependencyArtifacts() ) {
167+
if ( !Artifact.SCOPE_TEST.equals( a.getScope() ) ) {
168+
try {
169+
urls.add( a.getFile().toURI().toURL() );
170+
getLog().debug( "Adding classpath entry for dependency " + a.getId() );
171+
}
172+
catch (MalformedURLException e) {
173+
String msg = "Unable to resolve URL for dependency " + a.getId() + " at " + a.getFile()
174+
.getAbsolutePath();
175+
if ( failOnError ) {
176+
throw new MojoExecutionException( msg, e );
177+
}
178+
getLog().warn( msg );
179+
}
180+
}
181+
}
182+
}
183+
161184
return new URLClassLoader( urls.toArray( new URL[urls.size()] ), Enhancer.class.getClassLoader() );
162185
}
163186

0 commit comments

Comments
 (0)