【问题标题】:Include JDBC driver for Postgres in the JAR fo my simple demo app driven by Maven [duplicate]在我由 Maven 驱动的简单演示应用程序的 JAR 中包含 Postgres 的 JDBC 驱动程序 [重复]
【发布时间】: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


    【解决方案1】:

    .war 文件(例如您在构建 Vaadin Web 应用程序时看到的那些文件)默认包含依赖项。

    相比之下,Maven 构建的.jar 文件默认情况下包含任何依赖项。

    您可以使用诸如maven-shade-plugin 之类的插件来创建一个包含依赖项的阴影 jar:

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
            <configuration>
              <!-- put your configurations here -->
            </configuration>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
    </project>
    

    更多示例可在Apache Maven Shade Plugin 项目页面上找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2011-04-15
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2012-11-23
      相关资源
      最近更新 更多