我想添加oracle jdbc驱动程序到我的项目作为依赖(运行时范围)- ojdbc14。 在MVNrepository站点中,要放在POM中的依赖项是:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.3.0</version>
</dependency>

当然,这是行不通的,因为它不在maven使用的中央存储库中。 两个问题:

我如何找到包含此工件的存储库(如果有的话)? 如何添加它以便Maven使用它?


当前回答

无论出于何种原因,我都无法让上述任何解决方案发挥作用。(仍然不能)。

相反,我所做的是将jar包含在我的项目中(blech),然后为它创建一个“系统”依赖项,指示jar的路径。这可能不是正确的方法,但确实有效。并且它消除了团队中的其他开发人员(或设置构建服务器的人员)将jar放在本地存储库中的需要。

更新:当我运行Hibernate工具时,此解决方案适用于我。但是,它似乎不能用于构建WAR文件。它不包括目标WAR文件中的ojdbc6.jar文件。

1)在项目的根目录下创建一个名为“lib”的目录。

2)复制ojdbc6.jar文件(不管这个jar叫什么)。

3)创建一个依赖项,看起来像这样:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc</artifactId>
    <version>14</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/ojdbc6.jar</systemPath> <!-- must match file name -->
</dependency>

丑,但对我有用。

要在war文件中包含这些文件,请将以下内容添加到pom中

<build>
    <finalName>MyAppName</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${basedir}/src/main/java</directory>
                        <targetPath>WEB-INF/classes</targetPath>
                        <includes>
                            <include>**/*.properties</include>
                            <include>**/*.xml</include>
                            <include>**/*.css</include>
                            <include>**/*.html</include>
                        </includes>
                    </resource>
                    <resource>
                        <directory>${basedir}/lib</directory>
                        <targetPath>WEB-INF/lib</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

其他回答

1. 我如何找到包含此工件的存储库(如果有的话)?

正如DavidS所评论的那样,我在回答时引用的那行已经不再出现在我所链接的当前(在我现在写作的时候)OTN许可协议中。这个答案只适用于工件的旧版本,比如10.2.0.3.0等等。

所有Oracle数据库JDBC驱动程序都在OTN许可协议下分发。

如果您阅读OTN许可协议,您会发现以下许可条款:

你不可以: ... -除非你的应用程序附有该等程序,否则不得分发该等程序; ...

所以这就是为什么你不能在任何公共Maven资源库中找到驱动程序的jar,因为它将单独发布,如果发生这种情况,它将违反许可证。

添加依赖项:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.3.0</version>
</dependency>

(或任何更高版本)使Maven下载ojdbc14-10.2.0.3.0。仅限Pom,在Pom中你可以读到:

...
<licenses>
    <license>
        <name>Oracle Technology Network Development and Distribution License Terms</name>
        <url>http://www.oracle.com/technology/software/htdocs/distlic.html</url>
    </license>
</licenses>
...

通知您OTN许可证。

2. 如何添加它以便Maven使用它?

为了使上面的依赖能够工作,我同意victor hugo的建议,他建议你手动将jar安装到你的本地Maven存储库(.m2目录),通过运行:

mvn install:install-file -Dfile={Path_to_your_ojdbc.jar} -DgroupId=com.oracle 
-DartifactId=ojdbc -Dversion=10.2.0.3.0 -Dpackaging=jar

但我想补充的是,上面的许可条款不仅限制了您无法找到JDBC jar的地方,而且还限制了您安装它的地方!

事实上,您的本地Maven存储库必须是私有的,而不是共享的,因为如果它是共享的,它将是一种单独分发jar的分发,即使是向您的局域网中的一小群人分发,这代表了OTN许可协议的违反。

此外,我认为您应该避免在您的公司存储库管理器(如Artifactory或Nexus)中作为单个工件安装JDBC jar,因为如果安装了它,它仍然会单独分发,即使只分发给您组织中的人员,这代表了OTN许可协议的违反。

你可以在这里找到一个在Maven项目上使用Oracle JDBC驱动程序的Github简单示例项目。

您可以找到持续集成的所有解释+一个示例,并在Travis-CI上运行。

DEMO

pom.xml

<properties>
    <oracle.driver.version>12.2.0.1</oracle.driver.version>
</properties>

<repositories>
    <repository>
        <id>maven.oracle.com</id>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <url>https://maven.oracle.com</url>
        <layout>default</layout>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>maven.oracle.com</id>
        <url>https://maven.oracle.com</url>
    </pluginRepository>
</pluginRepositories>

<dependencies>
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>${oracle.driver.version}</version>
    </dependency>
</dependencies>

mvnsettings.xml

<settings>
    <servers>
        <server>
            <id>maven.oracle.com</id>
            <username>${OTN_USERNAME}</username>
            <password>${OTN_PASSWORD}</password>
            <configuration>
                <basicAuthScope>
                    <host>ANY</host>
                    <port>ANY</port>
                    <realm>OAM 11g</realm>
                </basicAuthScope>
                <httpConfiguration>
                    <all>
                        <params>
                            <property>
                                <name>http.protocol.allow-circular-redirects</name>
                                <value>%b,true</value>
                            </property>
                        </params>
                    </all>
                </httpConfiguration>
            </configuration>
        </server>
    </servers>
</settings>

如何在当地环境中使用

在test/mvnsettings.xml文件中修改${OTN_USERNAME

在test/mvnsettings.xml文件中修改Oracle密码${OTN_PASSWORD}

mvn clean install --settings test/mvnsettings.xml

大家好!最后,我们可以使用甲骨文的官方回购: https://blogs.oracle.com/dev2dev/get-oracle-jdbc-drivers-and-ucp-from-oracle-maven-repository-without-ides

请尝试以下内容:

<dependency>
    <groupId>com.oracle.ojdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>19.3.0.0</version>
</dependency>

下载这个jar并将其放在项目src/lib中。现在可以使用maven安装程序插件了。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <id>install-oracle-jdbc</id>
            <goals>
                <goal>install-file</goal>
            </goals>
            <phase>clean</phase>
            <configuration>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc6</artifactId>
                <version>11.2.0</version>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
                <createChecksum>true</createChecksum>
                <file>${project.basedir}/src/lib/ojdbc6.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

现在你只需要执行mvn clean一次,oracle库就会安装在你的本地maven存储库中。