使用Maven程序集插件时遇到问题 [英] Trouble getting started with maven assembly plugin

查看:141
本文介绍了使用Maven程序集插件时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,我在这里听起来很无知,但是我是Maven的新手,我一直在与我确定很简单的东西相撞.

I'm sorry to sound ignorant here, but I'm new to Maven, and have been banging my head against something that I'm sure is quite simple.

文档说:

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

[...] a project could produce a ZIP assembly which contains a project's JAR artifact in the root directory, the runtime dependencies in a lib/ directory, and a shell script to launch a stand-alone application.

这正是我想要做的完全!但我似乎无法实现.

which is exactly what I want to do! But I can't seem to make it happen.

我的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看起来像这样:

and the referenced dist.xml looks like this:

<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/中得到了两个jar(一个在zip归档文件中,另一个在zip文件中).根),并且它们都不在结果MANIFEST.MF中包含我的mainClass条目.

This achieves the layout I want in the zip file (though I'm quite sure I'm not getting there in the correct way) but I get two jars in target/ (one in the zip archive, the other in the root), and neither of them includes my mainClass entry in the resultant MANIFEST.MF.

如果我将project.packaging更改为"pom",我认为这是正确的,那么当然,额外的jar(在target/的根目录中消失了,但是我得到了以下警告:

If I change the project.packaging to "pom", which I thought might be correct, of course the extra jar (in the root of target/ goes away, but I get these warning:

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

... and indeed my artifact is not in the archive, and there are still no entries added to MANIFEST.MF.

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

Anyone have time to help out a beginner?

推荐答案

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

If I understand your problem correctly, your ZIP is correctly created, but the my-app-0.0.1-SNAPSHOT contained in it (as well as the JAR directly located in target/ directory) does not include your main class in the MANIFEST.MF file?

实际上,assembly插件并非专用于执行此类任务.这是 JAR插件的任务,它提供了一种指示方法,在MANIFEST.MF 项目的主类中.您只需在当前pom.xml

In fact, the assembly plugin is not dedicated to execute such a task. This is the task of the JAR plugin, which provides a way to indicates, in the MANIFEST.MF the main class of your project. You simply must add this configuration in your current 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其他项目的父项,或汇总多个模块.


Regarding your try to change the packaging of the project to a pom packaging: it was a bad idea ;) Indeed, the pom packaging is used for project without any other resources than the pom.xml itself. It is really useful for pom.xml that are defined as the parent of others projects, or to aggregate multiples modules.

这篇关于使用Maven程序集插件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆