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

查看:120
本文介绍了在编程时添加一个库在基于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 对此问题的回答,

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

但是创建一个新的项目配置文件似乎太过分了,对我来说似乎是个小小的任务.而且,我总是要将此罐子从构建输出中排除,而不仅仅是在测试时或在其他受限情况下.

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.

推荐答案

<scope>provided</scope>

在POM文件中使用<scope>标记,其值为provided.

<scope>provided</scope>

Use <scope> tag in POM file, with a value of provided.

摘录自依赖性范围页面上的依赖机制简介:

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

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.

运行时
[…]

runtime
[…]

测试
[…]

test
[…]

系统
[…]

system
[…]

导入
[…]

import
[…]

赞:

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

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

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