Skip to content

Commit 745137a

Browse files
committed
HBX-3234: Improve the Maven hbm2orm Mojo
- Add default pretty printing of the resulting mapping.xml file Signed-off-by: Koen Aers <[email protected]>
1 parent 718dbf7 commit 745137a

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,34 @@
1212
import java.util.Objects;
1313

1414
import org.apache.maven.cli.MavenCli;
15+
import org.hibernate.tool.api.version.Version;
16+
import org.junit.jupiter.api.BeforeAll;
1517
import org.junit.jupiter.api.Test;
1618
import org.junit.jupiter.api.io.TempDir;
1719

1820
public class TransformHbmTestIT {
1921

2022
public static final String MVN_HOME = "maven.multiModuleProjectDirectory";
23+
private static File baseFolder;
24+
private static File localRepo;
2125

2226
@TempDir
2327
private Path projectPath;
2428

29+
@BeforeAll
30+
public static void beforeAll() throws Exception {
31+
// The needed resource for this test are put in place
32+
// in the 'baseFolder' (normally 'target/test-classes')
33+
// by the 'build-helper-maven-plugin' execution.
34+
// See the 'pom.xml'
35+
baseFolder = determineBaseFolder();
36+
localRepo = new File(baseFolder.getParentFile(), "local-repo");
37+
}
2538
@Test
2639
public void testSimpleHbmTransformation() throws Exception {
2740
System.setProperty(MVN_HOME, projectPath.toAbsolutePath().toString());
2841
writePomFile();
29-
copyHbmFile();
30-
42+
copyHbmFile();
3143
runTransformHbmToOrm();
3244
}
3345

@@ -51,16 +63,29 @@ private void copyHbmFile() throws Exception {
5163
assertTrue(destinationFile.exists());
5264
}
5365

54-
private void runTransformHbmToOrm() {
66+
private void runTransformHbmToOrm() throws Exception {
5567
File destinationDir = new File(projectPath.toFile(), "src/main/resources/");
5668
File ormXmlFile = new File(destinationDir, "simple.mapping.xml");
5769
assertFalse(ormXmlFile.exists());
5870
new MavenCli().doMain(
59-
new String[]{"org.hibernate.tool:hibernate-tools-maven:7.2.0.CR2:hbm2orm", "generate-sources"},
71+
new String[]{
72+
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
73+
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm",
74+
"generate-sources"},
6075
projectPath.toAbsolutePath().toString(),
6176
null,
6277
null);
78+
// Check the existaence of the transformed file
6379
assertTrue(ormXmlFile.exists());
80+
// Check if it's pretty printed
81+
assertTrue(Files.readString(ormXmlFile.toPath()).contains("\n <table name=\"Foo\"/>\n"));
82+
}
83+
84+
private static File determineBaseFolder() throws Exception {
85+
URL classUrl = TransformHbmTestIT.class.getResource(
86+
"/" + TransformHbmTestIT.class.getName().replace(".", "/") + ".class");
87+
return new File(classUrl.toURI())
88+
.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile();
6489
}
6590

6691
private static final String simplePomContents =

maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
import jakarta.xml.bind.JAXBException;
5050
import jakarta.xml.bind.Marshaller;
51+
import org.hibernate.tool.api.xml.XMLPrettyPrinter;
5152

5253
@Mojo(
5354
name = "hbm2orm",
@@ -58,6 +59,9 @@ public class TransformHbmMojo extends AbstractMojo {
5859
@Parameter(defaultValue = "${project.basedir}/src/main/resources")
5960
private File inputFolder;
6061

62+
@Parameter(defaultValue = "true")
63+
private boolean format;
64+
6165
@Override
6266
public void execute() {
6367
MappingBinder mappingBinder = new MappingBinder(
@@ -119,12 +123,19 @@ private void marshall(Marshaller marshaller, JaxbEntityMappingsImpl mappings, Fi
119123
getLog().info("Marshalling file: " + hbmXmlFile.getAbsolutePath() + " into " + mappingXmlFile.getAbsolutePath());
120124
try {
121125
marshaller.marshal( mappings, mappingXmlFile );
126+
if (format) {
127+
XMLPrettyPrinter.prettyPrintFile(mappingXmlFile);
128+
}
122129
}
123130
catch (JAXBException e) {
124131
throw new RuntimeException(
125132
"Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`",
126-
e
127-
);
133+
e);
134+
}
135+
catch (IOException e) {
136+
throw new RuntimeException(
137+
"Unable to format XML file `" + mappingXmlFile.getAbsolutePath() + "`",
138+
e);
128139
}
129140
}
130141

0 commit comments

Comments
 (0)