[英]java: getting the right resource
I have a library foo.jar
that contains an svnversion.properties
file (just looks like svnversion=12345M
or whatever) and the following static method in a class SVNVersion
: 我有一个库
foo.jar
,它包含一个svnversion.properties
文件(看起来像svnversion=12345M
或其他)以及class SVNVersion
的以下静态方法:
public static SVNVersion fromString(String s) { ... }
public static SVNVersion fromResources(Class<?> cl) {
ResourceBundle svnversionResource = ResourceBundle.getBundle(
"svnversion", Locale.getDefault(), cl.getClassLoader());
if (svnversionResource.containsKey("svnversion"))
{
String svnv = svnversionResource.getString("svnversion");
return fromString(svnv);
}
else
{
return null;
}
}
I also have a library bar.jar
that contains an svnversion.properties
file as well (let's say it contains svnversion=789
). 我还有一个包含
svnversion.properties
文件的库bar.jar
(假设它包含svnversion=789
)。
However, when I run the following within a class SomeClassInBarJar
that's in bar.jar
: 但是,当我在
bar.jar
的class SomeClassInBarJar
中运行以下内容时:
SVNVersion vfoo = SVNVersion.fromResources(SVNVersion.class);
SVNVersion vbar = SVNVersion.fromResources(SomeClassInBarJar.class);
and I print the result, I see 789
twice. 我打印结果,我看到
789
两次。 Clearly I am not doing this right. 显然,我没有做到这一点。 How do I get the right
svnversion.properties
file in the root of the jar file containing a given class? 如何在包含给定类的jar文件的根目录中获取正确的
svnversion.properties
文件? (assuming it's there) (假设它在那里)
edit: I just tried 编辑:我刚试过
InputStream is = cl.getResourceAsStream("/svnversion.properties");
and it has the same problem. 它有同样的问题。 I seem to be able only to get access to the main jar file's
/svnversion.properties
and not the library's /svnversion.properties
. 我似乎只能访问主jar文件的
/svnversion.properties
而不是库的/svnversion.properties
。
You obviously cannot use this approach, as whatever svnversion.properties
file comes will always be used the classloader. 你显然不能使用这种方法,因为无论
svnversion.properties
文件是什么,都将始终使用类加载器。 It is the same behaviour you would see for classes: if two classes with the same name are on the classpath, it's whatever comes first that is used. 它与您在类中看到的行为相同:如果类路径上有两个具有相同名称的类,那么首先使用的是它。
A (convoluted) approach would be to find out what jar a class belongs to, and then retrieve svnversion.properties
in that jar: 一个(错综复杂的)方法是找出一个类属于哪个jar,然后检索该jar中的
svnversion.properties
:
public static JarFile getJarFile(Class<?> cl) {
URL classUrl = cl.getResource(cl.getSimpleName() + ".class");
if (classUrl != null) {
try {
URLConnection conn = classUrl.openConnection();
if (conn instanceof JarURLConnection) {
JarURLConnection connection = (JarURLConnection) conn;
return connection.getJarFile();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return null;
}
public static SVNVersion fromResources(Class<?> cl) {
JarFile jarFile = getJarFile(cl);
ZipEntry entry = jarFile.getEntry("svnversion.properties");
Properties props = new Properties();
try {
props.load(jarFile.getInputStream(entry));
} catch (IOException e) {
throw new RuntimeException(e);
}
if (props.containsKey("svnversion")) {
String svnv = props.getProperty("svnversion");
return fromString(svnv);
} else {
return null;
}
}
That's why, IMHO, you'd probably be better off storing the svn version number in the class themselves as final static variables (and using svn $Revision$
keyword). 这就是为什么,恕我直言,你可能最好将svn版本号作为最终静态变量存储在类中(并使用svn
$Revision$
keyword)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.