Skip to content

Commit 3140b4f

Browse files
committed
jdbc: fix hardcoded driver version
Use build-helper-maven-plugin to parse ${project.version} and templating-maven-plugin to substitute placeholders in Version.java. It is possible that IntelliJ IDEA will not detect generated sources automatically, shows 'cannot resolve symbol' errors re Version class and even fails to build the project. In this case execute `mvn clean compile` in the project root from a terminal, switch to IDEA, click right button on the project in the left-hand side panel and choose Maven -> Reimport. There is also the relevant [issue][1] in the JetBrains tracker. [1]: https://youtrack.jetbrains.com/issue/IDEA-161010
1 parent 1e7fc18 commit 3140b4f

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,37 @@
4646
<target>1.6</target>
4747
</configuration>
4848
</plugin>
49+
<!--
50+
Set parsedVersion.majorVersion and parsedVersion.minorVersion
51+
properties.
52+
-->
53+
<plugin>
54+
<groupId>org.codehaus.mojo</groupId>
55+
<artifactId>build-helper-maven-plugin</artifactId>
56+
<version>3.0.0</version>
57+
<executions>
58+
<execution>
59+
<id>parse-version</id>
60+
<goals>
61+
<goal>parse-version</goal>
62+
</goals>
63+
</execution>
64+
</executions>
65+
</plugin>
66+
<!-- Process src/main/java-templates directory. -->
67+
<plugin>
68+
<groupId>org.codehaus.mojo</groupId>
69+
<artifactId>templating-maven-plugin</artifactId>
70+
<version>1.0.0</version>
71+
<executions>
72+
<execution>
73+
<id>filter-src</id>
74+
<goals>
75+
<goal>filter-sources</goal>
76+
</goals>
77+
</execution>
78+
</executions>
79+
</plugin>
4980
<plugin>
5081
<artifactId>maven-surefire-plugin</artifactId>
5182
<version>2.22.0</version>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.tarantool;
2+
3+
public final class Version {
4+
public static final String version = "${project.version}";
5+
public static final int majorVersion = ${parsedVersion.majorVersion};
6+
public static final int minorVersion = ${parsedVersion.minorVersion};
7+
}

src/main/java/org/tarantool/jdbc/SQLDatabaseMetadata.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.List;
1515
import java.util.Map;
1616

17+
import org.tarantool.Version;
1718
import org.tarantool.JDBCBridge;
1819

1920
@SuppressWarnings("Since15")
@@ -114,17 +115,17 @@ public String getDriverName() throws SQLException {
114115

115116
@Override
116117
public String getDriverVersion() throws SQLException {
117-
return "1.8.jdbc";
118+
return Version.version;
118119
}
119120

120121
@Override
121122
public int getDriverMajorVersion() {
122-
return 1;
123+
return Version.majorVersion;
123124
}
124125

125126
@Override
126127
public int getDriverMinorVersion() {
127-
return 8;
128+
return Version.minorVersion;
128129
}
129130

130131
@Override

src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import static org.junit.jupiter.api.Assertions.assertTrue;
1818
import static org.junit.jupiter.api.Assertions.fail;
1919

20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
22+
2023
public class JdbcDatabaseMetaDataIT extends AbstractJdbcIT {
2124
private DatabaseMetaData meta;
2225

@@ -213,4 +216,27 @@ public void execute() throws Throwable {
213216
}
214217
assertEquals(3, i);
215218
}
219+
220+
@Test
221+
public void testGetDriverNameVersion() throws SQLException {
222+
String name = meta.getDriverName();
223+
String version = meta.getDriverVersion();
224+
int majorVersion = meta.getDriverMajorVersion();
225+
int minorVersion = meta.getDriverMinorVersion();
226+
227+
// Verify driver name.
228+
assertEquals("tarantool-java", name);
229+
230+
// Verify driver version format.
231+
// E.g. 1.7.6 or 1.7.6-SNAPSHOT.
232+
Pattern p = Pattern.compile("^(?<major>\\d+)\\.(?<minor>\\d+)\\.\\d+(?:-SNAPSHOT)?$");
233+
Matcher m = p.matcher(version);
234+
assertTrue(m.matches());
235+
236+
// Verify the full version matches major/minor ones.
237+
int majorVersionMatched = Integer.parseInt(m.group("major"));
238+
int minorVersionMatched = Integer.parseInt(m.group("minor"));
239+
assertEquals(majorVersion, majorVersionMatched);
240+
assertEquals(minorVersion, minorVersionMatched);
241+
}
216242
}

0 commit comments

Comments
 (0)