带有依赖项的 Maven 构建程序集 [英] Maven build assembly with dependencies

查看:34
本文介绍了带有依赖项的 Maven 构建程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有独立的 Java 应用程序,我想将其打包为:myapp.jar.并将所有相关 jar 复制到备用文件夹".理想情况下,我希望 maven 更新 META-INF 文件以将所有类路径依赖项 jars 条目添加到其中.

I have standalone java application, which i wanted to package as: myapp.jar. And all dependent jars to be copied to 'alternate folder'. Ideally, i would like to have maven update META-INF file to add all classpath dependencies jars entries into it.

例如,如果我的项目引用的是 commons.jar,并且当我使用此插件构建程序集时,它会将所有 .class 文件和包从 commons.jar 复制到 myappjar-with-dependencies.jar.

for example, if my project is referencing commons.jar, and when i use this plugin to build assembly, it copies all .class files and packages from commons.jar into myappjar-with-dependencies.jar.

maven assembly 插件的问题,将所有依赖解压到 myappjar-with-dependencies.jar 中.

The problem with maven assembly plugin unjars all dependencies into myappjar-with-dependencies.jar.

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.core.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> 
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>

推荐答案

你可以试试

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.core.App</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/bin.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>

src/assembly/bin.xml

src/assembly/bin.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">
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>false</unpack>
            <includes>
                <include>${artifact}</include>
            </includes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <unpack>false</unpack>
            <excludes>
                <exclude>${artifact}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

运行它

mvn clean package assembly:single

详情:http://maven.apache.org/plugins/maven-assembly-plugin/index.html

这篇关于带有依赖项的 Maven 构建程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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