简体   繁体   中英

Maven: Local dependecy file not found into war/WEB-INF/lib

I've added my local dependency library ( jar file):

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/libs/ojdbc7-12.1.0.2.jar</systemPath>
</dependency>

Everything works fine, up to when maven generates war artifact.

I've look up inside generated war file, but, jar dependency is not there inside.

Any ideas?

I know I'm able to use maven installfile . I need to focus the problem using this kind of dependency declaration.

From Maven documentation :

system : This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

provided : This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

It seems that system scope needs the container or JDK to provide the dependency as the provided scope. Because of that, the dependency is not packed into the WAR file.

You can pack the dependencies into the lib folder using maven-war-plugin like this:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        ...
        <webResources>
            <resource>
                <directory>libs</directory>
                <targetPath>WEB-INF/lib</targetPath>
                <includes>
                    <include>ojdbc7-12.1.0.2.jar</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

A WAR is an web archive for Servlet Containers like Tomcat, Glassfish, JBoss (...). They are specified by the Servlet Specifications. The specs point out that the Datasources (Database) is in the Field of the Servlet-Containers.

(...) type javax.sql.DataSource for which the reference to the data source is injected by the container prior to the component being made available to the application.

You should place the database-driver to the servlet-container, not the web-application.

Dependencies in compile scope are automatically added to the target's WEB-INF/lib as part of the Maven build. Dependencies in system scope are not, dependencies with a system scope must be explicitly provided by definition. More information on below URL

Maven 2 assembly with dependencies: jar under scope "system" not included

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