【问题标题】:building a jar and including it in a zip with maven-assembly-plugin使用 maven-assembly-plugin 构建一个 jar 并将其包含在一个 zip 中
【发布时间】:2011-06-24 03:02:10
【问题描述】:

我有一个 mavenized java 项目 (Maven2),我想将它构建到一个 jar 中,这很容易通过在 pom.xml 中提供 jar-with-dependencies 描述符Ref。

但是,我还需要将我的项目部署在一个包含一些 .exe 和 .bat 文件的 zip 中,其中包括来自调用 jar 的 bin 文件夹。 (我正在使用Tanuki,但我认为的用例并不重要)

换句话说,我需要一个构建,其中首先将我的源代码(和依赖项)打包到一个 jar 中,然后将该 jar 与 bin 文件夹中的一些附加文件一起放入一个 zip 中。

我应该在我的 pom.xml 和 'assembly'.xml 中放什么?

【问题讨论】:

    标签: maven-2 build maven-assembly-plugin


    【解决方案1】:

    Maven-assembly-plugin 是执行此操作的正确工具。

    您必须在 pom 的“build”部分声明此插件,并在项目的根目录中创建另一个配置文件“assembly.xml”。在此文件中,您将定义 zip 文件的内容。

    配置选项描述在官网:http://maven.apache.org/plugins/maven-assembly-plugin/

    这是该插件的基本配置示例,应该可以满足您的需求。

    POM 配置:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <finalName>zipfile</finalName>
            <descriptors>
                <descriptor>${basedir}/assembly.xml</descriptor>
            </descriptors>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    组装配置:

    <assembly>
        <formats>
            <format>zip</format>
        </formats>
    
        <fileSets>
            <fileSet>
                <directory>to_complete</directory>
                <outputDirectory />
                <includes>
                    <include>**/*.jar</include>
                    <include>**/*.bat</include>
                    <include>**/*.exe</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>
    

    【讨论】:

    • 感谢您的回答,这是我让它工作所需的关键开始。
    • 我确实需要添加另一个描述符来制作我的 jar。实际上,我只是从您提到的站点复制了 jar-with-dependencies 描述符格式,因为使用 jar-with-dependencies descriptorRef 封装了 zip 构建。即它首先构建 'sic' jar,然后是 zip,然后是 with-dependencies.jar,它使用第一个(无用的)jar 构建一个 zip,但没有第二个,这很奇怪吧?!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2013-01-30
    • 2010-11-28
    • 2013-01-30
    • 2015-05-17
    • 2013-02-19
    相关资源
    最近更新 更多