Search code examples
javagroovymavengmaven-plugin

How to load/find a JAR resource from inside GMaven script?


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.


Solution

  • 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.