【问题标题】:Maven assembly plugin support Jar format?Maven 程序集插件支持 Jar 格式?
【发布时间】:2009-09-08 10:41:09
【问题描述】:

我已将我的程序集描述符配置为具有 jar 类型的程序集

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

但是,在运行 mvn install 时获取的是 zip 文件而不是 jar。我哪里出错了?

【问题讨论】:

  • 你可以发布你的程序集的其余部分吗?

标签: maven-2 maven-plugin


【解决方案1】:

此配置生成一个带有分类器 jar-assembly 的 jar 程序集,其中仅包含目标/类的内容。如果需要将其他内容添加到 jar 中,您可以添加其他文件集。为确保您的目标目录中没有任何先前运行的 zip 存档,您可以将其删除或运行 mvn clean

<assembly>
  <id>jar-assembly</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
      <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

插件配置应如下所示。注意将 appendAssemblyId 设置为 false 将导致默认 jar 被程序集中的 jar 替换,如果这不是所需的行为,请删除该元素:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-2</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
          <descriptor>src/main/assembly/archive.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
  </executions>
</plugin>    

【讨论】:

    【解决方案2】:

    为什么不使用pre-defined assembly jar-with-dependencies?描述符文件下方:

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

    要通过预定义的描述符使用assembly:assembly,请运行:

    mvn assembly:assembly -DdescriptorId=jar-with-dependencies
    

    要生成程序集作为正常构建周期的一部分,请将单个或单目录 mojo 绑定到包阶段(请参阅 Usage):

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- append to the packaging phase. -->
                <goals>
                  <goal>single</goal> <!-- goals == mojos -->
                </goals>
              </execution>
            </executions>
          </plugin>
          [...]
    </project>
    

    【讨论】:

    • 该描述符将捆绑项目的所有依赖项在 jar 中,这并不总是想要的
    • 你从哪里得到这个链接的?它与 Maven 站点上的不同:maven.apache.org/plugins/maven-assembly-plugin/…
    • @User1 Google 的错 :) 我确实指的是文档的一个非常旧的版本。感谢您指出这一点。
    • 我问是因为 Maven 网站上的那个似乎也没有构建一个 jar-with-dependencies。我不得不使用你指向的那个并将 outputFileNameMapping 作为一个空字符串添加到dependencySet 中。也许你认识我们可以告诉的人。
    • @User1 实际上,在使用预定义描述符时,您不必显式提供程序集描述符,只需提供其名称即可。这很奇怪。我要测试一下。
    猜你喜欢
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2018-11-06
    相关资源
    最近更新 更多