Is there a Maven “compiler-only” scope for dependency artifacts

三世轮回 提交于 2019-12-03 22:14:43

If the jars with are not true "runtime" dependencies (only for building) but not for the final artifact, you can exclude them various means:

  1. Exclusion in the assembly descriptor
  2. Exclusion in the jar (or war, ear, whatever) plugin configuration
  3. Shade Plugin minimize jar goal

I agree that shipping unnecessary classes is annoying (I've seen junit and testng jars in production deployments - brrrr...), but for all practical purposes it's a rather minor one.

If you have a dependency conflict (i.e shipping a "all deps" version of a library or framework), that's a different story, but doesn't sound like what you're facing here.

Don't complain that the language does not offer fine distinctions if you only know half its vocabulary.

If a dependency is only used for building, such as an annotation processor, it should be a maven <plugin>, or <dependency> thereof.

Otherwise, if it is in the compilation classpath, it will be necessary for linking the generated class file at runtime. Then, there are two cases:

  1. It is always necessary to load that class
    1. the class should be shipped as part of the application: <scope>compile</scope>
    2. the class should be provided by the runtime environment: <scope>provided</scope>
  2. It is sometimes necessary (because that class will be loaded only under particular circumstances): <optional>true</optional>

The only option not covered is compiling a Java program, and never running it in a JVM. This is a really obscure use case, and I can't fault the designers of maven for not including a scope just to express this distinction - particularly since it is irrelevant to Maven's core responsibility (building the software).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!