18

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.

Ali Shakiba
  • 20,549
  • 18
  • 61
  • 88
  • Have you looked at this post ? http://stackoverflow.com/questions/3720768/ignore-maven-dependency-during-specific-execution-phase – Stephan Mar 25 '11 at 12:27
  • @Stephan Thanks, I have found maven profiles cleaner to get around this in different environments. – Ali Shakiba Mar 26 '11 at 01:33
  • 1
    Profiles work really well to have multiple, separate build environments. We use this at work because our build server hosts `system` dependencies in a different location than local does. – jpaugh Apr 26 '16 at 22:07

4 Answers4

7

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.

Petr Kozelka
  • 7,670
  • 2
  • 29
  • 44
  • As you can read [here](https://stackoverflow.com/questions/3545292/how-to-get-maven-project-version-to-the-bash-command-line) maven is also used to just get some information. However, this fails if a dependency can not be resolved. E.g. to perform a release you could modify the pom.xml or maven.config and set the new version. If you want to verify that now maven is using that version you may want to call `mvn -q exec:exec -Dexec.executable=echo -Dexec.args='${project.version}'` but this will then fail what is a pitty. – Jörg Apr 05 '19 at 12:34
1

assuming the missing dependency is only important in the local environment you can use a combination of profile and activation via your .m2/settings.xml

You remove the dependency from your general dependencies and move it as a referenced dependency into a profile of your pom.xml (Project level profile) and activate the profile through your m2/settings.xml.

You may also remove the dependency completly from your pom.xml and move the dependency into a profile which resides in your .m2/settings.xml (User level profile)

see the introduction to profiles section

another way which fits your needs maybe better

The activation of the above mentioned profile based on the presence of the file in .m2/repository/local/dependency/1.0.0-Snapshot/local.jar

<profiles>
  <profile>
    <activation>
      <file>
        <exists>/home/user/.m2/repository/local/dependency/1.0.0-Snapshot/local.jar</exists>
      </file>
    </activation>
    ...
  </profile>
</profiles>
p1brunne
  • 444
  • 3
  • 3
  • I know how to use profiles as you can see in my question and my comment under my question. – Ali Shakiba May 29 '11 at 04:00
  • I added the file based activation of the profile which should help you further. – p1brunne May 31 '11 at 20:05
  • It's not a cross system solution. I have even tried this with maven properties (before asking this question) but for some reason which I couldn't figure out it didn't work. – Ali Shakiba Jun 01 '11 at 02:06
  • By cross system you mean different platforms (*nix, windows, etc.) or different local developer machines (different absolute paths)? – p1brunne Jun 01 '11 at 20:33
0

Bizarrely, I had a problem with maven where it would say "cannot resolve dependency" (of some weird transitive dependency that didn't exist anymore), but it only failed with that error message if my ~/.m2/settings.xml had a

got my jars from a local repository

  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>a name</id>
          <url>http://somewhere/local/a</url>
        </repository>

If I changed it to mirror, it would not fail.

  <mirrors>
    <mirror>
      <id>a name</id>
      <name>an awesome description</name>
      <url>http://some/where/local</url>
      <mirrorOf>external:*</mirrorOf>

(same repo). Go figure.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
-2

Please try to exclude the dependencies from the POM

<executions>
<execution>
<id>***failed dependency Id***</id>
</execution>
</executions>