Maven Shade with ojdbc Dependency

扶醉桌前 提交于 2021-02-11 16:50:37

问题


I'm trying to develop a Java application that connects to an Oracle database and executes a function. If I run the application in Eclipse it works, but when I try to run the .jar built with Maven Shade an error is thrown "Error encountered: java.sql.SQLException: No suitable driver found".

Steps

  1. Execute "mvn clean install -U"

  2. Execute .jar with "java -jar example-1.0-SNAPSHOT.jar"

I can see that the Maven throws some WARNINGS about overlapping resources, could this be the reason?

[WARNING] example-1.0-SNAPSHOT.jar, ojdbc10-19.3.0.0.jar, ons-19.3.0.0.jar, oraclepki-19.3.0.0.jar, 
osdt_cert-19.3.0.0.jar, osdt_core-19.3.0.0.jar, simplefan-19.3.0.0.jar, ucp-19.3.0.0.jar define 1 
overlapping resources:
[WARNING]   - META-INF/MANIFEST.MF
[WARNING] maven-shade-plugin has detected that some class files are
[WARNING] present in two or more JARs. When this happens, only one
[WARNING] single version of the class is copied to the uber jar.
[WARNING] Usually this is not harmful and you can skip these warnings,
[WARNING] otherwise try to manually exclude artifacts based on
[WARNING] mvn dependency:tree -Ddetail=true and the above output.

Application

package function.example;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class App {
    public static void main(String[] args) {
        String result = null;
        try {
            result = checkForPalindrome("racecar");
        } catch (SQLException e) {
            System.out.println("Error encountered: " + e);
            e.printStackTrace();
        }
        System.out.println(result);
    }

    public static Connection getConnection() {

        Properties prop = ReadPropertyFile();
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(prop.getProperty("db.URL"), prop.getProperty("db.user"),
                    prop.getProperty("db.password"));
        } catch (SQLException e) {
            System.out.println("Error encountered: " + e);
            e.printStackTrace();
        }
        return conn;
    }

    public static String checkForPalindrome(String word) throws SQLException {
        String sql = "{? = call CHECKFORPALINDROME(?)}";
        try (Connection conn = getConnection(); java.sql.CallableStatement stmt = conn.prepareCall(sql);) {
            stmt.setString(2, word);
            stmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            stmt.execute();
            String stmtResult = stmt.getString(1);
            return stmtResult;
        }
    }

    private static Properties ReadPropertyFile() {
        Properties prop = new Properties();
        try (InputStream input = new FileInputStream("c:\\config.properties")) {
            prop.load(input);
        } catch (FileNotFoundException e) {
            System.out.println("Error encountered: " + e);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Error encountered: " + e);
            e.printStackTrace();
        }
        return prop;
    }
}

POM

<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>function</groupId>
    <artifactId>example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>ODB-function-example-pom</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc10 -->
        <dependency>
            <groupId>com.oracle.ojdbc</groupId>
            <artifactId>ojdbc10</artifactId>
            <version>19.3.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <manifestEntries>
                                <Main-Class>function.example.App</Main-Class>
                            </manifestEntries>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

来源:https://stackoverflow.com/questions/60864273/maven-shade-with-ojdbc-dependency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!