maven参考文档
maven坐标体系
在maven中,所有的依赖都是通过坐标计算的,坐标体系有以下元素:
- groupId
代表公司或组织的域名,我们的项目全部使用com.lefu - artifactId
代表项目或工程名称,类如boss - version
项目版本,以-SNAPSHOT结尾的代表为快照版本,其他的为RELEASE版本 - packaging
依赖类型,常见的有pom,jar,war,默认为jar,非必需
maven中通过指定前面3者就能唯一定位一个依赖。
maven仓库
maven中,所有的依赖和插件都放置在仓库内,仓库有几种类型,中心仓库,代理仓库,本地仓库,另外还有一个特殊的eclipse工作区作用域。
- eclipse工作区
eclipse默认会编译项目时查询workspace中的maven项目,如果有符合的就不会去仓库里找了。 - 本地仓库
在我们开发机上h:/repo目录下的就是我们每个人的开发环境本地仓库 - 代理仓库
我们开发环境的nexus就是我们内部的代理仓库,可以进行我们内部项目的发布操作 - 中心仓库
默认随maven安装文件配置的仓库,由apache提供的中心仓库,包含大部分开源的项目依赖
maven在构建一个项目时,会按照由上至下的顺序查找pom.xml中配置的依赖,不存在的依赖会从下一级的仓库中拉取,只拉取一次。
maven基本命令
- mvn clean
只清理target编译输出目录 - mvn clean package
在构建前先进行clean清理,clean是可选操作,然后进行打包,打包结果在target目录中 - mvn clean install
在构建前清理,同上,在打包结束后,会把target目录中的结构安装到本地仓库中,这是package和install的区别。
profile定义
profile为不同环境的构建提供了支持,profile中的定义可以覆盖所有的基础定义,包括jar引用,构建插件等。
注意:因为profile的这种特性,所以在我们修改开发的配置时,也需要修改生产的对于文件。
配置profile的前提条件,需要在项目配置新建一个目录来放置生产所需的配置文件。
pom.xml
src
main
java #java源代码
resourcec #项目资源配置文件
webapp #web项目静态资源
conf #
prodction #生产配置文件
cron #生产定时配置文件
test
java #测试代码
resources #测试资源
|
war项目配置profile
<
profiles
>
<
profile
>
<!-- id属性用来区分多套profile配置 -->
<
id
>production</
id
>
<!-- 这里显式覆盖jar依赖 -->
<
dependencies
>
<
dependency
>
<
groupId
>com.lefu</
groupId
>
<
artifactId
>dsmclient</
artifactId
>
<
version
>1.0</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
plugins
>
<!-- 如果web.xml需要在不同环境配置,需要使用到此插件 -->
<
plugin
>
<
artifactId
>maven-war-plugin</
artifactId
>
<
version
>2.1.1</
version
>
<
configuration
>
<
webXml
>${project.build.directory}/temp/web.xml</
webXml
>
</
configuration
>
</
plugin
>
</
plugins
>
<
resources
>
<
resource
>
<!-- 默认扫描的配置文件路径 -->
<
directory
>src/main/resources</
directory
>
<!-- 是否开启属性过滤,只针对 properties 文件,因为变量的表达式与Spring解析的表达式是一样的 ${expression},在项目打包时会执行此过滤操作-->
<
filtering
>true</
filtering
>
<!-- 显式排除以下文件,就是打包时会忽略这些配置文件,对于的includes是显式包含配置文件 -->
<
excludes
>
<
exclude
>db.properties</
exclude
>
<
exclude
>essc.properties</
exclude
>
<
exclude
>system.properties</
exclude
>
<
exclude
>spring-context/spring-task.xml</
exclude
>
</
excludes
>
</
resource
>
<
resource
>
<!-- 资源文件可以设置多个目录,此目录就是生产需要的配置文件,这是基本约定 -->
<
directory
>src/main/conf/production</
directory
>
<
filtering
>true</
filtering
>
</
resource
>
<
resource
>
<
directory
>src/main/conf/WEB-INF</
directory
>
<
filtering
>true</
filtering
>
<
targetPath
>../temp</
targetPath
>
</
resource
>
</
resources
>
</
build
>
</
profile
>
<
profile
>
<!-- 定时的profile配置 -->
<
id
>production-cron</
id
>
<
dependencies
>
<
dependency
>
<
groupId
>com.lefu</
groupId
>
<
artifactId
>dsmclient</
artifactId
>
<
version
>1.0</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
plugins
>
<
plugin
>
<
artifactId
>maven-war-plugin</
artifactId
>
<
version
>2.1.1</
version
>
<
configuration
>
<
webXml
>${project.build.directory}/temp/web.xml</
webXml
>
</
configuration
>
</
plugin
>
</
plugins
>
<
resources
>
<
resource
>
<
directory
>src/main/resources</
directory
>
<
filtering
>true</
filtering
>
<
excludes
>
<
exclude
>db.properties</
exclude
>
<
exclude
>essc.properties</
exclude
>
<
exclude
>system.properties</
exclude
>
<
exclude
>springContext/spring-task.xml</
exclude
>
</
excludes
>
</
resource
>
<
resource
>
<
directory
>src/main/conf/production</
directory
>
<
filtering
>true</
filtering
>
</
resource
>
<
resource
>
<
directory
>src/main/conf/WEB-INF</
directory
>
<
filtering
>true</
filtering
>
<
targetPath
>../temp</
targetPath
>
</
resource
>
<
resource
>
<
directory
>src/main/conf/cron</
directory
>
<
filtering
>true</
filtering
>
</
resource
>
</
resources
>
</
build
>
</
profile
>
</
profiles
>
|
控制台(jar)项目配置
jar项目运行需要一些前提条件,项目结构也需要进行调整,如下:
pom.xml
release.xml #assembly打包需要的配置描述文件
bin #启动关闭相关shell脚本,已存在的项目可以拷贝生产目前运行的脚本文件
logs #日志输出目录,可以不创建,由启动脚本自动创建
src
main
java #java源代码
resources #项目资源文件,如果是需要在运行时更改的文件不能放在这里,因为jar打包时会默认打入jar包内部,不能修改
conf #本地开发需要的配置文件,打包后也可以修改
production #生产配置文件
conf #生产的配置文件,只有放置在这里才能在运行时修改配置
test
java
resources
|
项目pom.xml的修改,最关键的是引入了assembly插件来进行打包的动作
<
build
>
<
sourceDirectory
>src/main/java</
sourceDirectory
>
<
resources
>
<
resource
>
<
directory
>src/main/resources</
directory
>
</
resource
>
<
resource
>
<
directory
>src/main/conf</
directory
>
<
filtering
>true</
filtering
>
<
targetPath
>../conf</
targetPath
>
</
resource
>
</
resources
>
<
plugins
>
<
plugin
>
<
artifactId
>maven-source-plugin</
artifactId
>
<
version
>2.1</
version
>
<
configuration
>
<
attach
>true</
attach
>
</
configuration
>
<
executions
>
<
execution
>
<
phase
>compile</
phase
>
<
goals
>
<
goal
>jar</
goal
>
</
goals
>
</
execution
>
</
executions
>
</
plugin
>
<
plugin
>
<
artifactId
>maven-assembly-plugin</
artifactId
>
<
configuration
>
<
descriptors
>
<
descriptor
>release.xml</
descriptor
>
</
descriptors
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
<
profiles
>
<
profile
>
<
id
>production</
id
>
<
build
>
<
resources
>
<
resource
>
<
directory
>src/main/resources</
directory
>
</
resource
>
<
resource
>
<
directory
>src/main/conf</
directory
>
<
excludes
>
<
exclude
>db.properties</
exclude
>
<
exclude
>conf.properties</
exclude
>
</
excludes
>
<
targetPath
>../conf</
targetPath
>
</
resource
>
<
resource
>
<
directory
>src/main/production/conf</
directory
>
<
filtering
>true</
filtering
>
<
targetPath
>../conf</
targetPath
>
</
resource
>
</
resources
>
<
plugins
>
<
plugin
>
<
artifactId
>maven-source-plugin</
artifactId
>
<
version
>2.1</
version
>
<
configuration
>
<
attach
>true</
attach
>
</
configuration
>
<
executions
>
<
execution
>
<
phase
>compile</
phase
>
<
goals
>
<
goal
>jar</
goal
>
</
goals
>
</
execution
>
</
executions
>
</
plugin
>
<
plugin
>
<
artifactId
>maven-assembly-plugin</
artifactId
>
<
configuration
>
<
descriptors
>
<
descriptor
>release.xml</
descriptor
>
</
descriptors
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
</
profile
>
</
profiles
>
|
另外需要配置一个属性,在pom.xml中:
<
properties
>
<!-- 键不变,只是值改变即可,这个值是项目类型标识,用于Medusa平台进行识别的一个依据 -->
<
lefu.project.name
>SmsServer</
lefu.project.name
>
</
properties
>
|
release.xml打包配置描述文件:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
assembly
xmlns
=
"<a href="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi
=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 <a href="http://maven.apache.org/xsd/assembly-1.1.0.xsd" "="" style="color: rgb(50, 108, 166); text-decoration-line: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;">http://maven.apache.org/xsd/assembly-1.1.0.xsd"
>
<
id
>assembly</
id
>
<
formats
>
<
format
>zip</
format
>
</
formats
>
<
fileSets
>
<
fileSet
>
<
directory
>bin</
directory
>
<
outputDirectory
>bin</
outputDirectory
>
</
fileSet
>
<
fileSet
>
<
directory
>${project.build.directory}/conf</
directory
>
<
outputDirectory
>conf</
outputDirectory
>
</
fileSet
>
<
fileSet
>
<
directory
>logs</
directory
>
<
outputDirectory
>logs</
outputDirectory
>
</
fileSet
>
</
fileSets
>
<
dependencySets
>
<
dependencySet
>
<
useProjectArtifact
>true</
useProjectArtifact
>
<
outputDirectory
>lib</
outputDirectory
>
<
scope
>compile</
scope
>
</
dependencySet
>
</
dependencySets
>
</
assembly
>
|
最终输出结构为:在target目录下会生成 artifactId-version.zip包,解压缩的目录结构为
artifactId-version
bin
#程序运行相关脚本
conf
#程序运行相关配置文件
lib
#程序运行相关jar
logs
#日志文件目录
|
脚本调整记录
-
2016年3月1号
控制台项目结构调整,去除对python环境的依赖,以Maven的assembly插件进行打包 -
11月21(pom.xml和生产build.py调整)
原来的脚步会将production/conf下的配置文件打包到jar里面,而外面也会有一套,但是外面的配置文件是无效的。
在pom.xml的profile(Production)的resource标签下,增加<targetPath>../temp</targetPath>标签行。
在生产的脚步中修改 sourceconfdir = os.path.join(targetdir, 'temp')