1

I need to reuse some python code in a Clojure library. I want to bundle the python scripts with my .jar, but as far as I can tell, that means python won't be able to easily access these scripts, as they'll be packaged inside the jar file.

Is there a way to specify that the resources of a .jar must be placed directly in the file system so that outsiders can get to them?

August Lilleaas
  • 54,010
  • 13
  • 102
  • 111

1 Answers1

1

There isn't an "auto-extract = true" feature built into the JAR standard. You'll have to roll your own solution.

You have several different options:

1) JAR files are basically zip archives. So, outsiders can access the scripts by reading the JAR as a zip. You can ship a bash script with your application that uses unzip to extract the script from your file.

2) In Maven, you can the Dependency plugin's dependency:unpack goal to extract the scripts from the jar so they can be placed into a more suitable location in your distribution assembly. So use something like this (below is untested):

   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <version>2.5.1</version>
     <executions>
       <execution>
         <id>unpack</id>
         <phase>package</phase>
         <goals>
           <goal>unpack</goal>
         </goals>
         <configuration>
           <artifactItems>
             <artifactItem>
               <groupId>com.augustl</groupId>
               <artifactId>scripts</artifactId>
               <version>1.0.0</version>
               <type>jar</type>
               <overWrite>false</overWrite>
               <outputDirectory>${project.build.directory}/scripts</outputDirectory>
               <includes>**/*.py</includes>
             </artifactItem>
           </artifactItems>
         </configuration>
       </execution>
     </executions>
   </plugin>

3) In your application, you can read the script as an InputStream and write it to a temporary file for execution. As long as your script has a fairly unique name, this is fairly straight-forward using Class.getResourceAsStream(String name)

See also:

Community
  • 1
  • 1
noahlz
  • 10,202
  • 7
  • 56
  • 75
  • How do you access the .jar file when it's just a dependency though? Typically, the contents of the jar is made available via the classpath and classloaders, but getting to the file itself might prove tricky. Also, this probably requires the users of the .jar to manually unzip it somehow, which isn't very elegant.. – August Lilleaas Sep 16 '12 at 17:49
  • What do you mean "just a dependency?" Maven dependency plugin will copy and unpack any jar you depend on wherever you want. http://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html – noahlz Sep 16 '12 at 20:43