Skip to content

Commit 2a9f4c4

Browse files
committed
HHH-18828 - Migrate to hisrc-basicjaxb for xjc plugins
1 parent 839793e commit 2a9f4c4

20 files changed

+344
-15
lines changed

hibernate-core/hibernate-core.gradle

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.apache.tools.ant.filters.ReplaceTokens
88

99
plugins {
10-
id 'org.hibernate.build.xjc-jakarta'
10+
id "local-xjc-plugin"
1111
}
1212

1313
description = 'Hibernate\'s core ORM functionality'
@@ -87,10 +87,6 @@ dependencies {
8787
// dependency here.
8888
antlr libs.antlr
8989
antlr libs.antlrRuntime
90-
91-
xjc jakartaLibs.xjc
92-
xjc jakartaLibs.jaxb
93-
xjc rootProject.fileTree(dir: 'patched-libs/jaxb2-basics', include: '*.jar')
9490
}
9591

9692
jar {

hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
1010
xmlns="http://www.hibernate.org/xsd/orm/hbm"
1111
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
12-
xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
12+
xmlns:simplify="http://jvnet.org/basicjaxb/xjc/simplify"
1313
targetNamespace="http://www.hibernate.org/xsd/orm/hbm"
1414
elementFormDefault="qualified"
1515
jaxb:extensionBindingPrefixes="simplify"

hibernate-core/src/main/xjb/hbm-mapping-bindings.xjb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<jaxb:bindings xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
4-
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
5-
xmlns:xs="http://www.w3.org/2001/XMLSchema"
6-
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
7-
jaxb:extensionBindingPrefixes="inheritance"
8-
version="3.0">
4+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
5+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
6+
xmlns:inheritance="http://jvnet.org/basicjaxb/xjc/inheritance"
7+
jaxb:extensionBindingPrefixes="inheritance"
8+
version="3.0">
99

1010
<jaxb:bindings schemaLocation="../resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd" node="/xsd:schema">
1111

hibernate-core/src/main/xjb/mapping-bindings.xjb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
55
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
66
jaxb:extensionBindingPrefixes="inheritance simplify"
7-
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
8-
xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
7+
xmlns:inheritance="http://jvnet.org/basicjaxb/xjc/inheritance"
8+
xmlns:simplify="http://jvnet.org/basicjaxb/xjc/simplify"
99
version="3.0">
1010

1111
<bindings schemaLocation="../resources/org/hibernate/xsd/mapping/mapping-7.0.xsd" node="/xsd:schema">

local-build-plugins/build.gradle

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
55
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
66
*/
7+
plugins {
8+
id "java-gradle-plugin"
9+
id "groovy"
10+
}
11+
712
repositories {
813
mavenCentral()
914
}
1015

11-
apply plugin: 'java-gradle-plugin'
12-
1316
group = 'org.hibernate.build'
1417
version = '1.0.0-SNAPSHOT'
1518
buildDir = "target"
1619

1720
dependencies {
1821
implementation gradleApi()
22+
23+
implementation "jakarta.inject:jakarta.inject-api:2.0.0"
24+
1925
implementation 'io.smallrye:jandex:3.1.2'
2026
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
2127
implementation 'jakarta.json.bind:jakarta.json.bind-api:2.0.0'
@@ -89,6 +95,10 @@ gradlePlugin {
8995
id = "org.hibernate.build.maven-embedder"
9096
implementationClass = "org.hibernate.build.maven.embedder.MavenEmbedderPlugin"
9197
}
98+
register( "xjc-plugin" ) {
99+
id = "local-xjc-plugin"
100+
implementationClass = "org.hibernate.build.xjc.XjcPlugin"
101+
}
92102
}
93103
}
94104

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.hibernate.build.xjc;
2+
3+
import org.gradle.api.Named;
4+
import org.gradle.api.Project;
5+
import org.gradle.api.file.RegularFileProperty;
6+
import org.gradle.api.provider.SetProperty;
7+
import org.gradle.api.tasks.Input;
8+
import org.gradle.api.tasks.InputFile;
9+
10+
import java.util.Arrays;
11+
import java.util.HashSet;
12+
import java.util.Set;
13+
14+
/**
15+
* @author Steve Ebersole
16+
*/
17+
public class SchemaDescriptor implements Named {
18+
private final String name;
19+
private final Project project;
20+
21+
private final RegularFileProperty xsdFile;
22+
private final RegularFileProperty xjcBindingFile;
23+
private final SetProperty<String> xjcExtensions;
24+
25+
public SchemaDescriptor(String name, Project project) {
26+
this.name = name;
27+
this.project = project;
28+
29+
xsdFile = project.getObjects().fileProperty();
30+
xjcBindingFile = project.getObjects().fileProperty();
31+
xjcExtensions = project.getObjects().setProperty( String.class );
32+
}
33+
34+
@Override
35+
public final String getName() {
36+
return name;
37+
}
38+
39+
@InputFile
40+
public RegularFileProperty getXsdFile() {
41+
return xsdFile;
42+
}
43+
44+
public void setXsdFile(Object reference) {
45+
xsdFile.set( project.file( reference ) );
46+
}
47+
48+
public void xsdFile(Object reference) {
49+
setXsdFile( reference );
50+
}
51+
52+
@InputFile
53+
public RegularFileProperty getXjcBindingFile() {
54+
return xjcBindingFile;
55+
}
56+
57+
public void setXjcBindingFile(Object reference) {
58+
xjcBindingFile.set( project.file( reference ) );
59+
}
60+
61+
public void xjcBindingFile(Object reference) {
62+
setXjcBindingFile( reference );
63+
}
64+
65+
@Input
66+
public SetProperty<String> ___xjcExtensions() {
67+
return xjcExtensions;
68+
}
69+
70+
public Set<String> getXjcExtensions() {
71+
return xjcExtensions.get();
72+
}
73+
74+
public void setXjcExtensions(Set<String> xjcExtensions) {
75+
this.xjcExtensions.set( xjcExtensions );
76+
}
77+
78+
public void setXjcExtensions(String... xjcExtensions) {
79+
xjcExtensions( xjcExtensions );
80+
}
81+
82+
public void xjcExtensions(String... xjcExtensions) {
83+
setXjcExtensions( new HashSet<>( Arrays.asList( xjcExtensions) ) );
84+
}
85+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.hibernate.build.xjc;
2+
3+
import org.gradle.api.NamedDomainObjectFactory;
4+
import org.gradle.api.Project;
5+
import org.gradle.api.Task;
6+
import org.gradle.api.file.Directory;
7+
import org.gradle.api.provider.Provider;
8+
import org.gradle.api.tasks.SourceSet;
9+
import org.gradle.api.tasks.SourceSetContainer;
10+
import org.gradle.api.tasks.TaskProvider;
11+
12+
import static org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME;
13+
14+
/**
15+
* Used as the factory for instances added to the {@link XjcExtension#getSchemas()} container.
16+
* <p>
17+
* For each schema descriptor, an XjcTask is created and wired up.
18+
*
19+
* @author Steve Ebersole
20+
*/
21+
public class SchemaDescriptorFactory implements NamedDomainObjectFactory<SchemaDescriptor> {
22+
private final XjcExtension xjcExtension;
23+
private final Task groupingTask;
24+
private final Project project;
25+
26+
public SchemaDescriptorFactory(XjcExtension xjcExtension, Task groupingTask, Project project) {
27+
this.xjcExtension = xjcExtension;
28+
this.groupingTask = groupingTask;
29+
this.project = project;
30+
}
31+
32+
@Override
33+
public SchemaDescriptor create(String name) {
34+
final SchemaDescriptor schemaDescriptor = new SchemaDescriptor( name, project );
35+
36+
final String taskName = determineXjcTaskName( schemaDescriptor );
37+
final Provider<Directory> taskOutputDirectory = xjcExtension.getOutputDirectory().dir( name );
38+
39+
// register the XjcTask for the schema
40+
final TaskProvider<XjcTask> xjcTaskRef = project.getTasks().register( taskName, XjcTask.class, (task) -> {
41+
task.setGroup( "xjc" );
42+
task.setDescription( "XJC generation for the " + name + " descriptor" );
43+
44+
// wire up the inputs and outputs
45+
task.getXsdFile().set( schemaDescriptor.getXsdFile() );
46+
task.getXjcBindingFile().set( schemaDescriptor.getXjcBindingFile() );
47+
task.getXjcExtensions().set( schemaDescriptor.___xjcExtensions() );
48+
task.getOutputDirectory().set( taskOutputDirectory );
49+
} );
50+
51+
final SourceSetContainer sourceSets = project.getExtensions().getByType( SourceSetContainer.class );
52+
final SourceSet mainSourceSet = sourceSets.getByName( MAIN_SOURCE_SET_NAME );
53+
mainSourceSet.getJava().srcDir( xjcTaskRef );
54+
55+
groupingTask.dependsOn( xjcTaskRef );
56+
57+
return schemaDescriptor;
58+
}
59+
60+
private static String determineXjcTaskName(SchemaDescriptor schemaDescriptor) {
61+
assert schemaDescriptor.getName() != null;
62+
63+
final char initialLetterCap = Character.toUpperCase( schemaDescriptor.getName().charAt( 0 ) );
64+
final String rest = schemaDescriptor.getName().substring( 1 );
65+
66+
return "xjc" + initialLetterCap + rest;
67+
}
68+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.hibernate.build.xjc;
2+
3+
import groovy.lang.Closure;
4+
import jakarta.inject.Inject;
5+
import org.gradle.api.NamedDomainObjectContainer;
6+
import org.gradle.api.Project;
7+
import org.gradle.api.Task;
8+
import org.gradle.api.file.DirectoryProperty;
9+
import org.gradle.api.provider.Property;
10+
import org.gradle.api.tasks.OutputDirectory;
11+
12+
/**
13+
* @author Steve Ebersole
14+
*/
15+
public abstract class XjcExtension {
16+
private final DirectoryProperty outputDirectory;
17+
private final Property<String> jaxbBasicsVersion;
18+
private final NamedDomainObjectContainer<SchemaDescriptor> schemas;
19+
20+
@Inject
21+
public XjcExtension(Task groupingTask, Project project) {
22+
outputDirectory = project.getObjects().directoryProperty();
23+
outputDirectory.convention( project.getLayout().getBuildDirectory().dir( "generated/sources/xjc/main" ) );
24+
25+
jaxbBasicsVersion = project.getObjects().property( String.class );
26+
jaxbBasicsVersion.convention( "2.2.1" );
27+
28+
// Create a dynamic container for SchemaDescriptor definitions by the user.
29+
// - for each schema they define, create a Task to perform the "compilation"
30+
schemas = project.container( SchemaDescriptor.class, new SchemaDescriptorFactory( this, groupingTask, project ) );
31+
}
32+
33+
@OutputDirectory
34+
public DirectoryProperty getOutputDirectory() {
35+
return outputDirectory;
36+
}
37+
38+
public Property<String> getJaxbBasicsVersion() {
39+
return jaxbBasicsVersion;
40+
}
41+
42+
@SuppressWarnings("unused")
43+
public final NamedDomainObjectContainer<SchemaDescriptor> getSchemas() {
44+
return schemas;
45+
}
46+
47+
@SuppressWarnings({ "unused", "rawtypes" })
48+
public NamedDomainObjectContainer<SchemaDescriptor> schemas(Closure closure) {
49+
return schemas.configure( closure );
50+
}
51+
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.hibernate.build.xjc;
2+
3+
import org.gradle.api.Plugin;
4+
import org.gradle.api.Project;
5+
import org.gradle.api.Task;
6+
import org.gradle.api.artifacts.Configuration;
7+
import org.gradle.api.artifacts.dsl.DependencyHandler;
8+
9+
import java.util.LinkedHashMap;
10+
11+
/**
12+
* @author Steve Ebersole
13+
*/
14+
public class XjcPlugin implements Plugin<Project> {
15+
public static final String XJC_BASIC_PLUGIN = "org.patrodyne.jvnet:hisrc-basicjaxb-plugins:2.2.1";
16+
public static final String XJC_BASIC_TOOLS = "org.patrodyne.jvnet:hisrc-basicjaxb-tools:2.2.1";
17+
public static final String XJC_BASIC_ANT = "org.patrodyne.jvnet:hisrc-basicjaxb-ant:2.2.1";
18+
19+
public static final String ANT_TASK_NAME = "org.jvnet.basicjaxb.xjc.XJC2Task";
20+
21+
@Override
22+
public void apply(Project project) {
23+
// Create the xjc grouping task
24+
final Task groupingTask = project.getTasks().create( "xjc", xjcTask -> {
25+
xjcTask.setGroup( "xjc" );
26+
xjcTask.setDescription( "Grouping task for executing one-or-more XJC compilations" );
27+
} );
28+
29+
// Create the Plugin extension object (for users to configure our execution).
30+
project.getExtensions().create( "xjc", XjcExtension.class, groupingTask, project );
31+
32+
final DependencyHandler dependencyHandler = project.getDependencies();
33+
final Configuration antTaskDependencies = project.getConfigurations().detachedConfiguration(
34+
dependencyHandler.create( XJC_BASIC_ANT ),
35+
dependencyHandler.create( XJC_BASIC_PLUGIN ),
36+
dependencyHandler.create( XJC_BASIC_TOOLS ),
37+
dependencyHandler.gradleApi()
38+
);
39+
40+
final LinkedHashMap<String, String> map = new LinkedHashMap<>( 3 );
41+
map.put( "name", "xjc" );
42+
map.put( "classname", ANT_TASK_NAME );
43+
map.put( "classpath", antTaskDependencies.getAsPath() );
44+
project.getAnt().invokeMethod( "taskdef", new Object[] { map } );
45+
project.getAnt().setSaveStreams( false );
46+
}
47+
}

0 commit comments

Comments
 (0)