添加外部库 .jar 到 Spring boot .jar internal /lib
Add external library .jar to Spring boot .jar internal /lib
我有一个无法使用 pom.xml 从 public 存储库导入的外部 .jar,它是 sqljdbc41.jar
。
我可以从我的 IDE 在本地 运行 项目,一切都会正常进行。我在下载后引用了这个库:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc41.jar</systemPath>
</dependency>
当我 运行 mvn clean package
创建我的 .jar 文件并尝试 运行 创建的 .jar 时,会弹出一个错误,其中提到 SQL服务器引用无效。然后我提取了我的 .jar 文件,确实如此,pom.xml
文件中引用的所有内容都被正确下载和添加,但是,我的 SQL 服务器没有。
我可以,以一种非常 hacky 的方式 * 只需在 sqljdbc41.jar
被编译为 .jar 后手动将其添加到我的 /lib 文件夹中,它就可以工作,但这似乎非常不理想。什么是更好的方法?
*用 Winrar 打开 .jar 文件,转到 /lib 文件夹,手动 selecting 我的 sqljdbc41.jar
文件,然后确保 select 底部的无压缩选项在 Winrar 为您提供压缩选项的左侧,以防您通过 Google 找到它但没有人回答。
您可以在本地存储库中安装 sqljdbc41.jar:
mvn install:install-file -Dfile=path/to/sqljdbc41.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc41 -Dversion=4.1 -Dpackaging=jar
然后将依赖声明为标准依赖:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
</dependency>
如果您使用远程工件存储库(nexus、archiva...),您还需要在此存储库上部署工件。您可以在这里找到更多信息:https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
另一种方法,你可以把它放在资源文件夹中,比如resources/lib/xxx.jar,然后像这样配置pom.xml:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/sqljdbc41.jar</systemPath>
在我的例子中,错误是在标签中提供了没有 "dot" 的版本号:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
这个有效:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1.8</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
您可以将 'includeSystemScope' 设置为 true。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
在 Spring 引导中:我也遇到了类似的问题,下面的代码帮助了我。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
对我有用:
project {root folder}/libs/ojdbc-11.2.0.3.jar
pom.xml:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>11.2.0.3</version>
<scope>system</scope>
<systemPath>${basedir}/libs/ojdbc-11.2.0.3.jar</systemPath>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
当 Spring-Boot 项目与 maven 或 gradle 插件一起使用时,它们默认将应用程序打包为可执行 jar。
这些可执行 jar 不能用作任何其他 Spring-Boot 项目的依赖项,因为可执行 jar 在 [=27= 中添加 classes ]BOOT-INF/classes文件夹。这意味着当可执行 jar 用作依赖项时无法找到它们,因为依赖项 jar 也将具有相同的 class 路径结构,如下所示。
如果我们想使用项目-A 作为项目-B 中的 Maven 依赖项,那么我们必须有两个工件。要生成两个工件,一个可用作依赖项,一个可执行,必须指定 classifier。此 class 符应用于可执行存档的名称,将默认存档用作依赖项。
要在 Maven 中配置一个 classifier of exec,您可以使用以下配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
所以这里的 MAJIC WORD 是 <classifier>exec</classifier>
这将创建一个如下所示的 jar 结构,然后它可以很容易地被 spring-boot 项目使用作为 class 路径上的 Maven 依赖 jar。
上面的插件需要添加到project-A pom中,在project-B中作为依赖使用。 spring documentation 第 16.5 节中也有同样的解释。还有。
为了通过本地存储库工作,我们将使用的目标 .jar 文件必须位于 s2 文件夹中。为此可以使用几种方法:
- 文件可以手动取下来放在相关的地方(不是
首选)。可以通过安装它来完成相同的过程
控制台。
- Relevant Remote URL写在.pom文件依赖和
刷新Intellij时自动放在s2文件夹中
(验证)在 IDE 中使用。
- 可以通过中央存储库解决 .pom 文件依赖关系来完成相同的过程。
注意:SpringBot上的相关jar工作不要忘记ComponentScan
我有一个无法使用 pom.xml 从 public 存储库导入的外部 .jar,它是 sqljdbc41.jar
。
我可以从我的 IDE 在本地 运行 项目,一切都会正常进行。我在下载后引用了这个库:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc41.jar</systemPath>
</dependency>
当我 运行 mvn clean package
创建我的 .jar 文件并尝试 运行 创建的 .jar 时,会弹出一个错误,其中提到 SQL服务器引用无效。然后我提取了我的 .jar 文件,确实如此,pom.xml
文件中引用的所有内容都被正确下载和添加,但是,我的 SQL 服务器没有。
我可以,以一种非常 hacky 的方式 * 只需在 sqljdbc41.jar
被编译为 .jar 后手动将其添加到我的 /lib 文件夹中,它就可以工作,但这似乎非常不理想。什么是更好的方法?
*用 Winrar 打开 .jar 文件,转到 /lib 文件夹,手动 selecting 我的 sqljdbc41.jar
文件,然后确保 select 底部的无压缩选项在 Winrar 为您提供压缩选项的左侧,以防您通过 Google 找到它但没有人回答。
您可以在本地存储库中安装 sqljdbc41.jar:
mvn install:install-file -Dfile=path/to/sqljdbc41.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc41 -Dversion=4.1 -Dpackaging=jar
然后将依赖声明为标准依赖:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
</dependency>
如果您使用远程工件存储库(nexus、archiva...),您还需要在此存储库上部署工件。您可以在这里找到更多信息:https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
另一种方法,你可以把它放在资源文件夹中,比如resources/lib/xxx.jar,然后像这样配置pom.xml:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/sqljdbc41.jar</systemPath>
在我的例子中,错误是在标签中提供了没有 "dot" 的版本号:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
这个有效:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1.8</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
您可以将 'includeSystemScope' 设置为 true。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
在 Spring 引导中:我也遇到了类似的问题,下面的代码帮助了我。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
对我有用:
project {root folder}/libs/ojdbc-11.2.0.3.jar
pom.xml:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>11.2.0.3</version>
<scope>system</scope>
<systemPath>${basedir}/libs/ojdbc-11.2.0.3.jar</systemPath>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
当 Spring-Boot 项目与 maven 或 gradle 插件一起使用时,它们默认将应用程序打包为可执行 jar。 这些可执行 jar 不能用作任何其他 Spring-Boot 项目的依赖项,因为可执行 jar 在 [=27= 中添加 classes ]BOOT-INF/classes文件夹。这意味着当可执行 jar 用作依赖项时无法找到它们,因为依赖项 jar 也将具有相同的 class 路径结构,如下所示。
如果我们想使用项目-A 作为项目-B 中的 Maven 依赖项,那么我们必须有两个工件。要生成两个工件,一个可用作依赖项,一个可执行,必须指定 classifier。此 class 符应用于可执行存档的名称,将默认存档用作依赖项。 要在 Maven 中配置一个 classifier of exec,您可以使用以下配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
所以这里的 MAJIC WORD 是 <classifier>exec</classifier>
这将创建一个如下所示的 jar 结构,然后它可以很容易地被 spring-boot 项目使用作为 class 路径上的 Maven 依赖 jar。
上面的插件需要添加到project-A pom中,在project-B中作为依赖使用。 spring documentation 第 16.5 节中也有同样的解释。还有。
为了通过本地存储库工作,我们将使用的目标 .jar 文件必须位于 s2 文件夹中。为此可以使用几种方法:
- 文件可以手动取下来放在相关的地方(不是 首选)。可以通过安装它来完成相同的过程 控制台。
- Relevant Remote URL写在.pom文件依赖和 刷新Intellij时自动放在s2文件夹中 (验证)在 IDE 中使用。
- 可以通过中央存储库解决 .pom 文件依赖关系来完成相同的过程。
注意:SpringBot上的相关jar工作不要忘记ComponentScan