考虑这个结构:
/project
/module-1
/src/test/resources/META-INF/persistence.xml
/module-2
在模块1中,创建了测试jar。模块-1 / pom.xml中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
此测试jar是module-2 / pom.xml中的依赖项:
<dependency>
<groupId>com.domain.test</groupId>
<artifactId>module-1</artifactId>
<scope>test</scope>
<type>jar</type>
</dependency>
问题是在模块2的测试中,无法找到/src/test/resources/META-INF/persistence.xml中定义的持久性单元(PU)。 PU以编程方式创建:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit);
EntityManager entityManager = entityManagerFactory.createEntityManager();
我怎样才能让它发挥作用?谢谢。
您声明的依赖项不是针对您创建的测试jar。你应该这样声明:
<dependency>
<groupId>com.domain.test</groupId>
<artifactId>test-shared</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>