Google Reflections & Maven - 在 Maven 中找不到数据但在 IDE 中有效
Google Reflections & Maven - not finding data in Maven but works in IDE
问题
简单处理请求:在java.util
包中找到List.class
的所有扩展。
这是我使用的来源:
Reflections reflections = new Reflections("java.util");
Set<Class<?>> subtypes = reflections.getSubTypesOf(List.class);
很简单,对吧?
它在 IntelliJ IDEA 和 Eclipse 中都有效,但是当我 运行 我通过 Maven 进行测试时它不起作用。我尝试使用 org.reflections.util.ConfigurationBuilder
提供的添加 URL 和过滤包名称的方法来添加东西,但没有成功。
有什么建议吗?
我查看了这个 post 但无法正常工作:“Unit test using the Reflections google library fails only when executed by Maven”
代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>ohno</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sample.java
package com.example.uhoh;
import org.reflections.Reflections;
import java.util.Set;
public class Sample {
@SuppressWarnings ("unchecked")
public static Set<Class<?>> lookup(Class<?> type) {
Reflections reflections = new Reflections("java.util");
return (Set<Class<?>>) reflections.getSubTypesOf(type);
}
}
SampleTest.java
package com.example.uhoh;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
import java.util.Set;
@Test
public class SampleTest {
public void testSample() {
Set<Class<?>> result = Sample.lookup(List.class);
Assert.assertNotNull(result, "NULL returned.");
Assert.assertFalse(result.isEmpty(), "Unable to find any extensions of java.util.List");
}
}
输出
在IDE工作
Maven 输出
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.uhoh.SampleTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@4e515669
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.261 sec <<< FAILURE!
(com.example.uhoh.SampleTest) Time elapsed: 0.071 sec <<< FAILURE!
java.lang.AssertionError: Unable to find any extensions of java.util.List expected [false] but found [true]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertFalse(Assert.java:63)
at com.example.uhoh.SampleTest.testSample(SampleTest.java:15)
Results :
Failed tests: (com.example.uhoh.SampleTest): Unable to find any extensions of java.util.List expected [false] but found [true]
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
并行错误
代码
public void testSample() {
Class[] 数据类型 = {
ArrayList.class, LinkedList.class,
List.class, 摘要List.class,
Collection.class,
Map.class、Properties.class
};
Arrays.stream(dataTypes).parallel().forEach(next -> {
Set<Class<?>> result = Sample.lookup(next);
Assert.assertNotNull(result, "NULL returned.");
Assert.assertFalse(result.isEmpty(), "Unable to find any extensions of java.util.List");
});
}
Maven 输出
java.lang.IllegalStateException: java.lang.IllegalStateException: zip file closed
要解决您的问题,请使用 Reflections 的 ConfigurationBuilder
为包含 class 的 jar 手动设置 URL。这在 IntelliJ 中有效,并且在使用命令行 Maven 调用 Surefire 插件时,mvn test
.
Sample.java
public static Set<Class<?>> lookup(Class<?> type) {
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
// For List.class, this will be a path to rt.jar.
configurationBuilder.addUrls(ClasspathHelper.forClass(type));
Reflections reflections = new Reflections(configurationBuilder);
return (Set<Class<?>>) reflections.getSubTypesOf(type);
}
我刚刚在 Reflections 库(0.9.11 版)中遇到了同样的问题,只是在从 Maven 构建执行单元测试时。对我的 Surefire 插件的一个简单的 POM 文件更改为我解决了这个问题。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<useSystemClassLoader>
配置参数默认为 'true'。强制它 'false' 似乎解决了我单元测试中的类加载器问题。
问题
简单处理请求:在java.util
包中找到List.class
的所有扩展。
这是我使用的来源:
Reflections reflections = new Reflections("java.util");
Set<Class<?>> subtypes = reflections.getSubTypesOf(List.class);
很简单,对吧?
它在 IntelliJ IDEA 和 Eclipse 中都有效,但是当我 运行 我通过 Maven 进行测试时它不起作用。我尝试使用 org.reflections.util.ConfigurationBuilder
提供的添加 URL 和过滤包名称的方法来添加东西,但没有成功。
有什么建议吗?
我查看了这个 post 但无法正常工作:“Unit test using the Reflections google library fails only when executed by Maven”
代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>ohno</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sample.java
package com.example.uhoh;
import org.reflections.Reflections;
import java.util.Set;
public class Sample {
@SuppressWarnings ("unchecked")
public static Set<Class<?>> lookup(Class<?> type) {
Reflections reflections = new Reflections("java.util");
return (Set<Class<?>>) reflections.getSubTypesOf(type);
}
}
SampleTest.java
package com.example.uhoh;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
import java.util.Set;
@Test
public class SampleTest {
public void testSample() {
Set<Class<?>> result = Sample.lookup(List.class);
Assert.assertNotNull(result, "NULL returned.");
Assert.assertFalse(result.isEmpty(), "Unable to find any extensions of java.util.List");
}
}
输出
在IDE工作
Maven 输出
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.uhoh.SampleTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@4e515669
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.261 sec <<< FAILURE!
(com.example.uhoh.SampleTest) Time elapsed: 0.071 sec <<< FAILURE!
java.lang.AssertionError: Unable to find any extensions of java.util.List expected [false] but found [true]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertFalse(Assert.java:63)
at com.example.uhoh.SampleTest.testSample(SampleTest.java:15)
Results :
Failed tests: (com.example.uhoh.SampleTest): Unable to find any extensions of java.util.List expected [false] but found [true]
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
并行错误
代码 public void testSample() { Class[] 数据类型 = { ArrayList.class, LinkedList.class, List.class, 摘要List.class, Collection.class, Map.class、Properties.class };
Arrays.stream(dataTypes).parallel().forEach(next -> {
Set<Class<?>> result = Sample.lookup(next);
Assert.assertNotNull(result, "NULL returned.");
Assert.assertFalse(result.isEmpty(), "Unable to find any extensions of java.util.List");
});
}
Maven 输出
java.lang.IllegalStateException: java.lang.IllegalStateException: zip file closed
要解决您的问题,请使用 Reflections 的 ConfigurationBuilder
为包含 class 的 jar 手动设置 URL。这在 IntelliJ 中有效,并且在使用命令行 Maven 调用 Surefire 插件时,mvn test
.
Sample.java
public static Set<Class<?>> lookup(Class<?> type) {
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
// For List.class, this will be a path to rt.jar.
configurationBuilder.addUrls(ClasspathHelper.forClass(type));
Reflections reflections = new Reflections(configurationBuilder);
return (Set<Class<?>>) reflections.getSubTypesOf(type);
}
我刚刚在 Reflections 库(0.9.11 版)中遇到了同样的问题,只是在从 Maven 构建执行单元测试时。对我的 Surefire 插件的一个简单的 POM 文件更改为我解决了这个问题。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<useSystemClassLoader>
配置参数默认为 'true'。强制它 'false' 似乎解决了我单元测试中的类加载器问题。