【问题标题】:Trouble getting started with maven assembly plugin开始使用 Maven 程序集插件时遇到问题
【发布时间】:2023-03-16 17:26:01
【问题描述】:

很抱歉在这里听起来很无知,但我是 Maven 的新手,并且一直在努力解决一些我确信很简单的事情。

docs 说:

[...] 项目可以生成一个 ZIP 程序集,该程序集在根目录中包含项目的 JAR 工件、在 lib/ 目录中的运行时依赖项以及用于启动独立应用程序的 shell 脚本。

正是我想要做的!但我似乎无法做到。

我的POM如下:

<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.anearalone</groupId>
   <artifactId>myapp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   [...]
   <build>
      <finalName>myapp</finalName>
      <plugins>
         <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-assembly-plugin</artifactId>
         <version>2.2.1</version>
         <executions>
            <execution>
               <id>make-assembly</id>
               <phase>package</phase>
               <goals>
                  <goal>single</goal>
               </goals>
               <configuration>
                  <descriptors>
                     <descriptor>src/main/assemble/dist.xml</descriptor>
                  </descriptors>
                  <archive>
                     <manifest>
                        <mainClass>com.anearalone.myapp.CLI</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                     </manifest>
                  </archive>
               </configuration>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build>
[...]
</project>

并且引用的 dist.xml 看起来像这样:

<assembly>
   <id>dist</id>
   <formats>
      <format>zip</format>
   </formats>
   <files>
      <file>
         <outputDirectory>/</outputDirectory>
         <source>src/main/bin/arkify.sh</source>
         <fileMode>755</fileMode>
      </file>
   </files>
   <dependencySets>
      <dependencySet>
         <useProjectArtifact>false</useProjectArtifact>
          <includes>
             <include>*:jar</include>
          </includes>
          <outputDirectory>/lib</outputDirectory>
      </dependencySet>
      <dependencySet>
         <useProjectArtifact>true</useProjectArtifact>
         <includes>
            <include>com.anearalone:myapp:jar:0.0.1-SNAPSHOT</include>
         </includes>
         <outputDirectory>/</outputDirectory>
      </dependencySet>
   </dependencySets>
</assembly>

这实现了我在 zip 文件中想要的布局(尽管我很确定我没有以正确的方式到达那里)但是我在 target/ 中得到了两个罐子(一个在 zip 存档中,另一个在根),并且它们都没有在结果 MANIFEST.MF 中包含我的 mainClass 条目。

如果我将 project.packaging 更改为“pom”,我认为这可能是正确的,那么额外的 jar(target/ 的根目录中的)当然会消失,但我会收到以下警告:

[WARNING] Cannot include project artifact: com.anearalone:myapp:pom:0.0.1-SNAPSHOT; it doesn't have an associated file or directory.
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o  'com.anearalone:myapp'

...确实我的工件不在存档中,MANIFEST.MF 中仍然没有添加任何条目。

有人有时间帮助初学者吗?

【问题讨论】:

    标签: maven-2 maven-assembly-plugin


    【解决方案1】:

    如果我正确理解您的问题,则说明您的 ZIP 已正确创建,但其中包含的 my-app-0.0.1-SNAPSHOT(以及直接位于 target/ 目录中的 JAR)在 MANIFEST.MF 文件中不包含您的主类?

    其实assembly插件并不是专门用来执行这样的任务的。这是JAR plugin 的任务,它提供了一种在MANIFEST.MF the main class of your project 中指示的方法。您只需在当前的pom.xml添加此配置:

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>my.app.MainClass</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
    

    关于您尝试将项目的包装更改为 pom 包装:这是个坏主意 ;) 实际上,pom 包装用于项目,除了 pom.xml 本身之外没有任何其他资源。这对于定义为the parent of others projectspom.xmlaggregate multiples modules 非常有用。

    【讨论】:

    • 啊,完美!我应该从usage page 的前几行中推断出这一点。非常感谢!
    猜你喜欢
    • 2017-04-21
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 2019-05-28
    • 2018-11-21
    相关资源
    最近更新 更多