gpt4 book ai didi

java - 如果依赖包具有相同命名空间的类,如何使 ant 构建失败

转载 作者:行者123 更新时间:2023-12-01 16:22:18 28 4
gpt4 key购买 nike

我们的服务有一个基于插件的架构,我们依赖于我们支持的每个插件包。我们最近遇到一个问题,在运行时我们会收到诸如未找到枚举属性之类的错误,因为插件具有具有相同包名和类名的枚举/类。如果插件具有相同包名和类名的类/枚举,我们如何编写一些测试或使 ANT 构建失败。

最佳答案

迭代插件目录的 jar 文件并查找包重复项的代码示例。

public class JarDuplicatePackageTest {
private static Map<String, Integer> packageCount = new HashMap<>();

static class JarFileFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
return name.endsWith("jar");
}
}

public static void main(String[] args) {

try {
File pluginsDirectory = new File("plugins");
FilenameFilter filter = new JarFileFilter();
File[] files = pluginsDirectory.listFiles(filter);

for(int i = 0; i < files.length; i++) {
File file = files[i];
FileInputStream fileInputStream = new FileInputStream(file);
JarInputStream jarInputStream = new JarInputStream(fileInputStream);

while(jarInputStream.getNextEntry() != null) {
JarEntry entry = jarInputStream.getNextJarEntry();

// list the contents of the jar file
System.out.println(entry.toString());

if(entry.toString().endsWith("class")) {
int incremented = 1;
if(packageCount.containsKey(entry.toString())) {
incremented = packageCount.get(entry.toString()) + 1;
}
packageCount.put(entry.toString(), incremented);
}
}
jarInputStream.close();
fileInputStream.close();
}

packageCount.forEach((k, v) -> {
if(v > 1){ System.out.println("Duplicate: " + k );}
});

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

关于java - 如果依赖包具有相同命名空间的类,如何使 ant 构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62236967/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com