Skip to content

Commit 0c8366e

Browse files
committed
HSEARCH-3319 WIP: maven plugin to generate "metamodel classes"
1 parent 296c173 commit 0c8366e

File tree

11 files changed

+492
-2
lines changed

11 files changed

+492
-2
lines changed

metamodel/generator-plugin/pom.xml

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.hibernate.search</groupId>
6+
<artifactId>hibernate-search-parent-public</artifactId>
7+
<version>7.2.0-SNAPSHOT</version>
8+
<relativePath>../../build/parents/public</relativePath>
9+
</parent>
10+
<artifactId>hibernate-search-metamodel-generator-plugin</artifactId>
11+
<packaging>maven-plugin</packaging>
12+
13+
<name>Hibernate Search Metamodel Generator Plugin</name>
14+
<description>Hibernate Search Metamodel Generator Plugin, creates static metamodel classes</description>
15+
16+
<properties>
17+
<!-- This is a publicly distributed module that should be published: -->
18+
<deploy.skip>false</deploy.skip>
19+
<enforcer.dependencyconvergence.skip>true</enforcer.dependencyconvergence.skip>
20+
<java.module.name>org.hibernate.search.metamodel.generator.plugin</java.module.name>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.hibernate.search</groupId>
26+
<artifactId>hibernate-search-engine</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.hibernate.search</groupId>
30+
<artifactId>hibernate-search-mapper-orm</artifactId>
31+
<optional>true</optional>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.hibernate.search</groupId>
35+
<artifactId>hibernate-search-mapper-pojo-standalone</artifactId>
36+
<optional>true</optional>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.jboss.logging</groupId>
40+
<artifactId>jboss-logging</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.jboss.logging</groupId>
44+
<artifactId>jboss-logging-annotations</artifactId>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.hibernate.search</groupId>
49+
<artifactId>hibernate-search-util-internal-integrationtest-mapper-orm</artifactId>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.apache.maven</groupId>
54+
<artifactId>maven-plugin-api</artifactId>
55+
<version>${maven.min.version}</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.apache.maven</groupId>
59+
<artifactId>maven-core</artifactId>
60+
<version>${maven.min.version}</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.apache.maven.plugin-tools</groupId>
64+
<artifactId>maven-plugin-annotations</artifactId>
65+
<version>3.13.0</version>
66+
<scope>provided</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.codehaus.plexus</groupId>
70+
<artifactId>plexus-utils</artifactId>
71+
<version>3.5.1</version>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>org.hibernate.search</groupId>
76+
<artifactId>hibernate-search-util-internal-test-common</artifactId>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.hibernate.search</groupId>
80+
<artifactId>hibernate-search-util-internal-integrationtest-backend-lucene</artifactId>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>com.h2database</groupId>
85+
<artifactId>h2</artifactId>
86+
</dependency>
87+
</dependencies>
88+
89+
<build>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-compiler-plugin</artifactId>
94+
</plugin>
95+
<plugin>
96+
<groupId>org.moditect</groupId>
97+
<artifactId>moditect-maven-plugin</artifactId>
98+
<executions>
99+
<execution>
100+
<id>add-module-infos</id>
101+
<!-- Do not generate module-info with moditect-maven-plugin:
102+
This module depends on Lucene jars that have split packages module issue. -->
103+
<phase>none</phase>
104+
</execution>
105+
</executions>
106+
</plugin>
107+
</plugins>
108+
</build>
109+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.metamodel.generator.plugin;
8+
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.util.Collection;
15+
import java.util.List;
16+
import java.util.Objects;
17+
import java.util.Properties;
18+
19+
import org.hibernate.SessionFactory;
20+
import org.hibernate.search.engine.backend.metamodel.IndexDescriptor;
21+
import org.hibernate.search.engine.backend.metamodel.IndexFieldDescriptor;
22+
import org.hibernate.search.mapper.orm.Search;
23+
import org.hibernate.search.mapper.orm.entity.SearchIndexedEntity;
24+
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
25+
import org.hibernate.search.util.impl.integrationtest.backend.lucene.LuceneBackendConfiguration;
26+
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
27+
28+
import org.apache.maven.model.Dependency;
29+
import org.apache.maven.plugin.AbstractMojo;
30+
import org.apache.maven.plugin.MojoExecutionException;
31+
import org.apache.maven.plugin.MojoFailureException;
32+
import org.apache.maven.plugins.annotations.LifecyclePhase;
33+
import org.apache.maven.plugins.annotations.Mojo;
34+
import org.apache.maven.plugins.annotations.Parameter;
35+
import org.apache.maven.project.MavenProject;
36+
37+
@Mojo(name = "generate-metamodel", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
38+
public class HibernateSearchMetamodelGeneratorMojo extends AbstractMojo {
39+
40+
@Parameter(defaultValue = "${project}", required = true, readonly = true)
41+
MavenProject project;
42+
43+
@Parameter(property = "annotatedTypes")
44+
List<String> annotatedTypes;
45+
46+
@Parameter(property = "properties")
47+
Properties properties;
48+
49+
@Override
50+
public void execute() throws MojoExecutionException, MojoFailureException {
51+
getLog().info( "Hibernate Search Metamodel Generator" );
52+
getLog().info( "Dependencies: " + project.getDependencies() );
53+
54+
if ( hasOrmMapper( project.getDependencies() ) ) {
55+
getLog().info( "Sources: " + project.getCompileSourceRoots() );
56+
OrmSetupHelper.SetupContext setupContext =
57+
OrmSetupHelper.withSingleBackend( new LuceneBackendConfiguration() ).start();
58+
59+
properties.forEach( (k, v) -> setupContext.withProperty( Objects.toString( k ), v ) );
60+
61+
Path generatedMetamodelLocation =
62+
Path.of( project.getBuild().getOutputDirectory() ).resolveSibling( "generated-metamodel-sources" );
63+
project.addCompileSourceRoot( generatedMetamodelLocation.toString() );
64+
65+
66+
try ( SessionFactory sessionFactory = setupContext.setup( annotatedTypes() ) ) {
67+
SearchMapping mapping = Search.mapping( sessionFactory );
68+
69+
Collection<? extends SearchIndexedEntity<?>> indexedEntities = mapping.allIndexedEntities();
70+
71+
for ( SearchIndexedEntity<?> indexedEntity : indexedEntities ) {
72+
createClass( indexedEntity, generatedMetamodelLocation );
73+
}
74+
75+
getLog().info( "Indexed entities: " + indexedEntities );
76+
77+
}
78+
79+
}
80+
}
81+
82+
private void createClass(SearchIndexedEntity<?> indexedEntity, Path root) {
83+
getLog().info( "Creating class for entity: " + indexedEntity.jpaName() );
84+
85+
IndexDescriptor descriptor = indexedEntity.indexManager().descriptor();
86+
87+
StringBuilder fields = new StringBuilder();
88+
89+
for ( IndexFieldDescriptor staticField : descriptor.staticFields() ) {
90+
fields.append( '\n' )
91+
.append( '\t' ).append( "public String " ).append( staticField.relativeName() ).append( ";" );
92+
}
93+
94+
try {
95+
Class<?> javaClass = indexedEntity.javaClass();
96+
Path pckg = root.resolve( Path.of( javaClass.getPackageName().replace( '.', '/' ) ) );
97+
Files.createDirectories( pckg );
98+
try ( FileOutputStream os =
99+
new FileOutputStream( pckg.resolve( javaClass.getSimpleName() + ".java" ).toFile() ); ) {
100+
os.write( new StringBuilder().append( "package " ).append( javaClass.getPackageName() ).append( ";\n\n" )
101+
.append( "class " ).append( javaClass.getSimpleName() ).append( "__ {\n" )
102+
.append( fields )
103+
.append( "\t\n}" )
104+
.toString().getBytes( StandardCharsets.UTF_8 ) );
105+
}
106+
107+
}
108+
catch (IOException e) {
109+
throw new RuntimeException( e );
110+
}
111+
112+
}
113+
114+
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 ) );
119+
}
120+
return types;
121+
}
122+
catch (ClassNotFoundException e) {
123+
throw new RuntimeException( e );
124+
}
125+
}
126+
127+
private boolean hasOrmMapper(List<Dependency> dependencies) {
128+
for ( Dependency dependency : dependencies ) {
129+
if ( "hibernate-search-mapper-orm".equals( dependency.getArtifactId() ) ) {
130+
return true;
131+
}
132+
}
133+
return false;
134+
}
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.hibernate.search</groupId>
6+
<artifactId>hibernate-search-metamodel-generator-test-parent</artifactId>
7+
<version>7.2.0-SNAPSHOT</version>
8+
<relativePath>..</relativePath>
9+
</parent>
10+
<artifactId>hibernate-search-metamodel-generator-test-generation</artifactId>
11+
12+
<name>Hibernate Search Metamodel Generator TEST</name>
13+
14+
<properties>
15+
<!-- This is a publicly distributed module that should be published: -->
16+
<deploy.skip>false</deploy.skip>
17+
<java.module.name>org.hibernate.search.metamodel.generator.test</java.module.name>
18+
<org.hibernate.search.integrationtest.orm.database.kind>h2</org.hibernate.search.integrationtest.orm.database.kind>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.hibernate.search</groupId>
24+
<artifactId>hibernate-search-metamodel-generator-model</artifactId>
25+
<version>${project.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.hibernate.search</groupId>
29+
<artifactId>hibernate-search-mapper-orm</artifactId>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.hibernate.search</groupId>
37+
<artifactId>hibernate-search-metamodel-generator-plugin</artifactId>
38+
<version>${project.version}</version>
39+
<executions>
40+
<execution>
41+
<id>generate</id>
42+
<goals>
43+
<goal>generate-metamodel</goal>
44+
</goals>
45+
<phase>generate-sources</phase>
46+
<configuration>
47+
<annotatedTypes>
48+
<type>org.hibernate.search.metamodel.generator.model.MyEntity</type>
49+
<type>org.hibernate.search.metamodel.generator.model.MyProgrammaticEntity</type>
50+
</annotatedTypes>
51+
<properties>
52+
<hibernate.search.mapping.configurer>org.hibernate.search.metamodel.generator.model.MyConfigurer</hibernate.search.mapping.configurer>
53+
</properties>
54+
</configuration>
55+
</execution>
56+
</executions>
57+
<dependencies>
58+
<dependency>
59+
<groupId>org.hibernate.search</groupId>
60+
<artifactId>hibernate-search-metamodel-generator-model</artifactId>
61+
<version>${project.version}</version>
62+
</dependency>
63+
</dependencies>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
</plugin>
69+
<plugin>
70+
<groupId>org.moditect</groupId>
71+
<artifactId>moditect-maven-plugin</artifactId>
72+
<executions>
73+
<execution>
74+
<id>add-module-infos</id>
75+
<!-- Do not generate module-info with moditect-maven-plugin -->
76+
<phase>none</phase>
77+
</execution>
78+
</executions>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
</project>
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.hibernate.search</groupId>
6+
<artifactId>hibernate-search-metamodel-generator-test-parent</artifactId>
7+
<version>7.2.0-SNAPSHOT</version>
8+
<relativePath>..</relativePath>
9+
</parent>
10+
<artifactId>hibernate-search-metamodel-generator-model</artifactId>
11+
12+
<name>Hibernate Search Metamodel Generator Model</name>
13+
14+
<properties>
15+
<!-- This is a publicly distributed module that should be published: -->
16+
<deploy.skip>false</deploy.skip>
17+
<java.module.name>org.hibernate.search.metamodel.generator</java.module.name>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.hibernate.search</groupId>
23+
<artifactId>hibernate-search-engine</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.hibernate.search</groupId>
27+
<artifactId>hibernate-search-mapper-orm</artifactId>
28+
<optional>true</optional>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.hibernate.search</groupId>
32+
<artifactId>hibernate-search-mapper-pojo-standalone</artifactId>
33+
<optional>true</optional>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.jboss.logging</groupId>
37+
<artifactId>jboss-logging</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.jboss.logging</groupId>
41+
<artifactId>jboss-logging-annotations</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.hibernate.search</groupId>
46+
<artifactId>hibernate-search-util-internal-test-common</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
</plugin>
57+
<plugin>
58+
<groupId>org.moditect</groupId>
59+
<artifactId>moditect-maven-plugin</artifactId>
60+
<executions>
61+
<execution>
62+
<id>add-module-infos</id>
63+
<!-- Do not generate module-info with moditect-maven-plugin -->
64+
<phase>none</phase>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>

0 commit comments

Comments
 (0)