Skip to content

Commit

Permalink
Exclude some sqlite native libraries from the jar
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PseudoKnight committed Nov 7, 2023
1 parent e84cba4 commit 97c0882
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,22 @@
<filter>
<artifact>org.xerial:sqlite-jdbc:jar:*</artifact>
<includes>
<include>native/**</include>
<include>org/ibex/**</include>
<include>org/sqlite/**</include>
</includes>
<!-- Native library exclusions for os/arch that are not officially supported -->
<!-- Can still be added by users using the sqlite/native directory -->
<excludes>
<exclude>org/sqlite/native/FreeBSD/**</exclude>
<exclude>org/sqlite/native/Linux-Android/**</exclude>
<exclude>org/sqlite/native/Linux/ppc64/*</exclude>
<exclude>org/sqlite/native/Linux/arm/*</exclude>
<exclude>org/sqlite/native/Linux/armv6/*</exclude>
<exclude>org/sqlite/native/Linux/armv7/*</exclude>
<exclude>org/sqlite/native/Linux/x86/*</exclude>
<exclude>org/sqlite/native/Linux-Musl/x86/*</exclude>
<exclude>org/sqlite/native/Windows/x86/*</exclude>
<exclude>org/sqlite/native/Windows/armv7/*</exclude>
</excludes>
</filter>
<filter>
<artifact>org.apache.commons:commons-io:jar:*</artifact>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/laytonsmith/database/SQLiteProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.laytonsmith.core.Profiles;
import com.laytonsmith.core.MethodScriptFileLocations;
import org.sqlite.SQLiteJDBCLoader;
import org.sqlite.util.OSInfo;

import java.io.File;
import java.sql.SQLException;
import java.util.Map;
Expand Down Expand Up @@ -37,6 +40,22 @@ public String getConnectionString() throws SQLException {
} catch (ClassNotFoundException ex) {
throw new SQLException("Cannot load SQLite. Check your installation and try again");
}
// Set native library override path if not already set. (e.g. -Dorg.sqlite.lib.path="/path/to/lib")
if(System.getProperty("org.sqlite.lib.path") == null) {
System.setProperty("org.sqlite.lib.path", new File(MethodScriptFileLocations.getDefault().getConfigDirectory(),
"sqlite/native/" + OSInfo.getNativeLibFolderPathForCurrentOS()).getAbsolutePath());
}
try {
// Load native library before connection to detect when the library is missing.
// This is done in SQLiteDataSource as well.
SQLiteJDBCLoader.initialize();
} catch (Exception ex) {
throw new SQLException("Failed to load a native sqlite library for your platform."
+ " You can download the library file from"
+ " https://github.com/xerial/sqlite-jdbc/tree/master/src/main/resources/org/sqlite/native/"
+ OSInfo.getNativeLibFolderPathForCurrentOS() + " and place it into "
+ System.getProperty("org.sqlite.lib.path"));
}
return "jdbc:sqlite:" + getFile();
}

Expand Down
23 changes: 22 additions & 1 deletion src/main/java/com/laytonsmith/persistence/SQLiteDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import com.laytonsmith.annotations.datasource;
import com.laytonsmith.core.MSLog;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.MethodScriptFileLocations;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.persistence.io.ConnectionMixin;
import com.laytonsmith.persistence.io.ConnectionMixinFactory;
import org.sqlite.SQLiteJDBCLoader;
import org.sqlite.util.OSInfo;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.sql.DriverManager;
Expand Down Expand Up @@ -61,6 +66,22 @@ public SQLiteDataSource(URI uri, ConnectionMixinFactory.ConnectionMixinOptions o
mixin = getConnectionMixin();
try {
Class.forName(org.sqlite.JDBC.class.getName());
// Set native library override path if not already set. (e.g. -Dorg.sqlite.lib.path="/path/to/lib")
if(System.getProperty("org.sqlite.lib.path") == null) {
System.setProperty("org.sqlite.lib.path", new File(MethodScriptFileLocations.getDefault().getConfigDirectory(),
"sqlite/native/" + OSInfo.getNativeLibFolderPathForCurrentOS()).getAbsolutePath());
}
try {
// Load native library before connection to detect when the library is missing.
// This is done in SQLiteProfile as well.
SQLiteJDBCLoader.initialize();
} catch (Exception ex) {
throw new DataSourceException("Failed to load a native sqlite library for your platform."
+ " You can download the library file from"
+ " https://github.com/xerial/sqlite-jdbc/tree/master/src/main/resources/org/sqlite/native/"
+ OSInfo.getNativeLibFolderPathForCurrentOS() + " and place it into "
+ System.getProperty("org.sqlite.lib.path"));
}
path = mixin.getPath();
connect();
long startTime = System.currentTimeMillis();
Expand Down Expand Up @@ -88,7 +109,7 @@ public SQLiteDataSource(URI uri, ConnectionMixinFactory.ConnectionMixinOptions o
}
}
} catch (ClassNotFoundException | UnsupportedOperationException | IOException | SQLException ex) {
throw new DataSourceException("An error occured while setting up a connection to the SQLite database", ex);
throw new DataSourceException("An error occurred while setting up a connection to the SQLite database", ex);
} finally {
if(DO_DISCONNECTS) {
disconnect();
Expand Down

0 comments on commit 97c0882

Please sign in to comment.