在maven中,所有的依赖都是通过坐标计算的,坐标体系有以下元素:
maven中通过指定前面3者就能唯一定位一个依赖。
maven中,所有的依赖和插件都放置在仓库内,仓库有几种类型,中心仓库,代理仓库,本地仓库,另外还有一个特殊的eclipse工作区作用域。
maven在构建一个项目时,会按照由上至下的顺序查找pom.xml中配置的依赖,不存在的依赖会从下一级的仓库中拉取,只拉取一次。
profile为不同环境的构建提供了支持,profile中的定义可以覆盖所有的基础定义,包括jar引用,构建插件等。
注意:因为profile的这种特性,所以在我们修改开发的配置时,也需要修改生产的对于文件。
配置profile的前提条件,需要在项目配置新建一个目录来放置生产所需的配置文件。
pom.xml
src
main
java #java源代码
resourcec #项目资源配置文件
webapp #web项目静态资源
conf #
prodction #生产配置文件
cron #生产定时配置文件
test
java #测试代码
resources #测试资源
|
<
profiles
>
<
profile
>
<
id
>production
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
>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
>
<
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项目运行需要一些前提条件,项目结构也需要进行调整,如下:
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
>
<
lefu.project.name
>SmsServer
lefu.project.name
>
properties
>
|
release.xml打包配置描述文件:
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
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
>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标签下,增加
在生产的脚步中修改 sourceconfdir = os.path.join(targetdir, 'temp')