我不确定如何将 jar 的路径解析到外部存储库,但假设 jar 在您的本地存储库中,那么您应该可以通过settings.localRepository
隐式变量访问它。然后,您已经知道您的组和工件 ID,因此在这种情况下,您的 jar 的路径是settings.localRepository + "/my-group/my-artifact/1.0/my-artifact-1.0.jar"
此代码应允许您读取 jar 文件并从中获取文本文件。注意我通常不会自己编写这段代码来将文件读入 byte[],我只是为了完整起见把它放在这里。理想情况下使用来自 apache commons 或类似库的东西来做到这一点:
def file = null
def fileInputStream = null
def jarInputStream = null
try {
//construct this with the path to your jar file.
//May want to use a different stream, depending on where it's located
fileInputStream = new FileInputStream("$settings.localRepository/my-group/my-artifact/1.0/my-artifact-1.0.jar")
jarInputStream = new JarInputStream(fileInputStream)
for (def nextEntry = jarInputStream.nextEntry; (nextEntry != null) && (file == null); nextEntry = jarInputStream.nextEntry) {
//each entry name will be the full path of the file,
//so check if it has your file's name
if (nextEntry.name.endsWith("my-file.txt")) {
file = new byte[(int) nextEntry.size]
def offset = 0
def numRead = 0
while (offset < file.length && (numRead = jarInputStream.read(file, offset, file.length - offset)) >= 0) {
offset += numRead
}
}
}
}
catch (IOException e) {
throw new RuntimeException(e)
}
finally {
jarInputStream.close()
fileInputStream.close()
}