Maven - Include dependent libs in jar without unpacking dependencies?

后端 未结 3 454
Happy的楠姐
Happy的楠姐 2021-01-03 21:19

We\'re trying to build a client jar that includes unpacked dependent jar\'s. And the manifest should have class-path entries to the dependent jars. T

相关标签:
3条回答
  • 2021-01-03 22:04

    Indeed, assembling using jar-with-dependencies causes maven to unpack all the dependencies as ${assembly.dependencySets.dependency.unpack} is set to true in the corresponding assembly descriptor.

    A simple fix would be to provide an assembly descriptor similar to the jar-with-dependencies.xml and modify ${assembly.dependencySets.dependency.unpack} to false, like this:

    EDIT: For an unknown reason, the behavior when using <unpack>false</unpack> is not exactly the same and it seems necessary to add <outputDirectory>/</outputDirectory> to the fileSet or you don't get the expected result.

    <assembly>
      <id>uberjar</id>
      <formats>
        <format>jar</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <dependencySets>
        <dependencySet>
          <unpack>false</unpack>
          <scope>runtime</scope>
        </dependencySet>
      </dependencySets>
      <fileSets>
        <fileSet>
          <directory>${project.build.outputDirectory}</directory>
          <outputDirectory>/</outputDirectory>
        </fileSet>
      </fileSets>
    </assembly>
    
    0 讨论(0)
  • 2021-01-03 22:07

    You can add your dependencies as jar files to your jar:

    assembly-descriptor.xml

    <assembly>
        <id>uberjar</id>
        <formats>
            <format>jar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <unpack>false</unpack>
                <scope>runtime</scope>
                <useProjectArtifact>false</useProjectArtifact>
            </dependencySet>
        </dependencySets>
        <fileSets>
            <fileSet>
                <directory>${project.build.outputDirectory}</directory>
                <outputDirectory>.</outputDirectory>
            </fileSet>
        </fileSets>
    </assembly>
    

    pom.xml

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <executions>
            <execution>
                <id>make-uberjar</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptor>src/main/assemble/assembly-descriptor.xml</descriptor>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    But unfortunately you can't use the Class-Path header in manifest.mf, see Adding Classes to the JAR File's Classpath:

    Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over Internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.

    0 讨论(0)
  • 2021-01-03 22:22

    The solution proposed by Pascal Thivent defines a new assembly for the Maven assembly plugin. The Maven assembly plugin offers defaults assemblies which are 'bin', 'jar-with-dependencies', 'project' and 'src' producing various predefined bundles.

    A new assembly has to be defined in a new xml file, most of the time located in src/assemble. Then it will be loaded instead of the predefined one, this way:

    <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
          <configuration>
    
              <!-- disabled predefined assembly -->
              <!--
              <descriptorRefs>
                 <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              -->
    
              <descriptors>
                 <descriptor>src/assemble/myAssembly.xml</descriptor>
              </descriptors>
          </configuration>
    </plugin>
    
    0 讨论(0)
提交回复
热议问题