simgineer
simgineer

Reputation: 1898

maven: how to embed a jar dependency within another jar

I need to make an executable jar containing more jars embedded directly in it. I can't unpack the classes from within the original vendor's jars because it will break the signatures and functionality. It appears that the default behavior of maven-shade and maven-assembly plugins are to extract the classes within the jar dependencies and merge them into a new mega jar. How do I just simply embed a jar within a jar in maven or am I stuck needing to use an maven ant run task?

Here is an equivalent ant build.xml target that I'm basically trying to port to a maven cicd pipeline.

<target name="create_cli_jar" depends="clean,init,compile,build_properties" description="Build and package the xyz command line utility">
    <jar destfile="${dist.dir}/${cli.prefix}.jar">
        <manifest>
            <attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
            <attribute name="Rsrc-Main-Class" value="com.xyz.GuiCliMain"/>
            <attribute name="Class-Path" value="."/>
            <attribute name="Rsrc-Class-Path" value="./ commons-net-3.6.jar commons-io-2.6.jar CodeMeter.jar"/>
        </manifest>
        <fileset dir="${dist.dir}" includes="build.properties" />
        <fileset dir="${classes.dir}"/>
        <zipfileset src="lib/jar-in-jar-loader.zip"/>
        <zipfileset dir="lib" includes="commons-net-3.6.jar"/>
        <zipfileset dir="lib" includes="commons-io-2.6.jar"/>
        <zipfileset dir="lib" includes="CodeMeter.jar"/>
    </jar>
</target>

Upvotes: 1

Views: 34

Answers (1)

simgineer
simgineer

Reputation: 1898

Here is a maven-assembly-plugin solution I was able to get working yesterday where I was able to select the jars I wanted to expand vs those that would be left intact and embedded as a jar:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 http://maven.apache.org/xsd/assembly-2.1.1.xsd">
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <!-- these are extracted from their original jars and merged into outer jar -->
    <dependencySet>
      <outputDirectory>/</outputDirectory> 
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack> 
      <scope>runtime</scope> 
      <includes>
        <include>com.xyz:abc-gui</include>
        <include>com.xyz:abc-common</include>
      </includes>
    </dependencySet>

    <!-- these jars are left intact and embedded into the outer jar -->
    <dependencySet>
      <outputDirectory>/</outputDirectory> 
      <useProjectArtifact>true</useProjectArtifact> 
      <unpack>false</unpack> 
      <scope>runtime</scope> 
      <includes>
        <include>com.wibu.codemeter:codemeter</include>
        <include>commons-net:commons-net</include> 
        <include>commons-io:commons-io</include> 
      </includes>
    </dependencySet>
  </dependencySets>
  <!-- include jar in jar launcher classes in outer jar -->
  <fileSets>
    <fileSet>
        <directory>${project.build.directory}/loader-classes</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
        <include>**/*</include> 
        </includes>
    </fileSet>
  </fileSets>
  <files>
    <file>
        <source>${project.basedir}/src/main/resources/abc.jpg</source> 
        <outputDirectory>/resources</outputDirectory> 
    </file>
    <file>
        <source>${project.basedir}/src/main/resources/abcico.jpg</source> 
        <outputDirectory>/resources</outputDirectory> 
    </file>
</files>
</assembly>

here is the pom.xml entry:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.7.1</version>
            <configuration>
                <descriptors> 
                    <descriptor>assembly.xml</descriptor> 
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Rsrc-Main-Class>xyz.MyGuiMain</Rsrc-Main-Class>
                        <Class-Path>.</Class-Path>
                        <Rsrc-Class-Path>./ commons-net-3.6.jar commons-io-2.6.jar codemeter-8.20.jar</Rsrc-Class-Path>
                        <SplashScreen-image>resources/abc.jpg</SplashScreen-image>
                    </manifestEntries>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Upvotes: 1

Related Questions