Insert the dependent jar into the installer jar

I have a multi-module maven project with an installer sub-project. The installer will be distributed as an executable JAR. It will configure the DB and extract the WAR file to the application server. I would like to use maven to build this jar like so:

/META-INF/MANIFEST.MF
/com/example/installer/Installer.class
/ Com / example / installation / ...
/server.war

The manifest will have a main class entry pointing to the installer class. How can I get maven to build the jar this way?

+1


source to share


1 answer


You can build a jar using the Maven Build Plugin .

First, you need to add some information to the plugins pom.xml section to make the resulting jar executable:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>com.example.installer.Installer</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

      


I recommend using a separate build descriptor to create the actual installation jar. Here's an example:

<assembly>
  <id>installer</id>

  <formats>
    <format>jar</format>
  </formats>

  <baseDirectory></baseDirectory>

  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <includes>
        <!-- this references your installer sub-project -->
        <include>com.example:installer</include>
      </includes>
      <!-- must be unpacked inside the installer jar so it can be executed -->
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <includes>
        <!-- this references your server.war and any other dependencies -->
        <include>com.example:server</include>
      </includes>
      <unpack>false</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

      




If you saved the build descriptor as "installer.xml", you can create your own jar by building as follows:

mvn clean package assembly: single -Ddescriptor = installer.xml


Hope this helps. Here are some additional links that you might find useful:

+3


source







All Articles