views:

320

answers:

3

I want to exclude one dependency when I'm doing deploy, I need it when I use the jar locally, but it should not be in deployed jar. is there any way to do it?

A: 

Maybe a good solution is to set the dependency with a <scope>provided</scope>. This way, the dependency will not be deployed.

However, locally, you will need to add the dependency in your classpath...

romaintaz
+2  A: 

It depends on what you mean by "use the jar locally".

If you mean that you don't want the jar to be included in any bundles, you can set the scope of the dependency to provided. This scope is only available on the compilation and test classpath, and is not transitive. A dependency with this scope will not be included in wars/ears.

<dependency>
  <groupId>some.groupid</groupId>
  <artifactId>my-dependency</artifactId>
  <version>1.0.0</version>
  <scope>provided</scope>
</dependency>

If you mean you don't want the jar to be bundled into a distribution built with the assembly plugin, you can configure the assembly to exclude a specific dependency.

Rich Seller
A: 
<profile>
    <id>localProfile</id>
    <activation>
     <property>
      <name>!deploy</name>
     </property>
    </activation>
    <dependencies>
     <dependency>
      ...
     </dependency>
    </dependencies>
</profile>

when i run it with

mvn deploy -D deploy=0

the jar doesnt have that dependency

01
While this approach works, Maven already provides a means to avoid deploying dependencies (see my answer), with this approach you run the risk of accidentally deploying the jar if you forget to include the property in your build command. Is there a reason why the provided scope won't work for you?
Rich Seller
provided scope excludes jar always and i wanted only to exclude it when i deploy.
01