Maven - 在jar中包含依赖库而不解包依赖项?

Mar*_*eon 20 maven-2 client-library maven-assembly-plugin

我们正在尝试构建一个包含解压缩的依赖jar的客户端jar .清单应该有class-path依赖罐子的条目.下面的片段可以工作,但罐子是解压缩的 - 我们怎样才能阻止罐子被解压?

       <plugin>
            <artifactId>maven-assembly-plugin</artifactId>

            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

                <archive>
                  <manifest>
                    <addClasspath>true</addClasspath>
                  </manifest>
                </archive>
            </configuration>

            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

Pas*_*ent 19

实际上,组装使用jar-with-dependencies原因行家解压作为所有依赖性${assembly.dependencySets.dependency.unpack}被设置为true在相应的组件的描述符.

一个简单的修正将是提供类似的组件描述符jar-with-dependencies.xml和修改${assembly.dependencySets.dependency.unpack}false,这样的:

编辑:由于未知原因,使用时的行为<unpack>false</unpack>不完全相同,似乎有必要添加<outputDirectory>/</outputDirectory>到fileSet或您没有得到预期的结果.

<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>
Run Code Online (Sandbox Code Playgroud)


dur*_*dur 5

您可以将依赖项作为jar文件添加到jar中:

装配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>
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

但遗憾的是,您无法使用Class-Path标头manifest.mf,请参阅向JAR文件的类路径添加类:

注:Class-Path头指向类或JAR文件在本地网络上,JAR文件或类通过Internet协议访问中没有JAR文件.要将JAR文件中的JAR文件中的类加载到类路径中,必须编写自定义代码来加载这些类.例如,如果MyJar.jar包含另一个调用的JAR文件MyUtils.jar,则无法使用清单中的Class-Path标头将类MyJar.jar's加载MyUtils.jar到类路径中.