在编程时包括一个库 &在基于 NetBeans Maven 的项目中编译,但从构建中排除 [英] Include a library while programming & compiling, but exclude from build, in NetBeans Maven-based project

查看:19
本文介绍了在编程时包括一个库 &在基于 NetBeans Maven 的项目中编译,但从构建中排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我需要访问我的 JDBC 驱动程序中的某些特定类="http://www.Vaadin.com/" rel="nofollow noreferrer">Vaadin 网络应用.但是在 Web 应用程序中,我们通常不会在我们的构建中捆绑 JDBC 驱动程序(.war 文件).相反,JDBC 驱动程序属于由 Servlet 容器(运行时环境)控制的文件夹.

I need to access some specific classes in a specific JDBC driver in my Vaadin web app. But in web apps, we normally do not bundle JDBC drivers within our build (the .war file). Instead, the JDBC drivers belong in a folder controlled by the Servlet container (the runtime environment).

所以,我需要 JDBC 驱动程序(jar 文件)是在我编辑代码和编译时在 classpath 上.但是必须从构建中省略该 jar 文件.

So, I need the JDBC driver (a jar file) to be on the classpath while I am editing my code and compiling. But that jar file must be omitted from the build.

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

I tried adding the exclusions and exclusion tags to my dependency element. But this did not work – The postgresql-9.4-1201.jdbc41.jar appeared in WEB-INF/lib folder.

<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>

新个人资料?

ZNK - M 对此问题的回答,在netbeans 中为maven 项目设置自定义运行时类路径,可能是我需要的.

This Answer by ZNK - M on the Question, Setting custom runtime classpath for a maven project in netbeans, may be what I need.

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

But creating a new project profile seems like overkill what seems like small little task to me. And, I always want to exclude this jar from my build output, not just when testing or in other limited scenarios.

您应该在 pom 中添加一个新的配置文件 run-with-netbeans,以声明额外的依赖项(使用提供的范围不将它们包含在发布中).

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).

然后,您必须将新配置文件添加到您的 IDE 中,以在命令行中使用 -P run-with-netbeans 选项运行 pom.

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 文件的基础知识.如果这种方法是可行的,那么如果有人可以详细说明所需的细节和步骤,那将会很有帮助.

But I am familiar only with the basics of editing a POM file. If that approach is the way to go, it would be helpful if someone could expand on the details and steps needed.

推荐答案

provided

在POM文件中使用标签,值为provided.

摘自依赖范围页面的一部分,依赖机制简介 :

Excerpt from the Dependency Scope section of the page, Introduction to the Dependency Mechanism :

compile
这是默认范围,如果没有指定则使用.编译依赖项在项目的所有类路径中都可用.此外,这些依赖项会传播到依赖项.

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.

提供
这很像编译,但表明您希望 JDK 或容器在运行时提供依赖项.例如,在为 Java Enterprise Edition 构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖项设置为提供的范围,因为 Web 容器提供这些类.此范围仅在编译和测试类路径上可用,不可传递.

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.

运行时
[…]

测试
[…]

系统
[…]

导入
[…]

像这样:

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

这篇关于在编程时包括一个库 &amp;在基于 NetBeans Maven 的项目中编译,但从构建中排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆