Skip to content

Commit 97c0882

Browse files
committed
Exclude some sqlite native libraries from the jar
These operating systems or architectures are not officially supported, are probably unused, are only relevent to cmdline, and most were only recently added to the sqlite package. But they still contribute significantly to the overall jar size. These exclusions reduces file size by around 7.8 MB. A fallback was added so that a user can still use sqlite on these platforms.
1 parent e84cba4 commit 97c0882

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

pom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,22 @@
700700
<filter>
701701
<artifact>org.xerial:sqlite-jdbc:jar:*</artifact>
702702
<includes>
703-
<include>native/**</include>
704-
<include>org/ibex/**</include>
705703
<include>org/sqlite/**</include>
706704
</includes>
705+
<!-- Native library exclusions for os/arch that are not officially supported -->
706+
<!-- Can still be added by users using the sqlite/native directory -->
707+
<excludes>
708+
<exclude>org/sqlite/native/FreeBSD/**</exclude>
709+
<exclude>org/sqlite/native/Linux-Android/**</exclude>
710+
<exclude>org/sqlite/native/Linux/ppc64/*</exclude>
711+
<exclude>org/sqlite/native/Linux/arm/*</exclude>
712+
<exclude>org/sqlite/native/Linux/armv6/*</exclude>
713+
<exclude>org/sqlite/native/Linux/armv7/*</exclude>
714+
<exclude>org/sqlite/native/Linux/x86/*</exclude>
715+
<exclude>org/sqlite/native/Linux-Musl/x86/*</exclude>
716+
<exclude>org/sqlite/native/Windows/x86/*</exclude>
717+
<exclude>org/sqlite/native/Windows/armv7/*</exclude>
718+
</excludes>
707719
</filter>
708720
<filter>
709721
<artifact>org.apache.commons:commons-io:jar:*</artifact>

src/main/java/com/laytonsmith/database/SQLiteProfile.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.laytonsmith.core.Profiles;
44
import com.laytonsmith.core.MethodScriptFileLocations;
5+
import org.sqlite.SQLiteJDBCLoader;
6+
import org.sqlite.util.OSInfo;
7+
58
import java.io.File;
69
import java.sql.SQLException;
710
import java.util.Map;
@@ -37,6 +40,22 @@ public String getConnectionString() throws SQLException {
3740
} catch (ClassNotFoundException ex) {
3841
throw new SQLException("Cannot load SQLite. Check your installation and try again");
3942
}
43+
// Set native library override path if not already set. (e.g. -Dorg.sqlite.lib.path="/path/to/lib")
44+
if(System.getProperty("org.sqlite.lib.path") == null) {
45+
System.setProperty("org.sqlite.lib.path", new File(MethodScriptFileLocations.getDefault().getConfigDirectory(),
46+
"sqlite/native/" + OSInfo.getNativeLibFolderPathForCurrentOS()).getAbsolutePath());
47+
}
48+
try {
49+
// Load native library before connection to detect when the library is missing.
50+
// This is done in SQLiteDataSource as well.
51+
SQLiteJDBCLoader.initialize();
52+
} catch (Exception ex) {
53+
throw new SQLException("Failed to load a native sqlite library for your platform."
54+
+ " You can download the library file from"
55+
+ " https://github.com/xerial/sqlite-jdbc/tree/master/src/main/resources/org/sqlite/native/"
56+
+ OSInfo.getNativeLibFolderPathForCurrentOS() + " and place it into "
57+
+ System.getProperty("org.sqlite.lib.path"));
58+
}
4059
return "jdbc:sqlite:" + getFile();
4160
}
4261

src/main/java/com/laytonsmith/persistence/SQLiteDataSource.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
import com.laytonsmith.annotations.datasource;
66
import com.laytonsmith.core.MSLog;
77
import com.laytonsmith.core.MSVersion;
8+
import com.laytonsmith.core.MethodScriptFileLocations;
89
import com.laytonsmith.core.constructs.Target;
910
import com.laytonsmith.persistence.io.ConnectionMixin;
1011
import com.laytonsmith.persistence.io.ConnectionMixinFactory;
12+
import org.sqlite.SQLiteJDBCLoader;
13+
import org.sqlite.util.OSInfo;
14+
15+
import java.io.File;
1116
import java.io.IOException;
1217
import java.net.URI;
1318
import java.sql.DriverManager;
@@ -61,6 +66,22 @@ public SQLiteDataSource(URI uri, ConnectionMixinFactory.ConnectionMixinOptions o
6166
mixin = getConnectionMixin();
6267
try {
6368
Class.forName(org.sqlite.JDBC.class.getName());
69+
// Set native library override path if not already set. (e.g. -Dorg.sqlite.lib.path="/path/to/lib")
70+
if(System.getProperty("org.sqlite.lib.path") == null) {
71+
System.setProperty("org.sqlite.lib.path", new File(MethodScriptFileLocations.getDefault().getConfigDirectory(),
72+
"sqlite/native/" + OSInfo.getNativeLibFolderPathForCurrentOS()).getAbsolutePath());
73+
}
74+
try {
75+
// Load native library before connection to detect when the library is missing.
76+
// This is done in SQLiteProfile as well.
77+
SQLiteJDBCLoader.initialize();
78+
} catch (Exception ex) {
79+
throw new DataSourceException("Failed to load a native sqlite library for your platform."
80+
+ " You can download the library file from"
81+
+ " https://github.com/xerial/sqlite-jdbc/tree/master/src/main/resources/org/sqlite/native/"
82+
+ OSInfo.getNativeLibFolderPathForCurrentOS() + " and place it into "
83+
+ System.getProperty("org.sqlite.lib.path"));
84+
}
6485
path = mixin.getPath();
6586
connect();
6687
long startTime = System.currentTimeMillis();
@@ -88,7 +109,7 @@ public SQLiteDataSource(URI uri, ConnectionMixinFactory.ConnectionMixinOptions o
88109
}
89110
}
90111
} catch (ClassNotFoundException | UnsupportedOperationException | IOException | SQLException ex) {
91-
throw new DataSourceException("An error occured while setting up a connection to the SQLite database", ex);
112+
throw new DataSourceException("An error occurred while setting up a connection to the SQLite database", ex);
92113
} finally {
93114
if(DO_DISCONNECTS) {
94115
disconnect();

0 commit comments

Comments
 (0)