在基于 NetBeans Maven 的项目中,在编程和编译时包含一个库,但从构建中排除

Include a library while programming & compiling, but exclude from build, in NetBeans Maven-based project

在 NetBeans 8 中,在基于 Maven 的项目中,如何在编程时使用 jar 而在构建时省略?

我需要访问特定 JDBC driver in my Vaadin web app. But in web apps, we normally do not bundle JDBC drivers within our build (the .war file 中的某些特定 类。相反,JDBC 驱动程序属于由 Servlet 容器(运行时环境)控制的文件夹。

所以,我在编辑代码和编译时需要 JDBC 驱动程序(一个 jar file) to be on the classpath。但是构建中必须省略那个 jar 文件。

exclusions 标签

我尝试将 exclusionsexclusion 标签添加到我的 dependency 元素。但这没有用——postgresql-9.4-1201.jdbc41.jar 出现在 WEB-INF/lib 文件夹中。

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1201-jdbc41</version>
    <exclusions>
        <exclusion>
            <groupId>org.postgresql</groupId>  Exclude from build 
            <artifactId>postgresql</artifactId>
        </exclusion>
    </exclusions>
</dependency>

新配置文件?

This Answer by ZNK - M on the Question, Setting custom runtime classpath for a maven project in netbeans,可能是我需要的

但是创建一个新的项目配置文件似乎有点矫枉过正,这对我来说似乎是一件小事。而且,我 总是 想从我的构建输出中排除这个 jar,而不仅仅是在测试或其他有限的场景中。

You should add a new profile run-with-netbeans in your pom that declares the additional dependencies (use the provided scope to not include them in the release).

Then you'll have to add the new profile to your IDE to run the pom with the -P run-with-netbeans option in the command line.

但我只熟悉编辑POM file的基础知识。如果这种方法是可行的方法,那么如果有人可以详细说明所需的细节和步骤,那将会很有帮助。

使用 provided 范围而不是此依赖项的默认 compile 范围。这正是它的用途。

<dependency>
    <scope>provided</scope>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
</dependency>

<scope>provided</scope>

在POM文件中使用<scope>标签,值为provided

摘自Dependency Scope section of the page, Introduction to the Dependency Mechanism

compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

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.

runtime
[…]

test
[…]

system
[…]

import
[…]

像这样:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1201-jdbc41</version>
    <scope>provided</scope>
</dependency>