问题
I have created executable Jar using Maven dependencies for connecting multiple database at runtime by passing the parameters.
I added dependencies as below:
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.7.jre8-preview</version>
</dependency>
In my case, when I ran executable jar it connected successfully to Oracle database but gives No suitable driver not found for SQL server database.
When I changed the order of dependency shown above to SQL server first and Oracle second and built new executable jar and ran it, I could able to connect to SQL server database successfully but this time it gives error No suitable driver not found for Oracle database.
When I compared both the extracted executable jars, it has exactly same structure, numbers of files and size of files and didn't find any single difference. Hence not able to found what could be the problem while building executable jar so that a single jar would work for both databases.
Any suggestion would be really help me to resolve this strange behavior...
Thanks in advance.
The .pom file is as below.
<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>com.mycompany.dataload</groupId>
<artifactId>DataLoadUtilities</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DataLoadUtilities</name>
<description>A utility project that started with fetching table metadata from SQL Server and Oracle</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.7.jre8-preview</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.mycompany.dataload.main.TableMetadataGeneratorForHive</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeScope>system</excludeScope>
<excludes>META-INF/*.SF</excludes>
<excludes>META-INF/*.DSA</excludes>
<excludes>META-INF/*.RSA</excludes>
<excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions></plugin>
</plugins>
</build></project>
回答1:
You are encountering this problem because of the way you have created your executable jar.
Each of ojdbc7-12.1.0.2.jar
and mssql-jdbc-6.1.7.jre8-preview.jar
contains a META-INF/services/java.sql.Driver
file containing the name of the respective java.sql.Driver
implementation classes for each vendor. It seems like you only get the first one that is encountered in the dependency list.
This is described in the javadoc for java.sql.DriverManager.
The Java 8 JAR File Specification - Service Provider provides more information that states that the META-INF/services/java.sql.Driver
file:
... should contain a newline-separated list of unique concrete provider-class names
Therefore you can probably configure an AppendingTransformer in your configuration to merge the content of these files during shading.
来源:https://stackoverflow.com/questions/44115344/executable-jar-built-using-maven-is-not-resolving-dependency-for-multiple-databa