【问题标题】:Maven: Build runnable jar and then add to zip with maven-assembly-pluginMaven:构建可运行的 jar,然后使用 maven-assembly-plugin 添加到 zip
【发布时间】:2013-01-30 14:06:55
【问题描述】:

我有一个 maven 问题,应该很容易解决,但事实证明有点困难。

我有一个构建 jar 的项目,并且需要该 jar 才能执行 com.foo.bar.MainClass 类,但我需要能够将它与一些脚本及其依赖项一起部署,例如

some-product-1.0.0.zip
  bin/
    some-product.sh
  lib/
    some-product-1.0.0.jar
    dependency1.jar
    dependency2.jar

我可以很容易地制作这个结构。我的pom.xml 看起来像

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.foo.bar.MainClass</mainClass>
                <addClasspath>true</addClasspath>
            </manifest>
        </archive>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
        </descriptors>
    </configuration>
</plugin>

而我的assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <files>
        <file>
            <source>src/main/scripts/some-product.sh</source>
            <outputDirectory>/bin</outputDirectory>
        </file>
    </files>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>compile</scope>
        </dependencySet>
    </dependencySets>
</assembly>

some-product.sh 看起来像

SCRIPTLOC=$( readlink -f $( dirname "${BASH_SOURCE[0]}" ) )
LIBS="$( dirname $SCRIPTLOC )/lib"
# builds a : joined list of all the jars in the lib directory
MYCLASSES=$(echo $LIBS/*.jar | tr ' ' ':')

java -cp "$CLASSPATH:$MYCLASSES" com.foo.bar.MainClass

但是当我运行脚本时得到的是

Exception in thread "main" java.lang.NoClassDefFoundError: com/foo/bar/MainClass
Cause by: java.lang.ClassNotFoundException com.foo.bar.MainClass
    at ...
Could not find the main class: com.foo.bar.MainClass.  Program will exit.

当我检查包含该类的 jar 时,清单只有

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: <myname>
Build-Jdk: 1.6.0_27

告诉我它没有将主类添加到清单中。

我已经用谷歌搜索了这个问题很长一段时间没有运气,并且程序集插件网站上的示例很糟糕。如何让 maven 正确构建此 jar 并将其添加到 zip 中?

编辑:我的 pom 中确实需要 maven-jar-plugin,但我真正的问题是我使用的是 Cygwin,它预期的类路径分隔符是 ; 而不是 :。我的解决方案是编写一些代码来确定要使用哪个类路径分隔符,并且使用 jar 插件现在可以完美运行。

【问题讨论】:

  • 为什么不让您的生活更轻松,并使用已经处理所有这些事情的appassembler-maven-plugin。它为 windows/unix 生成一个脚本,并为你处理所有的类路径。

标签: java maven maven-assembly-plugin


【解决方案1】:

我看到的两件事是

  1. classpath 设置不正确。

     SCRIPTLOC=$( readlink -f $( dirname "${BASH_SOURCE[0]}" ) )
    

    这里的 SCRIPTLOC 不是根目录。是&lt;the_complete_path_from_root&gt;/bin

     LIBS="$( dirname $SCRIPTLOC )/lib"
    

    这最终评估为&lt;the_complete_path_from_root&gt;/bin/lib

    稍后您将向其中添加 lib 文件夹,该文件夹实际上并不包含您的 jar。因此,要添加到类路径的正确路径是存在 jar 的 lib 文件夹,然后它将能够找到 com.foo.bar.MainClass

  2. 要将 Main 类添加到清单文件中,您需要将以下内容添加到 pom.xml 文件中。

    <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <configuration>
           <archive>
             <manifest>
               <mainClass>com.someclass.Main</mainClass>
               <packageName>com.someclass</packageName>
             </manifest>
             <manifestEntries>
                  <mode>development</mode>
                  <url>${pom.url}</url>
              </manifestEntries>
            </archive>
          </configuration>
      </plugin>
    

【讨论】:

  • 1.类路径肯定设置正确,我之前已放入 echo 语句以确保。 dirname 命令将获取父目录 (see documentation) 2. 我试过了,但我得到了同样的错误,NoClassDefFoundException
  • ok.. 所以这是您需要添加的maven-jar-plugin sn-p,以便在清单文件中包含主类。
  • 对不起,我在完成之前不小心提交了我的评论(总是忘记它们不是多行并按回车键)。我试过那个 sn-p,但它仍然给我同样的错误 =/
  • @bhek 我已经创建了一个聊天室,如果您需要讨论chat.stackoverflow.com/rooms/24530/maven-assembly-jar-issue
猜你喜欢
  • 2010-11-28
  • 1970-01-01
  • 2017-07-07
  • 2017-08-29
  • 1970-01-01
  • 2016-10-13
  • 2014-04-01
  • 2011-06-24
相关资源
最近更新 更多