1

In NetBeans 8, in a Maven-based project, how does one use a jar while programming but omit from build?

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

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.

exclusions Tag

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>

New Profile?

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.

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.

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

2 Answers2

1

Use the provided scope instead of the default compile scope for this dependency. That's exactly what it's for.

<dependency>
    <scope>provided</scope>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
</dependency>
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

<scope>provided</scope>

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

Excerpt from the 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
[…]

Like this:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1201-jdbc41</version>
    <scope>provided</scope>
</dependency>
Community
  • 1
  • 1
PatrykG
  • 93
  • 5
  • Thanks to you for that (and JB Nizet too)! I verified that does indeed work perfectly. Disabling the `scope` tag causes the `postgresql-9.4-1201.jdbc41.jar` jar file to appear in my build’s `WEB-INF/lib` folder. Enabling that `scope` element omits the jar. – Basil Bourque Aug 19 '15 at 06:44
  • FYI: More discussion on related Question, [What's the difference between these Maven dependency scopes: provided/compile/system/import](https://stackoverflow.com/q/16907682/642706) – Basil Bourque Jan 22 '18 at 01:30