简体   繁体   中英

How to include OJDBC driver in runnable jar?

I have a Java project, which connects to Oracle 12c database. Previously the ojdbc jar (and all other dependencies) had to be on the classpath for the jar to be able to run. However this is intended to be a standalone app, so I wanted to setup a build process which in the end spits out a single jar with all dependencies included. I made the following steps:

  • Converted the project to a Maven project
  • Setup dependencies like Log4j as Maven dependencies
  • Following this guide, I managed to include ojdbc as dependency
  • Setup maven-assembly-plugin to generate a runnable jar like this:
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>fully.qualified.path.to.Main</mainClass>
            </manifest>
            <manifestEntries>
                <Built-On>${maven.build.timestamp} UTC</Built-On>
                <ModuleName>${project.name}</ModuleName>
                <ModuleVersion>${project.version}</ModuleVersion>
            </manifestEntries>
            <manifestSections>
                <manifestSection>
                    <name>Release section</name>
                    <manifestEntries>
                        <BaseVersion>${baseversion}</BaseVersion>
                        <BuildNumber>${buildnumber}</BuildNumber>
                        <GITRevision>${gitrevision}</GITRevision>
                    </manifestEntries>
                </manifestSection>
            </manifestSections>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The resulting jar includes most of the dependencies in form of class files, eg there is an 'org/apache/log4j' folder in the root of the jar with Log4j class files. The problem is that the ojdbc (com/oracle/jdbc) is not there, and I'm getting ClassNotFoundExceptions runtime. I checked and the ojdbc jar is present under my.m2 folder in the correct path.

Is there any way to include the ojdbc dependencies, either as a jar or as class files, in my runnable jar?

EDIT: Based on Essex Boy's comment, I used the shade plugin like so:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <minimizeJar>true</minimizeJar>
        <transformers>
            <transformer
                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <manifestEntries>
                    <Main-Class>fully.qualified.path.to.Main</Main-Class>
                    <Built-On>${maven.build.timestamp} UTC</Built-On>
                    <ModuleName>${project.name}</ModuleName>
                    <ModuleVersion>${project.version}</ModuleVersion>
                    <BaseVersion>${baseversion}</BaseVersion>
                    <BuildNumber>${buildnumber}</BuildNumber>
                    <GITRevision>${gitrevision}</GITRevision>
                </manifestEntries>
            </transformer>
        </transformers>
    </configuration>
</plugin>

With this I get a similar jar, with most dependencies, but ojdbc is still excluded. I tried to add an artifactSet tag, and include 'com.oracle.jdbc:ojdbc8' explicitly, but still it is not added. The only upside is that with the minimizeJar option I get a smaller jar with only the actual dependencies included.

Found the answer. You need to set this. Class.forName("oracle.jdbc.driver.OracleDriver")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM