maven依赖项中的两个相同的类。违反密封

问题描述 投票:0回答:1

今天,我遇到了不寻常的情况。为了测试Java EE,我将使用glassfish-embedded-all。在生产中,我将使用apache derby数据库。因此,当我编写用于测试数据库的小型测试类时,出现此错误:

java.lang.SecurityException: sealing violation: can't seal package org.apache.derby.impl.services.locks: already loaded

我的pom:

  <dependencies>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.14.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbynet</artifactId>
            <version>10.14.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.14.2.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.main.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>3.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

问题是嵌入式服务器还具有derby类。因此,我有许多用于编译的类,也有一些针对测试的derby类。而在测试过程中,它会导致我不知道如何解决的冲突。

似乎我需要在测试期间忽略嵌入式服务器中的derиy类。有什么想法吗?

我的测试:

public class JpaTest {
    private static final String PERSISTENCE_UNIT_NAME = "people";
    private EntityManagerFactory factory;

    @Before
    public void setUp() throws Exception {
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        // Error appear here
        EntityManager em = factory.createEntityManager();


P.S。我不能理解Maven阴影插件如何工作。似乎此插件是针对最终jar的。现在不用于单元测试...

P.S.2。如果我尝试在某些Main方法中执行测试,则可以正常工作(如果没有玻璃鱼依赖项)

maven jpa glassfish derby java-ee-7
1个回答
0
投票

如果我记错了,在生命周期的每个阶段都将使用Maven编译范围,因此在测试阶段也将使用具有编译范围的依赖项。该解决方案声称从玻璃鱼中排除了德比依赖性,从而迫使您使用它。

更多,您的依赖项的显式作用域定义为compile,并且未指定其他内容...这些相同,因为maven用作默认作用域compile一个

© www.soinside.com 2019 - 2024. All rights reserved.