开发者

How can I use different JARs for compiling and testing in maven?

开发者 https://www.devze.com 2022-12-17 13:59 出处:网络
I compile my programm against javaee-api. But for Junit testing I must use a specific implementation like glassfish\'s javaee.jar to avoid errors like java.lang.ClassFormatError: Absent Code attribute

I compile my programm against javaee-api. But for Junit testing I must use a specific implementation like glassfish's javaee.jar to avoid errors like java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence (see also 1).

So avoid using methods, that are only available in glassfish implementation, I want to compile my artifact with the general api, but run junit with the implementation 开发者_Go百科jar. But both provide equal named classes and inferfaces, so the class loader gets in trouble.

What is the best way to solve this problem? Can I solve this problem with maven?

Thanks a lot


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.

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>
0

精彩评论

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

关注公众号