【问题标题】:How to fail an ant build if dependent packages have classes with same namespace如果依赖包具有相同命名空间的类,如何使 ant 构建失败
【发布时间】:2020-09-25 22:29:00
【问题描述】:

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

【问题讨论】:

    标签: java kotlin build ant


    【解决方案1】:

    遍历插件目录的 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();
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 2018-11-24
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多