有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我们正在尝试构建一个包含解包的依赖jar的客户机jar。清单应该有class-path个依赖jar条目。下面的代码片段可以正常工作,但是罐子已经打开了-我们如何阻止罐子被打开

       <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>

共 (3) 个答案

  1. # 1 楼答案

    Pascal Thivent提出的解决方案为Maven assembly插件定义了一个新的程序集。Maven assembly插件提供了默认的程序集,它们是“bin”、“jar with dependencies”、“project”和“src”,生成各种预定义的捆绑包

    新的程序集必须在新的xml文件中定义,大多数情况下位于src/assemble中。然后它将被加载,而不是预定义的,如下所示:

    <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>
    
  2. # 2 楼答案

    实际上,使用jar-with-dependencies进行组装会导致maven解压所有依赖项,因为${assembly.dependencySets.dependency.unpack}在相应的组装描述符中被设置为true

    一个简单的修复方法是提供一个类似于jar-with-dependencies.xml的程序集描述符,并将${assembly.dependencySets.dependency.unpack}修改为false,如下所示:

    EDIT:由于未知原因,使用<unpack>false</unpack>时的行为不完全相同,似乎有必要将<outputDirectory>/</outputDirectory>添加到文件集,否则无法获得预期结果

    <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>
    
  3. # 3 楼答案

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

    程序集描述符。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>
    

    但不幸的是,您不能在manifest.mf中使用Class-Path头,请参见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.