17
17
import java .net .URL ;
18
18
import java .net .URLClassLoader ;
19
19
import java .util .ArrayList ;
20
- import java .util .Arrays ;
21
20
import java .util .Collections ;
22
21
import java .util .List ;
23
22
24
23
import javassist .ClassPool ;
25
24
import javassist .CtClass ;
26
25
import javassist .CtField ;
27
26
27
+ import org .apache .maven .artifact .Artifact ;
28
28
import org .apache .maven .plugin .AbstractMojo ;
29
29
import org .apache .maven .plugin .MojoExecutionException ;
30
30
import org .apache .maven .plugin .MojoFailureException ;
31
31
import org .apache .maven .plugins .annotations .Execute ;
32
32
import org .apache .maven .plugins .annotations .LifecyclePhase ;
33
33
import org .apache .maven .plugins .annotations .Mojo ;
34
34
import org .apache .maven .plugins .annotations .Parameter ;
35
+ import org .apache .maven .plugins .annotations .ResolutionScope ;
36
+ import org .apache .maven .project .MavenProject ;
35
37
36
38
import org .hibernate .bytecode .enhance .spi .DefaultEnhancementContext ;
37
39
import org .hibernate .bytecode .enhance .spi .EnhancementContext ;
43
45
* @author Jeremy Whiting
44
46
* @author Luis Barreiro
45
47
*/
46
- @ Mojo (name = "enhance" , defaultPhase = LifecyclePhase .COMPILE )
48
+ @ Mojo (name = "enhance" , defaultPhase = LifecyclePhase .COMPILE , requiresDependencyResolution = ResolutionScope . COMPILE_PLUS_RUNTIME )
47
49
@ Execute (goal = "enhance" , phase = LifecyclePhase .COMPILE )
48
50
public class MavenEnhancePlugin extends AbstractMojo {
49
51
@@ -85,7 +87,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
85
87
File root = new File ( this .dir );
86
88
walkDir ( root );
87
89
88
- final ClassLoader classLoader = toClassLoader ( Arrays . asList ( root ) );
90
+ final ClassLoader classLoader = toClassLoader ( Collections . singletonList ( root ) );
89
91
90
92
EnhancementContext enhancementContext = new DefaultEnhancementContext () {
91
93
@ Override
@@ -147,7 +149,7 @@ private ClassLoader toClassLoader(List<File> runtimeClasspath) throws MojoExecut
147
149
for ( File file : runtimeClasspath ) {
148
150
try {
149
151
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 () );
151
153
}
152
154
catch (MalformedURLException e ) {
153
155
String msg = "Unable to resolve classpath entry to URL: " + file .getAbsolutePath ();
@@ -158,6 +160,27 @@ private ClassLoader toClassLoader(List<File> runtimeClasspath) throws MojoExecut
158
160
}
159
161
}
160
162
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
+
161
184
return new URLClassLoader ( urls .toArray ( new URL [urls .size ()] ), Enhancer .class .getClassLoader () );
162
185
}
163
186
0 commit comments