【发布时间】:2020-03-04 19:16:24
【问题描述】:
如何让 Maven 在我的应用的 .jar 文件中包含 JDBC driver for Postgres?
我将此依赖项元素添加到我的 POM 中的 <dependencies> 元素中。
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
</dependency>
IntelliJ IDE 显示驱动程序已成功下载,因为它在我的Project 窗格的“外部库”项中列出。我的代码可以使用 JDBC 类,例如PGSimpleDataSource。
当我构建时,如果我查看生成的 .jar 文件,没有包含 JDBC 驱动程序。
我的项目由 Maven 驱动,使用 maven-archetype-quickstart 原型。我确实将 POM 中的所有版本号都更新到了最新版本。我唯一的其他更改是添加以下内容以获取 JAR 的清单文件以指定 main 类。
<configuration>
<archive>
<manifest>
<mainClass>work.basil.example.App</mainClass>
</manifest>
</archive>
</configuration>
我认为默认情况下 Maven 会将所有依赖项捆绑到生成的 JAR 文件中。这就是我在构建 Vaadin Web 应用程序时看到的行为。更普遍的情况不是这样吗?或者 JDBC 驱动程序是特殊的并且由于某种原因被省略。
如果有帮助,这里是整个 POM。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>tryjdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<name>tryjdbc</name>
<description>A simple tryjdbc.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.0-M1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<mainClass>work.basil.example.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>
【问题讨论】:
标签: java maven jdbc jar dependencies