如何从 GMaven 脚本内部加载/查找 JAR 资源?
这是我的 gmaven 脚本,它试图查找并加载位于提供的依赖项(它是 pom.xml
的一部分):
[...]
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<configuration>
<source>
<![CDATA[
def File = // how to get my-file.txt?
]]>
</source>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>my-group</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
[...]
my-file.txt
位于 my-group:my-artifact:1.0
> JAR 文件。
This is my gmaven script, which is trying to find and load a file located somewhere inside the provided dependency (it's a section of pom.xml
):
[...]
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<configuration>
<source>
<![CDATA[
def File = // how to get my-file.txt?
]]>
</source>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>my-group</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
[...]
The my-file.txt
is located in my-group:my-artifact:1.0
JAR file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案非常简单:
那么 URL 将采用以下格式:
其余的都很简单。
The answer is very simple:
Then the URL will be in the following format:
The rest is trivial.
如果文件位于 Jar 中,那么从技术上讲,它不是文件,而是 Jar 条目。这意味着您有以下可能性:
InputStream
ClassLoader 或手动 Jar 处理
dependency:unpack
或依赖项:解包依赖项
)If the file is in the Jar, then it's technically not a file, but a Jar entry. Which means you have these possibilities:
InputStream
using either theClassLoader or manual Jar processing
dependency:unpack
ordependency:unpack-dependencies
)我不确定如何解析外部存储库的 jar 路径,但假设该 jar 位于本地存储库中,那么您应该可以通过
settings.localRepository
隐式变量访问它。然后你已经知道你的组和工件 ID,所以你的 jar 的路径,在这种情况下将是settings.localRepository + "/my-group/my-artifact/1.0/my-artifact-1.0.jar"< /code>
此代码应该允许您读取 jar 文件并从中获取文本文件。请注意,我通常不会自己编写此代码来将文件读入 byte[] 中,我只是将其放在这里是为了完整性。理想情况下,使用 apache commons 或类似库中的内容来完成此操作:
I'm not sure how to resolve the path to a jar to an external repository, but assuming the jar is in your local repository then you should have access to that via the
settings.localRepository
implicit variable. You then known your group and artifact id already, so the path to your jar, in this case would besettings.localRepository + "/my-group/my-artifact/1.0/my-artifact-1.0.jar"
This code should allow you to read in the jar file and get the text file from it. Note I wouldn't normally write this code to read a file into a byte[] myself, I just put it here for completeness. Ideally use something from apache commons or a similar library to do it: