mvn项目压缩打包
通常情况下,maven打包结果为jar或war包。
如果需要一并打包配置文件等参数,通过resources配置指定需要打包的文件参数,如下示例:
<project>
...
<!-- 定义环境 -->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<properties>
<!-- 定义profileActive属性 -->
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile> <!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile> <!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles> <build>
...
<!-- 资源配置 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>xxx-dev.yml</exclude>
<exclude>xxx-test.yml</exclude>
<exclude>xxx-prod.yml</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>xxx-${profileActive}.yml</include>
</includes>
</resource>
</resources>
...
</build>
</project>
注:通过resources配置打包的文件是一并打包到jar包或war包中的(位于classpath路径下)。
而在生产环境部署项目时,却需要动态修改数据库配置等参数,此时如果仅仅打一个war进行部署,对于配置文件修改非常不方便。
那这里就提出2个问题:
第一,J2EE项目是否可以支持配置文件在war包之外?
第二,如何通过maven将项目文件进行统一打包压缩?
Q1: 在J2EE项目中使用Spring框架时,可以将配置文件写在properties文件中,从外部加载相应配置参数。示例如下:
web.xml添加配置文件路径参数:
<context-param>
<param-name>confDir</param-name>
<param-value>file:${catalina.home}/conf</param-value><!--将配置文件放到tomcat目录下-->
<!-- <param-value>classpath:</param-value> --><!--将配置文件放在项目classpath路径下-->
</context-param>
spring加载配置文件:( 详见:https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ 之7.8 Container Extension Points)
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>${confDir}/jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>
spring数据源配置:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<property name="initialPoolSize" value="${initialPoolSize}" />
<property name="minPoolSize" value="${minPoolSize}" />
<property name="maxPoolSize" value="${maxPoolSize}" />
<property name="breakAfterAcquireFailure" value="${breakAfterAcquireFailure}" />
<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" />
<property name="acquireRetryAttempts" value="${acquireRetryAttempts}" />
<property name="acquireIncrement" value="${acquireIncrement}" />
<property name="acquireRetryDelay" value="${acquireRetryDelay}" />
<property name="testConnectionOnCheckout" value="${testConnectionOnCheckout}" />
<property name="testConnectionOnCheckin" value="${testConnectionOnCheckin}" />
</bean>
Q2: 通过maven插件maven-assembly-plugin将项目文件进行统一压缩打包,更专业,详见:http://maven.apache.org/plugins/maven-assembly-plugin/ 。
使用该插件时需要单独定义一个打包规则文件,如:assembly.xml,在该文件中定义一系列需要打包的文件参数。
pom.xml:
<build>
<plugins>
<!-- 部署打包: 通过maven-assembly插件压缩为tar包进行发布 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<finalName>xxx-${project.version}</finalName>
<!-- 定义压缩包存放位置 -->
<outputDirectory>${basedir}/release</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>verify</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
assembly.xml:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>distribution</id>
<formats>
<format>tar</format>
</formats>
<fileSets>
<!-- 打包配置文件 -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/conf</outputDirectory>
<includes>
<include>jdbc.properties</include>
</includes>
</fileSet>
<!-- 打包sql文件 -->
<fileSet>
<directory>${basedir}</directory>
<outputDirectory>/sql</outputDirectory>
<includes>
<include>*.sql</include>
</includes>
</fileSet>
<!-- 打包INSTALL,change.log -->
<fileSet>
<directory>${basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>INSTALL</include>
<include>change.log</include>
</includes>
</fileSet>
<!-- 打包war文件 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.war</include>
</includes>
</fileSet>
</fileSets>
<!-- 定义压缩包根目录名称 -->
<baseDirectory>xxx-${project.version}</baseDirectory>
</assembly>
mvn项目压缩打包的更多相关文章
- Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.java
Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in ...
- vue-webpack项目自动打包压缩成zip文件批处理
为什么需要这个? 使用vue框架开发项目,npm run build这个命令会一直用到,如果需要给后端发包,那你还要打包成zip格式的压缩包,特别是项目提测的时候,一天可能要执行重复好几次,所以才有了 ...
- r.js压缩打包(require + backbone)项目开发文件
最近项目稳定了一点,之前一直没空关注的开发文件压缩打包问题也有时间来解决了 AMD模块化开发中的代码压缩打包工具——r.js 环境搭建基于nodejs:用于AMD模块化开发中的项目文件压缩打包,不是A ...
- springboot将项目源代码打包
springboot将项目源代码打包并发布到仓库 如果我们有一些类和方法是公用的,可以打开公用包,而这时使用默认的build方式都所有依赖都打进去,而且你当然项目的文件虽然在包里,但却在boot-in ...
- maven创建项目,打包出可执行Jar
官网参考 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 配置多种打包方式 这个例子也不错 https://bl ...
- r.js压缩打包
AMD模块化开发中的代码压缩打包工具——r.js 环境搭建基于nodejs:用于AMD模块化开发中的项目文件压缩打包,不是AMD模式也是可以的 javascript部分 压缩javascript项目开 ...
- Maven项目的打包发布到Nexus私服和服务器
1.编写pom文件如下: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...
- 【linux】【jenkins】jenkins构建、mvn或者npm打包、docker运行、失败自动回滚脚本
小白对jenkins运维的使用有点简单的想法,这里开个记录贴记录下. 由于未找到jenkins构建失败后执行其他脚本的插件,也暂时没有使用其他运维工具.所以想自己写一个shell脚本,一是方便其他人使 ...
- node作为前台的项目如何打包静态js和css并生成版本号,loader插件的使用
一.使用场景: 1.node创建的前台项目需要输入地址展示页面 2.有设置缓存或者cdn的需要在静态文件更改时能使用新的而不是缓存的,需要版本号这里 3.可能需要压缩静态文件的 二.一些参考地址,需要 ...
随机推荐
- [CQOI2017]老C的方块
题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=4823 题解 观察那四种条件 有没有什么特点? 我们可以把蓝线两边的部分看做两个区域,这样 ...
- CF932E Team Work(第二类斯特林数)
传送门:CF原网 洛谷 题意:给定 $n,k$,求 $\sum\limits^n_{i=1}\dbinom{n}{i}i^k\bmod(10^9+7)$. $1\le n\le 10^9,1\le k ...
- AITP
AITP AITP比较简单,适合刚毕业,即将开始IT生涯的学生.接受CIPS认证课程的毕业生可以自动获得AITP证书,并获得一年的免费的CIPS会员资格. 接受非CIPS认证课程的毕业生需要申请,另外 ...
- bootstrap 栅栏系统
媒体查询 /* 超小屏幕(手机,小于 768px) */ /* 没有任何媒体查询相关的代码,因为这在 Bootstrap 中是默认的(还记得 Bootstrap 是移动设备优先的吗?) */ /* 小 ...
- 加密解密DES之Android、IOS、C#实现
Android实现 package com.sto.express.utils; import java.security.MessageDigest; import java.security.sp ...
- dispatchEvent(AWTEvent) 分派事件
点一个按钮,显示的分派一个指定的事件给系统. 下面是一个例子,当点击close按钮时,分派一个new WindowEvent(this,WindowEvent.WINDOW_CLOSING)事件给系统 ...
- 第一篇-Win10打开txt文件出现中文乱码
如果刚开始安装的是英文的Win10系统,那么打开txt文件时很容易出现乱码问题.包括打开cmd窗口,也是不能显示中文的.当然,麻烦的处理方法是: 在cmd中想要显示中文:先输入chcp 936,之后中 ...
- Good Bye 2018 D. New Year and the Permutation Concatenation
传送门 https://www.cnblogs.com/violet-acmer/p/10201535.html 题意: 求 n 的所有全排列组成的序列中连续的 n 个数加和为 n*(n+1)/2 的 ...
- CentOS7 yum 安装 PHP 5.6.24
配置yum源 追加CentOS 6.5的epel及remi源. # rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel- ...
- Percona xtrabackup
关于percona xtrabackup percona xtrabackup是世界上唯一开源的.免费的MySQL热备份软件,可以为InnoDB存储引擎和XtraDB存储引擎进行不间断的备份, ...