From 4d6dc90e684e554cb985f0326e154e44644b453b Mon Sep 17 00:00:00 2001 From: 杜云山 <870666625@qq.com> Date: Fri, 19 Mar 2021 11:18:14 +0800 Subject: [PATCH] feat: springboot加载外部jar示例 --- .gitignore | 25 +++++++++++++++++++++++++ .mvn/wrapper/maven-wrapper.jar | Bin 0 -> 47610 bytes .mvn/wrapper/maven-wrapper.properties | 1 + LICENSE | 24 ++++++++++++++++++++++++ externallibs/devices-aeroflex-0.0.1-SNAPSHOT.jar | Bin 0 -> 5644 bytes externallibs/h2-1.4.197.jar | Bin 0 -> 1927540 bytes externallibs/lamta-share-device-client-0.0.1-SNAPSHOT.jar | Bin 0 -> 11149 bytes externallibs/lamta-share-device-core-0.0.1-SNAPSHOT.jar | Bin 0 -> 21274 bytes externallibs/mina-core-2.1.4.jar | Bin 0 -> 667640 bytes mvnw | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mvnw.cmd | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ run.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ springboot加载外部jar.md | 29 +++++++++++++++++++++++++++++ src/main/java/com/example/x/Application.java | 35 +++++++++++++++++++++++++++++++++++ src/main/resources/application.properties | 0 17 files changed, 650 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 .mvn/wrapper/maven-wrapper.jar create mode 100644 .mvn/wrapper/maven-wrapper.properties create mode 100644 LICENSE create mode 100644 externallibs/devices-aeroflex-0.0.1-SNAPSHOT.jar create mode 100644 externallibs/h2-1.4.197.jar create mode 100644 externallibs/lamta-share-device-client-0.0.1-SNAPSHOT.jar create mode 100644 externallibs/lamta-share-device-core-0.0.1-SNAPSHOT.jar create mode 100644 externallibs/mina-core-2.1.4.jar create mode 100644 mvnw create mode 100644 mvnw.cmd create mode 100644 pom.xml create mode 100644 readme.md create mode 100644 run.sh create mode 100644 springboot加载外部jar.md create mode 100644 src/main/java/com/example/x/Application.java create mode 100644 src/main/resources/application.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82eca33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/.mvn/wrapper/maven-wrapper.jar b/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 0000000..9cc84ea Binary files /dev/null and b/.mvn/wrapper/maven-wrapper.jar differ diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..6c8c0e0 --- /dev/null +++ b/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1 @@ +distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cf1ab25 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/externallibs/devices-aeroflex-0.0.1-SNAPSHOT.jar b/externallibs/devices-aeroflex-0.0.1-SNAPSHOT.jar new file mode 100644 index 0000000..9e6a97d Binary files /dev/null and b/externallibs/devices-aeroflex-0.0.1-SNAPSHOT.jar differ diff --git a/externallibs/h2-1.4.197.jar b/externallibs/h2-1.4.197.jar new file mode 100644 index 0000000..5f311db Binary files /dev/null and b/externallibs/h2-1.4.197.jar differ diff --git a/externallibs/lamta-share-device-client-0.0.1-SNAPSHOT.jar b/externallibs/lamta-share-device-client-0.0.1-SNAPSHOT.jar new file mode 100644 index 0000000..382d623 Binary files /dev/null and b/externallibs/lamta-share-device-client-0.0.1-SNAPSHOT.jar differ diff --git a/externallibs/lamta-share-device-core-0.0.1-SNAPSHOT.jar b/externallibs/lamta-share-device-core-0.0.1-SNAPSHOT.jar new file mode 100644 index 0000000..ff480ed Binary files /dev/null and b/externallibs/lamta-share-device-core-0.0.1-SNAPSHOT.jar differ diff --git a/externallibs/mina-core-2.1.4.jar b/externallibs/mina-core-2.1.4.jar new file mode 100644 index 0000000..487ba4a Binary files /dev/null and b/externallibs/mina-core-2.1.4.jar differ diff --git a/mvnw b/mvnw new file mode 100644 index 0000000..5bf251c --- /dev/null +++ b/mvnw @@ -0,0 +1,225 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven2 Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Migwn, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + # TODO classpath? +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +echo $MAVEN_PROJECTBASEDIR +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/mvnw.cmd b/mvnw.cmd new file mode 100644 index 0000000..019bd74 --- /dev/null +++ b/mvnw.cmd @@ -0,0 +1,143 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven2 Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" + +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..115cb1a --- /dev/null +++ b/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.example + x + 0.0.1-SNAPSHOT + jar + + springboot.propertieslauncher + Demo project for Spring Boot and its usage of PropertiesLauncher + + + org.springframework.boot + spring-boot-starter-parent + 2.1.4.RELEASE + + + + + UTF-8 + UTF-8 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + + + + com.h2database + h2 + 1.4.197 + provided + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + ZIP + + h2 + + + + + repackage + + + + + + + + + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..c81f799 --- /dev/null +++ b/readme.md @@ -0,0 +1,60 @@ +Example for a self-executable Spring Boot JAR, which accesses a DB driver (or any other 3rd-party JAR) from a external (non-bundled) directory. + +This may be useful if you are not allowed to bundle the DB driver with your application. + +Tips: +* Use `ZIP` as layout in `spring-boot-maven-plugin` (to generate the `PropertiesLauncher` entry in the `MANIFEST.MF`) +* Set `true` to prepend a shell script to the resulting JAR +* See [command line examples](run.sh) for different types of running the JAR using the `loader.path` option + + +## Expected result + +Have a look for the output + +``` +Try to use external driver +-------------> Found driver h2 1.4 +``` +in all variants. The same application is started using different types of loading an external jar. + +``` +-------------------------- +Variant A via java -jar +-------------------------- +... +2019-04-25 22:51:59.934 INFO 26947 --- [ main] com.example.x.Application : Started Application in 6.005 seconds (JVM running for 6.465) +Try to use external driver +-------------> Found driver h2 1.4 + +-------------------------- +Variant B via java -jar +-------------------------- +... +2019-04-25 22:52:06.447 INFO 26948 --- [ main] com.example.x.Application : Started Application in 5.996 seconds (JVM running for 6.456) +Try to use external driver +-------------> Found driver h2 1.4 + +------------------------------------------------------------------------------------------------ +Variant C via self-executable and loader.path-configuration from (default-) loader.properties +------------------------------------------------------------------------------------------------ +... +Try to use external driver +-------------> Found driver h2 1.4 + +-------------------------------------------------------------------------------------------- +Variant D via self-executable and loader.path-configuration from different property file +-------------------------------------------------------------------------------------------- +... +2019-04-25 22:52:19.494 INFO 26979 --- [ main] com.example.x.Application : Started Application in 6.005 seconds (JVM running for 6.453) +Try to use external driver +-------------> Found driver h2 1.4 + +------------------------------------------------------------ +Variant E via self-executable and LOADER_PATH environment +------------------------------------------------------------ +... +2019-04-25 22:52:26.024 INFO 26995 --- [ main] com.example.x.Application : Started Application in 5.967 seconds (JVM running for 6.439) +Try to use external driver +-------------> Found driver h2 1.4 +``` diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..e24e0ae --- /dev/null +++ b/run.sh @@ -0,0 +1,42 @@ +mvn clean package -DskipTests + +# https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/executable-jar.html#executable-jar-launching +# The PropertiesLauncher looks in BOOT-INF/lib/ in your application archive by default, but you can add additional locations by setting an environment variable called LOADER_PATH or loader.path in loader.properties (which is a comma-separated list of directories, archives, or directories within archives). + +# https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-property-launcher-features + +current=`pwd` + +echo -------------------------- +echo Variant A via "java -jar" +echo -------------------------- +java -Dloader.path=$current/externallibs/ -jar ./target/x-0.0.1-SNAPSHOT.jar + +echo -------------------------- +echo Variant B via "java -jar" +echo -------------------------- +java -Dloader.path=$current/externallibs/h2-1.4.197.jar -jar ./target/x-0.0.1-SNAPSHOT.jar + +echo ------------------------------------------------------------------------------------------------ +echo Variant C via "self-executable and loader.path-configuration from (default-) loader.properties" +echo ------------------------------------------------------------------------------------------------ +echo loader.path=$current/externallibs/h2-1.4.197.jar > ./target/loader.properties +./target/x-0.0.1-SNAPSHOT.jar +rm ./target/loader.properties + +echo -------------------------------------------------------------------------------------------- +echo Variant D via "self-executable and loader.path-configuration from different property file " +echo -------------------------------------------------------------------------------------------- +export LOADER_CONFIG_LOCATION=otherloader.properties +echo loader.path=$current/externallibs/h2-1.4.197.jar > ./target/otherloader.properties +./target/x-0.0.1-SNAPSHOT.jar +unset LOADER_CONFIG_LOCATION +rm ./target/otherloader.properties + +echo ------------------------------------------------------------ +echo Variant E via "self-executable and LOADER_PATH environment" +echo ------------------------------------------------------------ +export LOADER_PATH=$current/externallibs/h2-1.4.197.jar +./target/x-0.0.1-SNAPSHOT.jar + + diff --git a/springboot加载外部jar.md b/springboot加载外部jar.md new file mode 100644 index 0000000..cb7c012 --- /dev/null +++ b/springboot加载外部jar.md @@ -0,0 +1,29 @@ +# 加载外部jar + + +> 作者: 杜云山 + +启动方式1 启动参数 -Dloader.path= + +```tex +启动: java -Dloader.path=d:\GitHub\spring-boot-propertieslauncher-with-external-jar-master\externallibs\ -jar x-0.0.1-SNAPSHOT.jar + +注意: -Dloader.path=的值如果有不对, 启动时直接值忽略了, 没有任何提示. +``` + + + +启动方式2 jar包同级 文件: + +``` +loader.properties + +内容: loader.path=/GitHub/spring-boot-propertieslauncher-with-external-jar-master/externallibs/ + + +也可以手动指定多个jar: 中间用,分隔 +loader.path=/GitHub/spring-boot-propertieslauncher-with-external-jar-master/externallibs/lamta-share-device-client-0.0.1-SNAPSHOT.jar,/GitHub/spring-boot-propertieslauncher-with-external-jar-master/externallibs/devices-aeroflex-0.0.1-SNAPSHOT.jar,/GitHub/spring-boot-propertieslauncher-with-external-jar-master/externallibs/lamta-share-device-core-0.0.1-SNAPSHOT.jar,/GitHub/spring-boot-propertieslauncher-with-external-jar-master/externallibs/mina-core-2.1.4.jar + +启动: java -jar x-0.0.1-SNAPSHOT.jar + +``` diff --git a/src/main/java/com/example/x/Application.java b/src/main/java/com/example/x/Application.java new file mode 100644 index 0000000..64bb0bc --- /dev/null +++ b/src/main/java/com/example/x/Application.java @@ -0,0 +1,35 @@ +package com.example.x; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +import java.lang.reflect.Constructor; + +@SpringBootApplication +public class Application +{ + + public static void main(String[] args) + { + SpringApplication.run(Application.class, args); + } + + @Bean + public CommandLineRunner commandLineRunner(ApplicationContext ctx) + { + return args -> + { + System.out.println("Try to use external driver"); + + Class aClass = Class.forName("org.h2.Driver"); + + Constructor c = aClass.getConstructor(); + java.sql.Driver driver = (java.sql.Driver) c.newInstance(); + + System.out.println("-------------> Found driver h2 " + driver.getMajorVersion() + "." +driver.getMinorVersion()); + }; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/main/resources/application.properties -- libgit2 0.21.2