• Maven打包时,将所有依赖包打入jar


    Maven打包时,将所有依赖包打入jar

    1、在src/main下添加一个assembly文件夹,在此文件夹下建立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">
    
        <!--
            必须写,否则打包时会有 assembly ID must be present and non-empty 错误
            这个名字最终会追加到打包的名字的末尾,如项目的名字为 speed-api-0.0.1-SNAPSHOT,
            则最终生成的包名为 speed-api-0.0.1-SNAPSHOT-bin.zip
         -->
        <id>bin</id>
    
        <!-- 打包后的文件格式,可以是zip,tar,tar.gz,tar.bz2,jar,war,dir -->
        <formats>
            <format>zip</format>
        </formats>
    
        <!-- 压缩包下是否生成和项目名相同的根目录 -->
        <includeBaseDirectory>false</includeBaseDirectory>
    
        <dependencySets>
            <dependencySet>
                <!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 -->
                <useProjectArtifact>false</useProjectArtifact>
                <outputDirectory>lib</outputDirectory>
                <unpack>false</unpack>
            </dependencySet>
        </dependencySets>
    
        <fileSets>
            <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
            <fileSet>
                <directory>${project.basedir}</directory>
                <outputDirectory></outputDirectory>
                <includes>
                    <include>README*</include>
                    <include>LICENSE*</include>
                    <include>NOTICE*</include>
                </includes>
            </fileSet>
    
            <!-- 把项目的配置文件,打包进zip文件的config目录 -->
            <fileSet>
                <directory>${project.basedir}/src/main/resources</directory>
                <outputDirectory>config</outputDirectory>
            </fileSet>
    
            <!-- 把项目的脚本文件,打包进zip文件的bin目录 -->
            <fileSet>
                <directory>${project.basedir}/src/main/bin</directory>
                <outputDirectory>bin</outputDirectory>
            </fileSet>
    
            <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
            <fileSet>
                <directory>${project.build.directory}</directory>
                <outputDirectory></outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>
    

    2、在pom.xml中,添加如下内容

     <build>
            <plugins>
                <!-- 指定启动类,将依赖打成外部jar包 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                            <addMavenDescriptor>false</addMavenDescriptor>
                            <manifest>
                                <!-- 是否要把第三方jar放到manifest的classpath中 -->
                                <addClasspath>true</addClasspath>
                                <!-- 外部依赖jar包的最终位置 -->
                                <classpathPrefix>lib/</classpathPrefix>
                                <!-- 项目启动类 -->
                                <mainClass>com.zbrx.speed.App</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
    
                <!-- 使用assembly打包 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptors>
                            <!-- assembly配置文件位置 -->
                            <descriptor>src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
    
                <!-- 打包发布时,跳过单元测试 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    3、maven install

    install后 target目录就会新增一个xxx-version-bin.zip,其内部就会存在本项目所有maven依赖的jar包了。

  • 相关阅读:
    在Fedora 20下使用TexturePacker
    实战微信JS SDK开发:贺卡制作与播放(1)
    Fedora 20下安装Google PinYin输入法
    Netty5 时间服务器 有粘包问题
    Netty5入门学习笔记003-TCP粘包/拆包问题的解决之道(下)
    Netty5入门学习笔记002-TCP粘包/拆包问题的解决之道(上)
    11g Rac 添加日志组
    工作中的生长与完善——Leo鉴书86
    搜集直方图repeat和skewonly
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxError Exception
  • 原文地址:https://www.cnblogs.com/zhangruifeng/p/14807368.html
Copyright © 2020-2023  润新知