在由Maven驱动的简单演示应用程序的JAR中包含Postgres的JDBC驱动程序 [英] Include JDBC driver for Postgres in the JAR fo my simple demo app driven by Maven

查看:86
本文介绍了在由Maven驱动的简单演示应用程序的JAR中包含Postgres的JDBC驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使Maven在我的应用程序的.jar文件中包含用于Postgres的JDBC驱动程序?

How to make Maven include the JDBC driver for Postgres inside my app's .jar file?

我将此依赖项元素添加到了POM中的<dependencies>元素中.

I added this dependency element to the <dependencies> element in my POM.

<!-- 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.

The IntelliJ IDE shows the driver was successfully downloaded, as it is listed in the "External Libraries" item of my Project pane. And my code can use the JDBC classes such as PGSimpleDataSource.

构建时,如果查看生成的.jar文件,则不包含JDBC驱动程序.

When I build, if I look inside the resulting .jar file, there is no JDBC driver included.

我的项目由Maven驱动,使用 maven-archetype-quickstart 原型.我确实将POM中的所有版本号更新为最新版本.我唯一的其他更改是添加以下内容以获得JAR的清单文件以指定main类.

My project is driven by Maven, using the maven-archetype-quickstart archetype. I did update all the version numbers within the POM to the latest. My only other change was to add the following to get the manifest file of the JAR to specify a main class.

                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>work.basil.example.App</mainClass>
                            </manifest>
                        </archive>
                    </configuration>

我认为默认情况下,Maven会将所有依赖项捆绑在生成的JAR文件中.这就是我在构建Vaadin Web应用程序时看到的行为.是不是更普遍呢?还是JDBC驱动程序很特殊,由于某种原因而被省略了.

I thought that Maven by default would bundle all dependencies inside the resulting JAR file. That is the behavior I have seen in building Vaadin web apps. Is that not the case more generally? Or is the JDBC driver special and being omitted for some reason.

如果有帮助,这里是整个POM.

If it helps, here is the entire 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>

推荐答案

.war 文件,例如您在构建Vaadin Web应用程序中看到的文件,默认情况下确实包含依赖项.

The .war files, such as those you saw in building Vaadin web apps, do include dependencies by default.

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

In contrast, the .jar files built by Maven do not include any dependencies by default.

您可以使用 maven-shade-plugin 创建一个阴影罐子,其中包括依赖项:

You can use a plugin such as maven-shade-plugin to create a shaded jar, which does include the dependencies:

<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中找到插件 项目页面.

Further examples can be found on the Apache Maven Shade Plugin project page.

这篇关于在由Maven驱动的简单演示应用程序的JAR中包含Postgres的JDBC驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆