温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java application maven项目如何打自定义zip包

发布时间:2021-07-12 11:40:12 来源:亿速云 阅读:153 作者:chen 栏目:编程语言

本篇内容介绍了“java application maven项目如何打自定义zip包”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1.配置pom.xml文件,添加build节点

<build>
        <!-- 输出的包名 -->
        <finalName>p2p</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>

        <resources>
            <!-- 控制资源文件的拷贝(默认复制到classes目录,最后打进jar包) -->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 排除外置的配置文件(运行时注释上使IDE能读取到配置文件;打包时放开注释让配置文件外置方便修改) -->
                <excludes>
                    <exclude>config.properties</exclude>
                </excludes>
            </resource>
            <!-- 配置文件外置的资源(存放到config目录,也是classpath路径,下面会配置) -->
           <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>config.properties</include>
                </includes>
                <targetPath>${project.build.directory}/config</targetPath>
           </resource>
       </resources>

       <plugins>
           <!-- 设置编译版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- 清单文件,设置入口类和classpath -->
                        <manifest>
                            <mainClass>com.hdwang.Application</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <!-- 给清单文件添加键值对,增加classpath路径,这里将config目录也设置为classpath路径 -->
                        <manifestEntries>
                            <Class-Path>config/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <classesDirectory>
                    </classesDirectory>
                </configuration>
            </plugin>
            <!-- 拷贝依赖的jar包到lib目录 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 解决资源文件的编码问题 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- 自定义打zip包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <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>
        </plugins>
    </build>

这个pom配置文件中注意红色字体部分,这是实现配置文件外置的关键配置,思路就是配置文件不打进jar包,放置到外面,且将此文件夹设置为classpath,这样子程序便可以通过根据classloader很方便地读取到配置文件了。下面给出读取配置文件的java代码,在IDE运行时和打包后,代码都不用修改,因为配置文件总能从classpath路径中找到!!!

java application maven项目如何打自定义zip包

工具包的maven信息

<dependency>
    <groupId>commons-configuration</groupId>
    <artifactId>commons-configuration</artifactId>
    <version>1.10</version>
</dependency>

2.新建maven-assembly-plugin插件的配置文件assembly.xml,内容如下

<assembly>
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <!-- 使用assembly拷贝依赖包 -->
    <!--<dependencySets>-->
        <!--<dependencySet>-->
            <!--<!– 是否包含自己(将项目生成的jar包也输出到lib目录) –>-->
            <!--<useProjectArtifact>false</useProjectArtifact>-->
            <!--<outputDirectory>lib</outputDirectory>-->
        <!--</dependencySet>-->
    <!--</dependencySets>-->
    <fileSets>
        <!-- 从目标目录拷贝文件去压缩 -->
        <fileSet>
            <directory>target</directory>
            <includes>
                <include>*.jar</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target/lib</directory>
            <outputDirectory>/lib</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target/config</directory>
            <outputDirectory>/config</outputDirectory>
        </fileSet>

        <!-- 从源目录拷贝文件去压缩 -->
        <fileSet>
            <directory>src/main/run</directory>
            <includes>
                <include>*.sh</include>
                <include>*.cmd</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main</directory>
            <includes>
                <include>ReadMe.txt</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

这个插件在package生命周期中运行,执行mvn package或者mvn install便可触发此插件的执行。这里我注释掉了拷贝依赖包的代码,是因为在pom.xml文件中已经配置了maven-dependency-plugin执行这样的操作,无须重复配置。fileSet可以配置需要拷贝压缩的文件,directory路径相对于项目根目录,outputDirectory路径相对于输出目录target,includes可以对拷贝的文件进行筛选。这里可以拷贝压缩输出目录的文件,应该就是因为此插件运行在程序编译打包之后,这样子就达到了我们自定义打包的要求:编译->拷贝资源文件->项目打包->拷贝依赖的jar包-> assembly进行拷贝压缩。然后使用打出的zip包就可以去部署发布了,解压后就能运行。

3.程序打包流程示意图

java application maven项目如何打自定义zip包

“java application maven项目如何打自定义zip包”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI