Declare Maven dependency as test runtime only

前端 未结 4 1198
半阙折子戏
半阙折子戏 2021-02-04 00:03

What is the best way to declare a Maven dependency as only being used for the test runtime (but not test compilation) class path?

Specifically, I want slf4j-api

相关标签:
4条回答
  • 2021-02-04 00:21

    There is no scope that does exactly what you want here; test is the best available option.

    A test-runtime scope has been requested before (Re: Need for a test-runtime scope?) and the suggested workaround is exactly the ignoreNonCompile configuration you've already discovered.

    dependency:analyze already has some limitations ("some cases are not detected (constants, annotations with source-only retention, links in javadoc)"). You may have to accept that any test-scope dependencies that it warns against are false positives.

    (You could split the definition of your tests into a separate module, which would have no slf4j implementation dependencies, then run them in another module. I don't think that would be worth it.)

    0 讨论(0)
  • 2021-02-04 00:23

    As a workaround i would suggest to have separate maven project with test cases that depend on main project

    0 讨论(0)
  • 2021-02-04 00:28

    Since maven-dependency-plugin 2.10 (revision 1649454, Jan 2015), you can also add to the configuration a list of ignoredDependencies, ignoredUnusedDeclaredDependencies and ignoredUsedUndeclaredDependencies.

    0 讨论(0)
  • 2021-02-04 00:30

    There is no concept of test-runtime in maven. The only real downside is the dependency analysis identifying these runtime test dependencies as unused. Since they are only test dependencies, however, this is pretty benign and cannot cause issues to other projects transitively dependent on this project.

    0 讨论(0)
提交回复
热议问题