如何从 GMaven 脚本内部加载/查找 JAR 资源?

发布于 2024-10-19 09:01:12 字数 922 浏览 3 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巷子口的你 2024-10-26 09:01:12

答案非常简单:

def url = getClass().getClassLoader().getResource("my-file.txt");

那么 URL 将采用以下格式:

jar:file:/usr/me/.m2/repository/grp/art/1.0-SNAPSHOT/art.jar!/my-file.tex

其余的都很简单。

The answer is very simple:

def url = getClass().getClassLoader().getResource("my-file.txt");

Then the URL will be in the following format:

jar:file:/usr/me/.m2/repository/grp/art/1.0-SNAPSHOT/art.jar!/my-file.tex

The rest is trivial.

千笙结 2024-10-26 09:01:12

如果文件位于 Jar 中,那么从技术上讲,它不是文件,而是 Jar 条目。这意味着您有以下可能性:

If the file is in the Jar, then it's technically not a file, but a Jar entry. Which means you have these possibilities:

转瞬即逝 2024-10-26 09:01:12

我不确定如何解析外部存储库的 jar 路径,但假设该 jar 位于本地存储库中,那么您应该可以通过 settings.localRepository 隐式变量访问它。然后你已经知道你的组和工件 ID,所以你的 jar 的路径,在这种情况下将是 settings.localRepository + "/my-group/my-artifact/1.0/my-artifact-1.0.jar"< /code>

此代码应该允许您读取 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()
    }

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 be settings.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:

    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()
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文