且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在maven中使用不同的JAR进行编译和测试?

更新时间:2023-11-09 08:35:58

我认为这是可能的。实际上,从版本2.0.9开始,Maven使用POM命令来构建类路径,因此您现在可以对其进行操作。如果将其与依赖范围结合使用,它应该有可能实现你想要的。实际上,如果您放置GlassFish的 javaee 依赖项(带有测试范围) 之前 javaee-api 依赖项,前者应该放在测试类路径中的后面,因此在单元测试中使用,而后者将在编译期间使用。从理论上讲,这应该有用,但它有点脆弱所以需要仔细记录。

I think that this is possible. Actually, starting with version 2.0.9, Maven uses the POM order to build the classpath, so you can manipulate it now. And if you combine this with Dependency Scope, it should be possible to achieve what you want. In practical terms, if you place GlassFish's javaee dependency (with a test scope) before the javaee-api dependency, the former should be placed before the later in the test classpath and thus used by unit tests while the later will be used during compile. In theory, this should work but it is kinda fragile so it needs to be carefully documented.

类似的东西(带有虚构的GFv3 jar):

Something like that (with a fictional GFv3 jar):

<dependencies>
  <dependency><!-- this one will be first on the test classpath -->
    <groupId>org.glassfish</groupId>
    <artifactId>javaee</artifactId>
    <version>6.0</version>
    <scope>test</scope>
  <dependency>
  <dependency><!-- this one will be used during compile -->
    <groupId>javax.javaee-api</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
  <dependency>
  ...
</dependencies>