在由Maven驱动的简单演示应用程序的JAR中包含Postgres的JDBC驱动程序

如何使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>
min9884 回答:在由Maven驱动的简单演示应用程序的JAR中包含Postgres的JDBC驱动程序

.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项目页面上找到。

本文链接:https://www.f2er.com/3139772.html

大家都在问