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路径中找到!!!

工具包的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包的更多相关文章
- Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图(转载)
		Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图 2017年04月05日 10:53:13 李学凯 阅读数:104997更多 所属专栏: Intellij Idea ... 
- Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图
		Maven 组件界面介绍 如上图标注 1 所示,为常用的 Maven 工具栏,其中最常用的有: 第一个按钮:Reimport All Maven Projects 表示根据 pom.xml 重新载入项 ... 
- eclipse maven 项目导出为 jar 包
		一个 maven 项目有很多依赖,所以最后打出的 jar 一般会很多,且比较大,打成 jar 包的步骤 (注意pom.xml文件中打包类型不能是war包): 1. 把 pom.xml 中依赖的库打成 ... 
- eclipse导入maven项目后依赖jar包更新问题->update project按钮
		eclipse导入maven项目后依赖jar包更新问题 1.eclipse有专门的导入maven项目按钮,file-import-maven project,eclipse会自动查找指定路径下的pom ... 
- 构建maven项目,自定义目录结构方法
		构建maven项目 创建自定义的文件目录方法: 在项目名称右键-->Builder Path-->Configure Builder Path...Source菜单下的Add Folder ... 
- 非web环境的注解配置的spring项目应用(non-web, Spring-data-jpa, JavaConfig, Java Application, Maven, AnnotationConfigApplicationContext)
		非web环境的spring应用 springframework提供的spring容器,非常适合应用于javaweb环境中. 同时,spring组件的低耦合性为普通java应用也提供了足够的支持. 以下 ... 
- Java 将Maven项目打成可执行jar包
		一.用maven-shade-plugin打包 在pom.xml文件中加入如下信息,利用Maven的maven-shade-plugin插件进行打包. <build> <plugin ... 
- maven项目依赖其他jar包的时候,idea运行没问题,java -jar 报错:java.lang.SecurityException: Invalid signature file digest
		当项目依赖其他jar包的时候,打出的jar包执行出错,抛出这个异常. 原因:因为依赖jar包中的META-INF中有多余的.SF文件与当前jar包冲突, 解决方案 一 在打包前删除依赖jar包的.SF ... 
- 【转】IntelliJ IDEA 创建 hello world Java web Maven项目
		学Java的大部分吧都是要整Java web开发项目的,那么最好用的编辑器估计就是这个 IntelliJ IDEA,然后现在maven管理项目是很流行的.然后我就示范一下,如何使用这个IntelliJ ... 
随机推荐
- Django Rest Framework源码剖析(七)-----分页
			一.简介 分页对于大多数网站来说是必不可少的,那你使用restful架构时候,你可以从后台获取数据,在前端利用利用框架或自定义分页,这是一种解决方案.当然django rest framework提供 ... 
- MySQL清理慢查询日志slow_log的方法
			一.清除原因 因为之前打开了慢查询,导致此表越来越大达到47G,导致磁盘快被占满,使用xtrabackup进行备份的时候文件也超大. mysql> show variables like 'lo ... 
- Shiro安全框架学习笔记
			一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和JavaEE项目中都可以使用.它主要用来处理身份认证,授权,企业会话管理 ... 
- 9、Dockerfile实战-Nginx
			上一节我们详解Dockerfile之后,现在来进行实战.我们通过docker build来进行镜像制作. build有如下选项: [root@localhost ~a]# docker build - ... 
- mac10.12.6系统配置clion编写CMakeLists文件运行opencv3
			按照mac10.12.6系统使用cmake安装opencv3.3.0+opencv_contrib-3.3.0下载编译安装好了文件以后,装好clion编译器,新建C++可执行工程,编写代码 opecv ... 
- OpenGL 笔记 <2> Compiling and Linking a shader program
			Preface 这一节所有的主要内容都在一个OpenGL库文件中<LoadShaders.h> ,只需要用LoadShader()函数进行加载即可.但是由于老是出错,所以自己实现了一下,也 ... 
- 在HTML中为JavaScript传递变量
			在html中为JavaScript传递变量是一个关键步骤,然后就可以通过对JavaScript变量操作,实现想要达到的目的 本节代码主要使用了JavaScript中的document对象中的getEl ... 
- VGGNet论文翻译-Very Deep Convolutional Networks for Large-Scale Image Recognition
			Very Deep Convolutional Networks for Large-Scale Image Recognition Karen Simonyan[‡] & Andrew Zi ... 
- 本科毕业平均年薪 30 万!经济寒冬挡不住 AI 人才的火热!
			互联网行业遭遇寒冬,企业纷纷裁员缩招,而 BAT 和硅谷明星公司对 AI 人才的投入却并不见放缓.为争夺相关人才,给应届毕业生开出的平均年薪高达 30 万. 而 TensorFlow 作为当下最流行的 ... 
- 20135337朱荟潼 Linux第三周学习总结  ——Linux内核源代码简介
			朱荟潼 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课http://mooc.study.163.com/course/USTC 1000029000 知识笔记 1.ar ... 
