6
6
*/
7
7
package org .hibernate .search .metamodel .generator .plugin ;
8
8
9
+ import java .io .File ;
9
10
import java .io .FileOutputStream ;
10
11
import java .io .IOException ;
12
+ import java .net .URL ;
13
+ import java .net .URLClassLoader ;
11
14
import java .nio .charset .StandardCharsets ;
15
+ import java .nio .file .FileSystems ;
12
16
import java .nio .file .Files ;
13
17
import java .nio .file .Path ;
18
+ import java .util .ArrayList ;
14
19
import java .util .Collection ;
15
20
import java .util .List ;
16
21
import java .util .Objects ;
17
22
import java .util .Properties ;
23
+ import java .util .stream .Collectors ;
24
+
25
+ import javax .tools .DiagnosticCollector ;
26
+ import javax .tools .JavaCompiler ;
27
+ import javax .tools .JavaFileObject ;
28
+ import javax .tools .StandardJavaFileManager ;
29
+ import javax .tools .StandardLocation ;
30
+ import javax .tools .ToolProvider ;
18
31
19
32
import org .hibernate .SessionFactory ;
20
33
import org .hibernate .search .engine .backend .metamodel .IndexDescriptor ;
25
38
import org .hibernate .search .util .impl .integrationtest .backend .lucene .LuceneBackendConfiguration ;
26
39
import org .hibernate .search .util .impl .integrationtest .mapper .orm .OrmSetupHelper ;
27
40
41
+ import org .apache .maven .artifact .Artifact ;
28
42
import org .apache .maven .model .Dependency ;
29
43
import org .apache .maven .plugin .AbstractMojo ;
30
44
import org .apache .maven .plugin .MojoExecutionException ;
@@ -43,9 +57,17 @@ public class HibernateSearchMetamodelGeneratorMojo extends AbstractMojo {
43
57
@ Parameter (property = "annotatedTypes" )
44
58
List <String > annotatedTypes ;
45
59
60
+ @ Parameter (property = "packagesToCompile" )
61
+ List <String > packagesToCompile ;
62
+
46
63
@ Parameter (property = "properties" )
47
64
Properties properties ;
48
65
66
+ @ Parameter (property = "sameModuleCompile" , defaultValue = "false" )
67
+ boolean sameModuleCompile ;
68
+
69
+ private URLClassLoader classLoader ;
70
+
49
71
@ Override
50
72
public void execute () throws MojoExecutionException , MojoFailureException {
51
73
getLog ().info ( "Hibernate Search Metamodel Generator" );
@@ -60,8 +82,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
60
82
61
83
Path generatedMetamodelLocation =
62
84
Path .of ( project .getBuild ().getOutputDirectory () ).resolveSibling ( "generated-metamodel-sources" );
63
- project .addCompileSourceRoot ( generatedMetamodelLocation .toString () );
64
-
65
85
66
86
try ( SessionFactory sessionFactory = setupContext .setup ( annotatedTypes () ) ) {
67
87
SearchMapping mapping = Search .mapping ( sessionFactory );
@@ -75,7 +95,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
75
95
getLog ().info ( "Indexed entities: " + indexedEntities );
76
96
77
97
}
78
-
98
+ project . addCompileSourceRoot ( generatedMetamodelLocation . toString () );
79
99
}
80
100
}
81
101
@@ -112,18 +132,79 @@ private void createClass(SearchIndexedEntity<?> indexedEntity, Path root) {
112
132
}
113
133
114
134
private Class <?>[] annotatedTypes () {
115
- try {
116
- Class <?>[] types = new Class <?>[annotatedTypes .size ()];
117
- for ( int i = 0 ; i < annotatedTypes .size (); i ++ ) {
118
- types [i ] = Class .forName ( annotatedTypes .get ( i ) );
135
+ if ( sameModuleCompile ) {
136
+ try {
137
+ List <Path > roots = new ArrayList <>();
138
+ List <Path > classes = new ArrayList <>();
139
+ for ( String compileSourceRoot : this .project .getCompileSourceRoots () ) {
140
+ Path root = Path .of ( compileSourceRoot );
141
+ roots .add ( root );
142
+
143
+ for ( String pkg : packagesToCompile ) {
144
+ Path path = root .resolve ( Path .of ( pkg .replace ( "." , FileSystems .getDefault ().getSeparator () ) ) );
145
+ if ( Files .exists ( path ) ) {
146
+ Files .list ( path ).filter ( f -> f .getFileName ().toString ().endsWith ( ".java" ) )
147
+ .forEach ( classes ::add );
148
+ }
149
+ }
150
+ }
151
+
152
+ Path output = Path .of ( project .getBuild ().getOutputDirectory () )
153
+ .resolveSibling ( "generated-metamodel-pre-compiled-classes" );
154
+ Files .createDirectories ( output );
155
+
156
+ JavaCompiler compiler = ToolProvider .getSystemJavaCompiler ();
157
+ StandardJavaFileManager fileManager = compiler .getStandardFileManager ( null , null , null );
158
+ fileManager .setLocationFromPaths ( StandardLocation .SOURCE_PATH , roots );
159
+ fileManager .setLocation ( StandardLocation .CLASS_PATH , getDependenciesAsFiles () );
160
+ fileManager .setLocationFromPaths ( StandardLocation .CLASS_OUTPUT , List .of ( output ) );
161
+
162
+ Iterable <? extends JavaFileObject > toCompile = fileManager .getJavaFileObjectsFromPaths ( classes );
163
+
164
+ DiagnosticCollector <JavaFileObject > diagnostic = new DiagnosticCollector <>();
165
+ JavaCompiler .CompilationTask task =
166
+ compiler .getTask ( null , fileManager , diagnostic , List .of (), null , toCompile );
167
+
168
+ task .call ();
169
+
170
+ classLoader = new URLClassLoader ( "hibernate-search-generator" ,
171
+ new URL [] { output .toUri ().toURL () }, this .getClass ().getClassLoader () );
172
+ Thread .currentThread ().setContextClassLoader ( classLoader );
173
+
174
+ Class <?>[] types = new Class <?>[annotatedTypes .size ()];
175
+ for ( int i = 0 ; i < annotatedTypes .size (); i ++ ) {
176
+ types [i ] = classLoader .loadClass ( annotatedTypes .get ( i ) );
177
+ }
178
+ return types ;
179
+
180
+ }
181
+ catch (IOException | ClassNotFoundException e ) {
182
+ throw new RuntimeException ( e );
119
183
}
120
- return types ;
121
184
}
122
- catch (ClassNotFoundException e ) {
123
- throw new RuntimeException ( e );
185
+ else {
186
+ try {
187
+ Class <?>[] types = new Class <?>[annotatedTypes .size ()];
188
+ for ( int i = 0 ; i < annotatedTypes .size (); i ++ ) {
189
+ types [i ] = Class .forName ( annotatedTypes .get ( i ) );
190
+ }
191
+ return types ;
192
+ }
193
+ catch (ClassNotFoundException e ) {
194
+ throw new RuntimeException ( e );
195
+ }
124
196
}
125
197
}
126
198
199
+ private Collection <File > getDependenciesAsFiles () {
200
+ project .setArtifactFilter ( artifact -> true );
201
+ project .setArtifacts ( null );
202
+ return project .getArtifacts ()
203
+ .stream ()
204
+ .map ( Artifact ::getFile )
205
+ .collect ( Collectors .toList () );
206
+ }
207
+
127
208
private boolean hasOrmMapper (List <Dependency > dependencies ) {
128
209
for ( Dependency dependency : dependencies ) {
129
210
if ( "hibernate-search-mapper-orm" .equals ( dependency .getArtifactId () ) ) {
0 commit comments