开发者

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

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 fr开发者_JS百科om 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.


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.


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>


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.


Please try to exclude the dependencies from the POM

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜