How can I add a packr step to a maven build script?

1.3k Views Asked by At

I'm trying to adjust my build procedure to produce a standalone executable. There are a few tools that do this and Packr seems like the perfect one. Also as far as I can tell it is officially supported my maven.

After spending roughly an epoch of googlin' I haven't had any luck finding a simple maven XML example of a packr build step. Also I'm very new to Maven which makes this more complicated. It seems to me though, that there should be a fairly standard XML block where only a few jar names would need to be changed to get it working.

I'm also working in eclipse and trying to build executables for windows and linux.

Packr: https://github.com/libgdx/packr

Packr in Maven: https://mvnrepository.com/artifact/com.badlogicgames.packr/packr

3

There are 3 best solutions below

1
On

1.Define profiles in pom.xml , for Windows, Linux and Mac OS X 2.Assuming that I have packr.jar downloaded , just invoke the necessary profile

<profiles>
    <profile>
        <id>windows-profile</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.6.0</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>-jar</argument>
                            <argument>${basedir}/packaging/packr.jar</argument>
                            <argument>${basedir}/packaging/config-windows.json</argument>
                        </arguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

To obtain your windows distributive, tell maven the desired profile :

clean install -Pwindows-profile

You can see below also the config-windows.json :

{
  "platform": "windows64",
  "jdk": "C:/Program Files/Java/jdk1.8.0_144",
  "executable": "file-sort",
  "classpath": [
    "target/file-sort-jar-with-dependencies.jar"
  ],
  "mainclass": "com.iscorobogaci.fx.FxApplication",
  "output": "../windows64"
}

Check it out on GitHub : https://github.com/scorobogaci/file-sorting-app

4
On

I have written a maven build plugin that wraps the packr java api. The plugin is built around version 2 of packr.

https://github.com/stevenmz/packer-maven-plugin

0
On

I couldn't find any maven plugin for this either.

There is version 1.2 of packr in maven central, but bear in mind this is now a fairly old version (https://github.com/libgdx/packr/issues/58).

You can use the standard exec-maven-plugin to draw upon the packr 1.2 dependency and then run it (see http://www.mojohaus.org/exec-maven-plugin/examples/example-exec-using-plugin-dependencies.html).

The relevant fragment in the pom.xml build file might look something like:

<build>
  <plugins>         
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.5.0</version>
      <dependencies>
        <!-- https://mvnrepository.com/artifact/com.badlogicgames.packr/packr -->
        <dependency>
          <groupId>com.badlogicgames.packr</groupId>
          <artifactId>packr</artifactId>
          <version>1.2</version>
        </dependency>
      </dependencies>

      <executions>
        <execution>
          <id>package-native-windows</id>

          <phase>package</phase>
          <goals>
            <goal>java</goal>
          </goals>

          <configuration>
            <includeProjectDependencies>false</includeProjectDependencies>
            <includePluginDependencies>true</includePluginDependencies>
            <executableDependency>
              <groupId>com.badlogicgames.packr</groupId>
              <artifactId>packr</artifactId>
            </executableDependency>

            <mainClass>com.badlogicgames.packr.Packr</mainClass>
            <arguments>
              <argument>packr-windows-config.json</argument>
            </arguments>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

If you've already got a <build> and <plugins> section in your pom.xml, then just take the <plugin> bit and put it in there.

The json config file might look something like:

{
  "platform": "windows",
  "jdk": "jre-8u102-windows-x64.zip",
  "executable": "MyApp",
  "appjar": "target/MyApp-jar-with-dependencies.jar",
  "classpath": [ "MyApp-jar-with-dependencies.jar" ],
  "mainclass": "com.foo.MyAppMain",
  "outdir": "target/native-windows"
}

One thing to be aware of, packr 1.2 seems to delete the 'outdir' directory, so be careful to use something other than the 'target' directory.

In case it helps, I personally ended up packaging for windows using launch4j with the launch4j-maven-plugin and packaging for linux/mac using Oracle's standard javapackager tool via the javafx-maven-plugin. I'd link to those two but StackOverflow tells me I don't have enough rep to include more than two links.