Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Maven to ignore a dependency if failed to resolve it?

Tags:

maven

I have a dependency which I have installed in Maven local repository and is being used locally but is not available on deployment server. I use that dependency using Class.forName(...) so there will be no problem if it's missed from classpath on deployment server.

Is there any way to tell Maven to ignore a dependency if it failed to resolve it?

I doesn't seem that <scope> or <optional> can solve this problem, but it may be possible to do it with <profiles> if there is any way to activate/deactivate a profile based on dependencies availability.

like image 552
Ali Shakiba Avatar asked Feb 26 '11 09:02

Ali Shakiba


People also ask

How you can exclude dependency in Maven?

You can use Exclude command from the context menu in the Maven dependency diagram to quickly exclude the specified dependency from POM and the respective tool windows. The dependency is also excluded from the Project and Maven tool windows.

How do you exclude a transitive dependency in Maven?

There we can exclude all transitive dependencies without specifying groupId and artifactId of the dependencies. So need to use astric(*) character as groupid and artifactid of the dependency. This wildcard transitive dependencies ignoring is available with maven 3.2.

What is Maven exclusion?

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.


1 Answers

Short answer: no.

Long answer: Once dependency is declared - either in common part or in an active profile - it must be resolvable when Maven attempts it; otherwise the build fails.

If maven allowed the requested behavior, the reproducibility of build would suffer a lot.

If obscurity and irreproducibility is not an issue for you, here is a hint how to do it:

  • call external ant from your pom.xml, using either exec-maven-plugin or maven-antrun-plugin
  • in the ant code, use artifact:dependencies from Maven Ant Tasks
  • wrap it in ant-contrib's trycatch block

In any case, I strongly discourage including such things into maven build. Having it as separate functionality, invoked via ant from commandline, might often be enough.

like image 140
Petr Kozelka Avatar answered Oct 06 '22 20:10

Petr Kozelka