我已尝试过多种方法在单元测试中从正确的jar访问清单 . 我遇到的问题是它似乎正在读取它所涉及的第一个jar文件,而不是我的类 . 这是我最近的尝试
InputStream is = Test.class.getResourceAsStream("/META-INF/MANIFEST.MF");
try {
if (is == null) {
System.out.println("null no input stream");
} else {
Manifest manifest = new Manifest(is);
Attributes attributes = manifest.getMainAttributes();
v = attributes.getValue("Test");
System.out.println("Test="+v);
v = attributes.getValue("Created-by");
System.out.println("By="+v);
}
} catch (IOException e) {
System.out.println("error");
} finally {
if (is != null) {
is.close();
}
}
这些都是结果
Test=null By=1.6.0_18 (Sun Microsystems Inc.)
MANIFEST.MF
我想使用 Test
的值,所以我知道这是读错文件
有谁知道如何指定包含清单文件的jar用于单元测试?
5 回答
这就是我想出来的.....注意用你的类/对象替换
HTMLDocumen
t .你可以尝试这个:
您可以访问包含您的类的jar:
我在你的代码中更改了这一行并得到了相同的结果,直到你找到清单文件然后从那里读取它 .
"Test"不是main attribute .
尝试getEntries()并在那里查找您的自定义属性 .
这是我最终使用的方法......
它的所有路径操作和jar名称的硬编码都很脏......这有关系吗?再一次谢谢你!