mavenonejar

Maven Jar requires two execution contexts


I need to be able to allow my maven package to be executed in two contexts.

  1. User needs to execute jar from the command line to generate a licence key from an AWT dialog that appears when the user executes java -jar myjar.jar

  2. myjar needs to also download all its transitive dependencies (in the "normal" maven way) when a client project references myjar as a dependency.

Scenario 1 requires the AWT forms jar to be packaged so that the AWT dialog pops up.
Scenario 2 requires all the other dependencies to be downloaded as the client project builds.

How do I package this to keep it as small as possible?

I've tried the jar-plugin, the shade-plugin and the assembly-plugin independently without much luck.

JAR-PLUGIN:
Placing the forms-1.2.1.jar in the lib/ directory and adding it to the classpath doesn't work because java -jar myjar.jar will not load it at the commandline

SHADE-PLUGIN:
Downloads the internet. How to exclude some dependencies?

ASSEMBLY-PLUGIN:
Downloads the internet. How to exclude some dependencies?

Is there a way to package the transitive dependencies of forms-1.2.1.jar (for scenario 1) but exclude all other dependencies at packaging time so they are downloaded at client project build time (for scenario 2).

Can the jar-plugin or assembly-plugin do this?


Solution

  • What you think about the following:

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <!-- NOTE: We don't need a groupId specification because the group is
                 org.apache.maven.plugins ...which is assumed by default.
             -->
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.2</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
            [...]
    </project>
    

    That will produce a jar file which contains all dependencies and make it possible to call it from command line, but to get it working correctly you need a supplemental part:

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.2</version>
            <configuration>
              [...]
              <archive>
                <manifest>
                  <mainClass>org.sample.App</mainClass>
                </manifest>
              </archive>
            </configuration>
            [...]
          </plugin>
          [...]
    </project>