Skip to content

Commit 628809a

Browse files
committed
HBX-2936 Use maven-injection-plugin to set the version string in the org.hibernate.tool.api.version.Version
1 parent 0584bc4 commit 628809a

File tree

5 files changed

+126
-96
lines changed

5 files changed

+126
-96
lines changed

orm/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,26 @@
8787
</dependency>
8888
</dependencies>
8989

90+
<build>
91+
<plugins>
92+
<plugin>
93+
<groupId>org.jboss.maven.plugins</groupId>
94+
<artifactId>maven-injection-plugin</artifactId>
95+
<configuration>
96+
<bytecodeInjections>
97+
<bytecodeInjection>
98+
<expression>${project.version}</expression>
99+
<targetMembers>
100+
<methodBodyReturn>
101+
<className>org.hibernate.tool.api.version.Version</className>
102+
<methodName>versionString</methodName>
103+
</methodBodyReturn>
104+
</targetMembers>
105+
</bytecodeInjection>
106+
</bytecodeInjections>
107+
</configuration>
108+
</plugin>
109+
</plugins>
110+
</build>
111+
90112
</project>
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package org.hibernate.tool.api.version;
22

33
public interface Version {
4-
5-
final static String CURRENT_VERSION = "7.0.0-SNAPSHOT";
64

5+
/**
6+
* @deprecated Use {@link #versionString()} instead.
7+
*/
8+
@Deprecated
9+
final static String CURRENT_VERSION = versionString();
10+
11+
static String versionString() {
12+
// This implementation is replaced during the build with another one that returns the correct value.
13+
return "UNKNOWN";
14+
}
715
}

orm/src/main/java/org/hibernate/tool/internal/export/common/TemplateHelper.java

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535

3636

3737
/**
38-
*
38+
*
3939
* Helper and wrapper for a Template engine (currently only FreeMarker).
40-
* Exposes only the essential functions to avoid too much coupling else where.
41-
*
40+
* Exposes only the essential functions to avoid too much coupling else where.
41+
*
4242
* @author max
4343
*
4444
*/
4545
public class TemplateHelper {
46-
46+
4747
static final Logger log = Logger.getLogger(TemplateHelper.class);
48-
48+
4949
private String templatePrefix;
5050
private File outputDirectory;
5151

@@ -54,17 +54,17 @@ public class TemplateHelper {
5454
protected SimpleHash context;
5555

5656
public TemplateHelper() {
57-
57+
5858
}
59-
59+
6060
public void init(File outputDirectory, String[] templatePaths) {
6161
this.outputDirectory = outputDirectory;
62-
62+
6363
context = new SimpleHash(new BeansWrapperBuilder(Configuration.VERSION_2_3_0).build());
6464
freeMarkerEngine = new Configuration(Configuration.VERSION_2_3_0);
65-
65+
6666
List<TemplateLoader> loaders = new ArrayList<TemplateLoader>();
67-
67+
6868
for (int i = 0; i < templatePaths.length; i++) {
6969
File file = new File(templatePaths[i]);
7070
if(file.exists() && file.isDirectory()) {
@@ -79,14 +79,14 @@ public void init(File outputDirectory, String[] templatePaths) {
7979
}
8080
}
8181
loaders.add(new ClassTemplateLoader(this.getClass(),"/")); // the template names are like pojo/Somewhere so have to be a rooted classpathloader
82-
82+
8383
freeMarkerEngine.setTemplateLoader(new MultiTemplateLoader((TemplateLoader[]) loaders.toArray(new TemplateLoader[loaders.size()])));
84-
84+
8585
}
86-
87-
86+
87+
8888
public class Templates {
89-
89+
9090
public void createFile(String content, String fileName) {
9191
Writer fw = null;
9292
try {
@@ -106,13 +106,13 @@ public void createFile(String content, String fileName) {
106106
}
107107
}
108108
}
109-
109+
110110
public File getOutputDirectory() {
111111
return outputDirectory;
112112
}
113113

114-
115-
114+
115+
116116
public void putInContext(String key, Object value) {
117117
log.trace("putInContext " + key + "=" + value);
118118
if(value == null) throw new IllegalStateException("value must not be null for " + key);
@@ -121,7 +121,7 @@ public void putInContext(String key, Object value) {
121121
log.warn( "Overwriting " + replaced + " when setting " + key + " to " + value + ".");
122122
}
123123
}
124-
124+
125125
public void removeFromContext(String key, Object expected) {
126126
log.trace("removeFromContext " + key + "=" + expected);
127127
Object replaced = internalRemoveFromContext(key);
@@ -130,7 +130,7 @@ public void removeFromContext(String key, Object expected) {
130130
throw new IllegalStateException("expected " + key + " to be bound to " + expected + " but was to " + replaced);
131131
}*/
132132
}
133-
133+
134134
public void ensureExistence(File destination) {
135135
// if the directory exists, make sure it is a directory
136136
File dir = destination.getAbsoluteFile().getParentFile();
@@ -147,8 +147,8 @@ else if ( !dir.exists() ) {
147147
throw new RuntimeException( "unable to create directory: " + dir.getAbsolutePath() );
148148
}
149149
}
150-
}
151-
150+
}
151+
152152
protected String getTemplatePrefix() {
153153
return templatePrefix;
154154
}
@@ -158,33 +158,33 @@ protected SimpleHash getContext() {
158158
}
159159

160160
public void processString(String template, Writer output) {
161-
161+
162162
try {
163163
Reader r = new StringReader(template);
164164
Template t = new Template("unknown", r, freeMarkerEngine);
165-
166-
t.process(getContext(), output);
167-
}
165+
166+
t.process(getContext(), output);
167+
}
168168
catch (IOException e) {
169169
throw new RuntimeException("Error while processing template string", e);
170-
}
170+
}
171171
catch (TemplateException te) {
172172
throw new RuntimeException("Error while processing template string", te);
173173
}
174174
catch (Exception e) {
175175
throw new RuntimeException("Error while processing template string", e);
176176
}
177177
}
178-
178+
179179
public void setupContext() {
180-
getContext().put("version", Version.CURRENT_VERSION);
180+
getContext().put("version", Version.versionString());
181181
getContext().put("ctx", getContext() ); //TODO: I would like to remove this, but don't know another way to actually get the list possible "root" keys for debugging.
182182
getContext().put("templates", new Templates());
183-
184-
getContext().put("date", new SimpleDate(new Date(), TemplateDateModel.DATETIME));
185-
183+
184+
getContext().put("date", new SimpleDate(new Date(), TemplateDateModel.DATETIME));
185+
186186
}
187-
187+
188188
protected Object internalPutInContext(String key, Object value) {
189189
TemplateModel model = null;
190190
try {
@@ -196,7 +196,7 @@ protected Object internalPutInContext(String key, Object value) {
196196
getContext().put(key, value);
197197
return model;
198198
}
199-
199+
200200
protected Object internalRemoveFromContext(String key) {
201201
TemplateModel model = null;
202202
try {
@@ -208,54 +208,54 @@ protected Object internalRemoveFromContext(String key) {
208208
getContext().remove(key);
209209
return model;
210210
}
211-
211+
212212
/** look up the template named templateName via the paths and print the content to the output */
213213
public void processTemplate(String templateName, Writer output, String rootContext) {
214214
if(rootContext == null) {
215215
rootContext = "Unknown context";
216216
}
217-
217+
218218
try {
219219
Template template = freeMarkerEngine.getTemplate(templateName);
220-
template.process(getContext(), output);
221-
}
220+
template.process(getContext(), output);
221+
}
222222
catch (IOException e) {
223223
throw new RuntimeException("Error while processing " + rootContext + " with template " + templateName, e);
224224
}
225-
catch (TemplateException te) {
225+
catch (TemplateException te) {
226226
throw new RuntimeException("Error while processing " + rootContext + " with template " + templateName, te);
227-
}
227+
}
228228
catch (Exception e) {
229229
throw new RuntimeException("Error while processing " + rootContext + " with template " + templateName, e);
230-
}
230+
}
231231
}
232-
233-
232+
233+
234234
/**
235235
* Check if the template exists. Tries to search with the templatePrefix first and then secondly without the template prefix.
236-
*
236+
*
237237
* @param name
238238
* @return
239-
*/
239+
*/
240240
/*protected String getTemplateName(String name) {
241241
if(!name.endsWith(".ftl")) {
242-
name = name + ".ftl";
242+
name = name + ".ftl";
243243
}
244-
244+
245245
if(getTemplatePrefix()!=null && templateExists(getTemplatePrefix() + name)) {
246246
return getTemplatePrefix() + name;
247-
}
248-
247+
}
248+
249249
if(templateExists(name)) {
250250
return name;
251-
}
252-
251+
}
252+
253253
throw new ExporterException("Could not find template with name: " + name);
254254
}*/
255-
255+
256256
public boolean templateExists(String templateName) {
257257
TemplateLoader templateLoader = freeMarkerEngine.getTemplateLoader();
258-
258+
259259
try {
260260
return templateLoader.findTemplateSource(templateName)!=null;
261261
}

test/nodb/src/test/java/org/hibernate/tool/VersionTest/TestCase.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Hibernate Tools, Tooling for your Hibernate Projects
3-
*
3+
*
44
* Copyright 2017-2020 Red Hat, Inc.
55
*
6-
* Licensed under the GNU Lesser General Public License (LGPL),
6+
* Licensed under the GNU Lesser General Public License (LGPL),
77
* version 2.1 or later (the "License").
88
* You may not use this file except in compliance with the License.
9-
* You may read the licence in the 'lgpl.txt' file in the root folder of
9+
* You may read the licence in the 'lgpl.txt' file in the root folder of
1010
* project or obtain a copy at
1111
*
1212
* http://www.gnu.org/licenses/lgpl-2.1.html
@@ -41,16 +41,16 @@
4141
import org.xml.sax.InputSource;
4242

4343
public class TestCase {
44-
44+
4545
@Test
4646
public void testVersion() throws Exception {
4747
assertEquals(
48-
org.hibernate.tool.api.version.Version.CURRENT_VERSION,
48+
org.hibernate.tool.api.version.Version.versionString(),
4949
extractVersion(getPomXml()));
5050
}
51-
51+
5252
private String getPomXml() throws Exception {
53-
CodeSource codeSource =
53+
CodeSource codeSource =
5454
org.hibernate.tool.api.version.Version.class.getProtectionDomain().getCodeSource();
5555
String path = codeSource.getLocation().getPath();
5656
String result = null;
@@ -68,18 +68,18 @@ private String getPomXml() throws Exception {
6868
result = readFromInputStream(stream);
6969
stream.close();
7070
}
71-
return result;
71+
return result;
7272
}
73-
73+
7474
private String readFromInputStream(InputStream stream) throws Exception {
7575
StringBuffer buffer = new StringBuffer();
7676
int b = -1;
7777
while ((b = stream.read()) != -1) {
7878
buffer.append((char)b);
7979
}
80-
return buffer.toString();
80+
return buffer.toString();
8181
}
82-
82+
8383
private String extractVersion(String pomXml) throws Exception {
8484
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
8585
DocumentBuilder builder = factory.newDocumentBuilder();
@@ -108,7 +108,7 @@ private String extractVersion(String pomXml) throws Exception {
108108
}
109109
}
110110
}
111-
return versionNode.getTextContent();
111+
return versionNode.getTextContent();
112112
}
113113

114114
}

0 commit comments

Comments
 (0)