encodeURI

Posted on

encodeURI-decodeURI与UrlEncode-UrlDecode

**[lzyox](http://lzyox.cublog.cn/) **

I'm willing to be an ox serving the country all my life.
encodeURI/decodeURI与UrlEncode/UrlDecode
摘要: 关于encodeURI,标准似乎是这么定义的:

"如果有空格就用%20代替,如果有其它字符就用%ASCII代替,如果有汉字等四个字节的字符,就用两个%ASCII来代替"

然而MS向来标新立异,继encodeURI之URL中文参数问题之后,encodeURI的噩梦继续袭来......

一、该死的空格

最近做两个页面的数据交换.由PageA发起Ajax请求到PageB,PageB从数据库读取数据返回给PageA,由于怕中间有特殊字符会导致js失败,所以使用了UrlEncode进行URI编码,再在客户端进行decodeURI解码.

结果发现空格无法被正确识别,UrlEncode将空格编码为+,而decodeURI只识别20%表示的空格。初步判断UrlEncode的编码格式和encodeURI不一致,为了验证这个看法,于是提取了键盘上的一些特殊符号进行编码比对:

字符: ~ ! @ /# $ % ^ & / ()_ + - =UrlEncode: %7e ! %40 %23 %24 %25 %5e %26 / () %2b - %3dencodeURI: ~ ! @ /# $ %25 %5E & /* () + - =字符: { } [ ] \ | ' ; : " / ? . , < >UrlEncode: %7b %7d %5b %5d %5c %7c ' %3b %3a %22 %2f %3f . %2c %3c %3eencodeURI: %7B %7D %5B %5D %5C %7C ' ; : %22 / ? . , %3C %3E

这些比较可以看出,两个的编码确实存在比较大区别上,特别是对于特殊字符的处理上面.

由此相当,在encodeURI之URL中文参数问题中自己所认为了,asp.net对form的action进行了2次编码的判断应该是错的,其实并没有进行2次编码,只是asp.net在接受到encodeURI编码的action之后,利用UrlDecode进行解码,然后再次用UrlEncode进行编码写入Html中,由于编码格式不一致,所以Postback之后的URI,js就无法使用decodeURI进行正确解码.

由此可以知道,如果你用encodeURI编码的字符串,是可以通过UrlDecode解码出来的,也就是说UrlDecode可以识别encodeURI(js)和UrlEncode(c/#)两个编码格式.可以想到,MS在设计这个类库的时候,已经考虑到了会接受到encodeURI的编码,按常理来想的话,既然考虑到了解码,自然会考虑到编码,也即UrlEncode应该提供可以编码成decodeURI可以解码的格式.可别的是,我一直无法找到这个方式.不知道是设计者给我们开的一个小玩笑,还是留下点瑕疵好让我们燃起编程的激情,不至于对千篇一律的Code工作感到厌倦,残念......

二、让SP来得更猛烈些吧

因为存在这个编码的不一致性,导致如果你的程序需要做比较多的Server-Client数据沟通的话,只能通过其他途径(json,xml等非URI),即使只是一个简单的字符串,你也需要增加许多额外的数据以满足你的格式.

一如MS的很多软件一样,SP满天飞,看来我也只好自己进行SP了.

分析下编码中差异,基本都集中在特殊字符的处理上,对于中文的处理貌似一致的(目前还没有测试出差异).于是定下了"利用encodeURI/decodeURI处理中文字符,其他的进行手工处理"的方案,修改了下之前的js代码:

KINN.Util.EncodeURI = function(unzipStr,isCusEncode){ if(isCusEncode){ var zipArray = new Array(); var zipstr = ""; var lens = new Array(); for(var i=0;i<unzipStr.length;i++){ var ac = unzipStr.charCodeAt(i); zipstr += ac; lens = lens.concat(ac.toString().length); } zipArray = zipArray.concat(zipstr); zipArray = zipArray.concat(lens.join("O")); return zipArray.join("N"); }else{ //return encodeURI(unzipStr); var zipstr=""; var strSpecial="!\"/#$%&'()/*+,/:;<=>?[]^`{|}~%"; var tt= ""; for(var i=0;i 0x7f){ zipstr+=encodeURI(unzipStr.substr(i,1)); }else{ if(chr==" ") zipstr+="+"; else if(strSpecial.indexOf(chr)!=-1) zipstr+="%"+c.toString(16); else zipstr+=chr; } } return zipstr; }}KINN.Util.DecodeURI = function(zipStr,isCusEncode){ if(isCusEncode){ var zipArray = zipStr.split("N"); var zipSrcStr = zipArray[0]; var zipLens; if(zipArray[1]){ zipLens = zipArray[1].split("O"); }else{ zipLens.length = 0; } var uzipStr = ""; for(var j=0;j0x7f){ uzipStr+=decodeURI("%"+asc.toString()+zipStr.substring(i+3,i+9).toString()); ; i+=8; }else{ uzipStr+=KINN.Util.AsciiToString(parseInt("0x"+asc)); i+=2; } }else{ uzipStr+= chr; } } return uzipStr; }}KINN.Util.StringToAscii = function(str){ return str.charCodeAt(0).toString(16);}KINN.Util.AsciiToString = function(asccode){ return String.fromCharCode(asccode);}

三、浪子语: 很奇怪,为什么Asp.net老是存在某些小毛病小问题,不知道是设计者忽略了,还是真的为了改善我们程序员苦闷的编程生活?

java里面的编码就是和encodeURI一样的.

或许标准就是用来打破,IE如此,ASP.NET依然如此...... 发表于: 2009-04-13,修改于: 2009-04-13 10:36

Maven实战(七)settings.xml相关配置

Posted on

Maven实战(七)settings.xml相关配置

一、简介

settings.xml对于maven来说相当于全局性的配置,用于所有的项目,当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使用settings.xml中的settings元素来确定这些配置。这包含了本地仓库位置,远程仓库服务器以及认证信息等。

settings.xml存在于两个地方:

1.安装的地方:$M2_HOME/conf/settings.xml

2.用户的目录:${user.home}/.m2/settings.xml

前者又被叫做全局配置,后者被称为用户配置。如果两者都存在,它们的内容将被合并,并且用户范围的配置优先。

平时配置时优先选择用户目录的settings.xml

下面是settings下的顶层元素的一个概览:

1

2 3

4 5

6 7

8 9

10 11

12 13

14 15<

settings

xmlns

=

"http://maven.apache.org/SETTINGS/1.0.0"

xmlns:xsi

=

"http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0

http://maven.apache.org/xsd/settings-1.0.0.xsd">

<

localRepository

/>

<

interactiveMode

/>

<

usePluginRegistry

/>

<

offline

/>

<

pluginGroups

/>

<

servers

/>

<

mirrors

/>

<

proxies

/>

<

profiles

/>

<

activeProfiles

/> </

settings

>

二、简单值

localRepository:这个值是构建系统的本地仓库的路径。默认的值是${user.home}/.m2/repository.如果一个系统想让所有登陆的用户都用同一个本地仓库的话,这个值是极其有用的。

interactiveMode:如果Maven要试图与用户交互来得到输入就设置为true,否则就设置为false,默认为true。

usePluginRegistry:如果Maven使用${user.home}/.m2/plugin-registry.xml来管理plugin的版本,就设置为true,默认为false。

offline:如果构建系统要在离线模式下工作,设置为true,默认为false。如果构建服务器因为网络故障或者安全问题不能与远程仓库相连,那么这个设置是非常有用的。

三、PluginGroups(插件组)

这个元素包含了一系列pluginGroup元素,每个又包含了一个groupId。当一个plugin被使用,而它的groupId没有被提供的时候,这个列表将被搜索。这个列表自动的包含了org.apache.maven.plugins和org.codehaus.mojo。

1

2 3

4 5

6 7

8 9

10 <

settings

xmlns

=

"http://maven.apache.org/SETTINGS/1.0.0"

xmlns:xsi

=

"http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0

http://maven.apache.org/xsd/settings-1.0.0.xsd">

...

<

pluginGroups

>

<

pluginGroup

org.mortbay.jetty</

pluginGroup

>

</

pluginGroups

>

...

</

settings

>

四、Servers(服务器)

  1. 定义jar包下载的Maven仓库

    1. 定义部署服务器

1

2 3

4 5

6 7

8 9

10 11

12 <

servers

>

<

server

>

<

id

tomcat</

id

>

<

username

bruce</

username

>

<

password

password</

password

>

</

server

>

<

server

>

<

id

shiyue</

id

>

<

username

admin</

username

>

<

password

password</

password

>

</

server

>

</

servers

>

tomcat: 部署服务器

shiyue: Mave私服

五、Mirrors(镜像)

指定仓库的地址,则默认从指定的镜像下载jar包及插件 1

2 3

4 5

6 7

8 9<

mirrors

>

<

mirror

>

<

id

mirrorId</

id

>

<

mirrorOf

/*</

mirrorOf

>

<

name

Human Readable Name for this Mirror.</

name

>

<

url

http://host:port/nexus-2.1.2/content/groups/public</

url

>

</

mirror

>

</

mirrors

>

六、Proxies(代理)

有时候你所在的公司基于安全因素考虑,要求你使用通过安全认证的代理访问因特网。这时就需要为Maven配置HTTP代理。

1

2 3

4 5

6 7

8 9

10 11

12 <

proxies

>

<

proxy

>

<

id

optional</

id

>

<

active

true</

active

>

<

protocol

http</

protocol

>

<

username

proxyuser</

username

>

<

password

proxypass</

password

>

<

host

proxy.host.net</

host

>

<

port

80</

port

>

<

nonProxyHosts

local.net|some.host.com</

nonProxyHosts

>

</

proxy

>

</

proxies

>

参考:http://maven.apache.org/settings.html

来源: [http://tangyanbo.iteye.com/blog/1971257](http://tangyanbo.iteye.com/blog/1971257)

Maven 构建生命周期

Posted on

Maven 构建生命周期

构建生命周期是什么?

构建生命周期阶段的目标是要执行的顺序是一个良好定义的序列。在此阶段的生命周期中的一个阶段。 作为一个例子,一个典型的Maven 构建生命周期是由下列顺序的阶段: PhaseHandles描述prepare-resourcesresource copyingResource copying can be customized in this phase.compilecompilationSource code compilation is done in this phase.packagepackagingThis phase creates the JAR / WAR package as mentioned in packaging in POM.xml.installinstallationThis phase installs the package in local / remote maven repository.

总是有可用于注册必须执行一个特定的阶段之前或之后的目标的前处理和后阶段。 当Maven开始建立一个项目,它通过定义序列的阶段步骤和执行注册的每个阶段的目标。 Maven的有以下三种标准的生命周期:

  • clean
  • default(or build)
  • site

目标代表一个特定的任务,这有助于项目的建设和管理。它可以被绑定到零个或多个生成阶段。一个没有绑定到任何构建阶段的目标的构建生命周期可以执行直接调用。 执行的顺序取决于目标和构建阶段被调用的顺序。例如,考虑下面的命令。清洁和包装参数的构建阶段,而依赖:复制的依赖关系是一个目标。 mvn clean dependency:copy-dependencies package

在这里,清洁的阶段,将首先执行,然后的依赖关系:复制依赖性的目标将被执行,并终于将执行包阶段。

清洁生命周期

当我们执行命令mvn clean命令后,Maven的调用清洁的生命周期由以下几个阶段组成的。

  • pre-clean
  • clean
  • post-clean

Maven 清洁目标(clean:clean)被绑定的清洁干净的生命周期阶段。clean:clean 目标删除删除build目录下的输出构建。因此,当mvn clean 命令执行时,Maven会删除编译目录。

提到清洁的生命周期在上述阶段的目标,我们可以自定义此行为。 在下面的示例中,我们将附加 maven-antrun-plugin:run目标的预清洁,清洁和清洁后的阶段。这将使我们能够调用的信息显示的清洁生命周期的各个阶段。 我们已经创建了一个pom.xml在 C:\ MVN\ 项目文件夹中。

4.0.0 com.companyname.projectgroup project 1.0 org.apache.maven.plugins maven-antrun-plugin 1.1 id.pre-clean pre-clean run pre-clean phase id.clean clean run clean phase id.post-clean post-clean run post-clean phase

现在,打开命令控制台,到该文件夹包含的pom.xml和执行以下mvncommand。

C:\MVN\project>mvn post-clean

Maven将开始处理和显示清洁生命周期的所有阶段

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0

[INFO] task-segment: [post-clean] [INFO] ------------------------------------------------------------------

[INFO] [antrun:run {execution: id.pre-clean}] [INFO] Executing tasks

 [echo] pre-clean phase

[INFO] Executed tasks

[INFO] [clean:clean {execution: default-clean}] [INFO] [antrun:run {execution: id.clean}]

[INFO] Executing tasks [echo] clean phase

[INFO] Executed tasks [INFO] [antrun:run {execution: id.post-clean}]

[INFO] Executing tasks [echo] post-clean phase

[INFO] Executed tasks [INFO] ------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------

[INFO] Total time: < 1 second [INFO] Finished at: Sat Jul 07 13:38:59 IST 2012

[INFO] Final Memory: 4M/44M [INFO] ------------------------------------------------------------------

你可以尝试调整mvn清洁的命令,该命令将显示前干净清洁,什么都不会被执行为清洁后阶段。

默认(或生成)的生命周期

这是主要的Maven的生命周期,用于构建应用程序。它有以下23个阶段。 Lifecycle Phase描述validateValidates whether project is correct and all necessary information is available to complete the build process.initializeInitializes build state, for example set propertiesgenerate-sourcesGenerate any source code to be included in compilation phase.process-sourcesProcess the source code, for example, filter any value.generate-resourcesGenerate resources to be included in the package.process-resourcesCopy and process the resources into the destination directory, ready for packaging phase.compileCompile the source code of the project.process-classesPost-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes.generate-test-sourcesGenerate any test source code to be included in compilation phase.process-test-sourcesProcess the test source code, for example, filter any values.test-compileCompile the test source code into the test destination directory.process-test-classesProcess the generated files from test code file compilation.testRun tests using a suitable unit testing framework(Junit is one).prepare-packagePerform any operations necessary to prepare a package before the actual packaging.packageTake the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file.pre-integration-testPerform actions required before integration tests are executed. For example, setting up the required environment.integration-testProcess and deploy the package if necessary into an environment where integration tests can be run.pre-integration-testPerform actions required after integration tests have been executed. For example, cleaning up the environment.verifyRun any check-ups to verify the package is valid and meets quality criterias.installInstall the package into the local repository, which can be used as a dependency in other projects locally.deployCopies the final package to the remote repository for sharing with other developers and projects.

There are few important concepts related to Maven Lifecycles which are wroth to mention:

  • When a phase is called via Maven command, for example mvn compile, only phases upto and including that phase will execute.
  • Different maven goals will be bound to different phases of Maven lifecycle depending upon the type of packaging (JAR / WAR / EAR).

在下面的示例中,我们将附加的Maven的antrun插件:运行目标构建生命周期的几个阶段。这将使我们能够回显的短信显示的生命周期的各个阶段。 我们已经更新了pom.xml文件在C:\MVN\项目文件夹中。

4.0.0 com.companyname.projectgroup project 1.0 org.apache.maven.plugins maven-antrun-plugin 1.1 id.validate validate run validate phase id.compile compile run compile phase id.test test run test phase id.package package run package phase id.deploy deploy run deploy phase

现在,打开命令控制台,进入该文件夹包含pom.xml和执行以下mvn命令。

C:\MVN\project>mvn compile

最多编译阶段,Maven将开始构建生命周期阶段的处理和显示。

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0

[INFO] task-segment: [compile] [INFO] ------------------------------------------------------------------

[INFO] [antrun:run {execution: id.validate}] [INFO] Executing tasks

 [echo] validate phase

[INFO] Executed tasks

[INFO] [resources:resources {execution: default-resources}] [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,

i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MVN\project\src\main\resources

[INFO] [compiler:compile {execution: default-compile}] [INFO] Nothing to compile - all classes are up to date

[INFO] [antrun:run {execution: id.compile}] [INFO] Executing tasks

 [echo] compile phase

[INFO] Executed tasks

[INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------ [INFO] Total time: 2 seconds

[INFO] Finished at: Sat Jul 07 20:18:25 IST 2012 [INFO] Final Memory: 7M/64M

[INFO] ------------------------------------------------------------------

网站的生命周期

Maven的网站插件通常用于创建新的文档,创建报告,部署网站等。 阶段

  • pre-site
  • site
  • post-site
  • site-deploy

在下面的示例中,我们将附加maven-antrun-plugin:run目标网站的生命周期的所有阶段。这将使我们能够呼应的短信显示的生命周期的各个阶段。 我们已经更新了pom.xml文件在C:\ MVN\项目文件夹中。

4.0.0 com.companyname.projectgroup project 1.0 org.apache.maven.plugins maven-antrun-plugin 1.1 id.pre-site pre-site run pre-site phase id.site site run site phase id.post-site post-site run post-site phase id.site-deploy site-deploy run site-deploy phase

现在,打开命令控制台,进入该文件夹包含pom.xml和执行以下mvn命令。

C:\MVN\project>mvn site

Maven将开始处理和显示最多的网站网站的生命周期阶段的阶段。

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0

[INFO] task-segment: [site] [INFO] ------------------------------------------------------------------

[INFO] [antrun:run {execution: id.pre-site}] [INFO] Executing tasks

 [echo] pre-site phase

[INFO] Executed tasks

[INFO] [site:site {execution: default-site}] [INFO] Generating "About" report.

[INFO] Generating "Issue Tracking" report. [INFO] Generating "Project Team" report.

[INFO] Generating "Dependencies" report. [INFO] Generating "Project Plugins" report.

[INFO] Generating "Continuous Integration" report. [INFO] Generating "Source Repository" report.

[INFO] Generating "Project License" report. [INFO] Generating "Mailing Lists" report.

[INFO] Generating "Plugin Management" report. [INFO] Generating "Project Summary" report.

[INFO] [antrun:run {execution: id.site}] [INFO] Executing tasks

 [echo] site phase

[INFO] Executed tasks

[INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------ [INFO] Total time: 3 seconds

[INFO] Finished at: Sat Jul 07 15:25:10 IST 2012 [INFO] Final Memory: 24M/149M

[INFO] - 来源: [http://www.yiibai.com/maven/maven_build_life_cycle.html](http://www.yiibai.com/maven/maven_build_life_cycle.html)

maven用途、核心概念、用法、常用参数和命令、扩展

Posted on

maven用途、核心概念、用法、常用参数和命令、扩展

本文由浅入深,主要介绍maven的用途核心概念(Pom、Repositories、Artifact、Build Lifecycle、Goal)介绍、用法(**Archetype意义及**创建各种项目)maven**常用参数和命令以及简单故障排除、maven扩展(eclipse、cobertura、findbugs、插件开发)、maven配置**。

本文较长,可根据个人需要有选择性的查看,比如先看用法再回过头来看核心概念

1、maven的用途

maven是一个项目构建和管理的工具,提供了帮助管理 构建文档报告依赖scms发布分发的方法。可以方便的编译代码、进行依赖管理、管理二进制库等等。

maven的好处在于可以将项目过程规范化、自动化、高效化以及强大的可扩展性

利用maven自身及其插件还可以获得代码检查报告、单元测试覆盖率、实现持续集成等等。

2、maven的核心概念介绍

2.1 Pom

pom是指project object Model。pom是一个xml,在maven2里为pom.xml。是maven工作的基础,在执行task或者goal时,maven会去项目根目录下读取pom.xml获得需要的配置信息

pom文件中包含了项目的信息和maven build项目所需的配置信息,通常有项目信息(如版本、成员)、项目的依赖、插件和goal、build选项等等

pom是可以继承的,通常对于一个大型的项目或是多个module的情况,子模块的pom需要指定父模块的pom

pom文件中节点为 Java代码 复制代码 收藏代码

  1. project pom文件的顶级元素
  2. modelVersion 所使用的object model版本,为了确保稳定的使用,这个元素是强制性的。除非maven开发者升级模板,否则不需要修改
  3. groupId 是项目创建团体或组织的唯一标志符,通常是域名倒写,如groupId org.apache.maven.plugins就是为所有maven插件预留的
  4. artifactId 是项目artifact唯一的基地址名
  5. packaging artifact打包的方式,如jar、war、ear等等。默认为jar。这个不仅表示项目最终产生何种后缀的文件,也表示build过程使用什么样的lifecycle
  6. version artifact的版本,通常能看见为类似0.0.1-SNAPSHOT,其中SNAPSHOT表示项目开发中,为开发版本
  7. name 表示项目的展现名,在maven生成的文档中使用
  8. url表示项目的地址,在maven生成的文档中使用
  9. description 表示项目的描述,在maven生成的文档中使用
  10. dependencies 表示依赖,在子节点dependencies中添加具体依赖的groupId artifactId和version
  11. build 表示build配置
  12. parent 表示父pom

project pom文件的顶级元素

modelVersion 所使用的object model版本,为了确保稳定的使用,这个元素是强制性的。除非maven开发者升级模板,否则不需要修改 groupId 是项目创建团体或组织的唯一标志符,通常是域名倒写,如groupId org.apache.maven.plugins就是为所有maven插件预留的

artifactId 是项目artifact唯一的基地址名 packaging artifact打包的方式,如jar、war、ear等等。默认为jar。这个不仅表示项目最终产生何种后缀的文件,也表示build过程使用什么样的lifecycle

。 version artifact的版本,通常能看见为类似0.0.1-SNAPSHOT,其中SNAPSHOT表示项目开发中,为开发版本

name 表示项目的展现名,在maven生成的文档中使用 url表示项目的地址,在maven生成的文档中使用

description 表示项目的描述,在maven生成的文档中使用 dependencies 表示依赖,在子节点dependencies中添加具体依赖的groupId artifactId和version

build 表示build配置 parent 表示父pom

其中groupId:artifactId:version唯一确定了一个artifact

2.2 Artifact

这个有点不好解释,大致说就是一个项目将要产生的文件,可以是jar文件,源文件,二进制文件,war文件,甚至是pom文件。每个artifact都由groupId:artifactId:version组成的标识符唯一识别。需要被使用(依赖)的artifact都要放在仓库(见Repository)中

2.3 Repositories

Repositories是用来存储Artifact的。如果说我们的项目产生的Artifact是一个个小工具,那么Repositories就是一个仓库,里面有我们自己创建的工具,也可以储存别人造的工具,我们在项目中需要使用某种工具时,在pom中声明dependency,编译代码时就会根据dependency去下载工具(Artifact,供自己使用。

对于自己的项目完成后可以通过mvn install命令将项目放到仓库(Repositories)中

仓库分为本地仓库和远程仓库,远程仓库是指远程服务器上用于存储Artifact的仓库,本地仓库是指本机存储Artifact的仓库,对于windows机器本地仓库地址为系统用户的.m2/repository下面。

对于需要的依赖,在pom中添加dependency即可,可以在maven的仓库中搜索:http://mvnrepository.com/

2.4 Build Lifecycle

是指一个项目build的过程。maven的Build Lifecycle分为三种,分别为default(处理项目的部署)、clean(处理项目的清理)、site(处理项目的文档生成)。他们都包含不同的lifecycle

Build Lifecycle是由phases构成的,下面重点介绍default Build Lifecycle几个重要的phase Xml代码 复制代码 收藏代码

  1. validate 验证项目是否正确以及必须的信息是否可用
  2. compile 编译源代码
  3. test 测试编译后的代码,即执行单元测试代码
  4. package 打包编译后的代码,在target目录下生成package文件
  5. integration-test 处理package以便需要时可以部署到集成测试环境
  6. verify 检验package是否有效并且达到质量标准
  7. install 安装package到本地仓库,方便本地其它项目使用
  8. deploy 部署,拷贝最终的package到远程仓库和替他开发这或项目共享,在集成或发布环境完成

validate 验证项目是否正确以及必须的信息是否可用

compile 编译源代码 test 测试编译后的代码,即执行单元测试代码

package 打包编译后的代码,在target目录下生成package文件 integration-test 处理package以便需要时可以部署到集成测试环境

verify 检验package是否有效并且达到质量标准 install 安装package到本地仓库,方便本地其它项目使用

deploy 部署,拷贝最终的package到远程仓库和替他开发这或项目共享,在集成或发布环境完成

以上的phase是有序的(注意实际两个相邻phase之间还有其他phase被省略,完整phase见lifecycle),下面一个phase的执行必须在上一个phase完成后

若直接以某一个phase为goal,将先执行完它之前的phase,如mvn install

将会先validate、compile、test、package、integration-test、verify最后再执行install phase

2.5 Goal

goal代表一个特定任务 Xml代码 复制代码 收藏代码

  1. A goal represents a specific task (finer than a build phase) which contributes to the building and managing of a project.

A goal represents a specific task (finer than a build phase) which contributes to the building and managing of a project.

mvn package表示打包的任务,通过上面的介绍我们知道,这个任务的执行会先执行package phase之前的phase

mvn deploy表示部署的任务

mven clean install则表示先执行clean的phase(包含其他子phase),再执行install的phase。

3、maven用法

主要讲下Archetype以及几种常用项目的创建

maven创建项目是根据Archetype(原型)创建的。下面先介绍下Archetype

3.1 Archetype

原型对于项目的作用就相当于模具对于工具的作用,我们想做一个锤子,将铁水倒入模具成型后,稍加修改就可以了。

类似我们可以根据项目类型的需要使用不同的Archetype创建项目。通过Archetype我们可以快速标准的创建项目。利用Archetype创建完项目后都有标准的文件夹目录结构

既然Archetype相当于模具,那么当然可以自己再造模具了啊,创建Archetype

下面介绍利用maven自带的集中Archetype创建项目。创建项目的goal为mvn archetype:generate,并且指定archetypeArtifactId,其中archetypeArtifactId见maven自带的archetypeArtifactId

3.2 quick start工程

创建一个简单的quick start项目,指定 -DarchetypeArtifactId为maven-archetype-quickstart,如下命令 Xml代码 复制代码 收藏代码

  1. mvn archetype:generate -DgroupId=com.trinea.maven.test -DartifactId=maven-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

mvn archetype:generate -DgroupId=com.trinea.maven.test -DartifactId=maven-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

其中DgroupId指定groupId,DartifactId指定artifactId,DarchetypeArtifactId指定ArchetypeId,

DinteractiveMode表示是否使用交互模式,交互模式会让用户填写版本信息之类的,非交互模式采用默认值

这样我们便建好了一个简单的maven项目,目录结构如下:

现在我们可以利用2.4的build Lifecycle进行一些操作,先命令行到工程根目录下

编译 mvn compile

打包 mvn package,此时target目录下会出现maven-quickstart-1.0-SNAPSHOT.jar文件,即为打包后文件

打包并安装到本地仓库mvn install,此时本机仓库会新增maven-quickstart-1.0-SNAPSHOT.jar文件。

3.3 web工程

创建一个简单的web项目,只需要修-DarchetypeArtifactId为maven-archetype-webapp即可,如下命令 Java代码 复制代码 收藏代码

  1. mvn archetype:generate -DgroupId=com.trinea.maven.web.test -DartifactId=maven-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

mvn archetype:generate -DgroupId=com.trinea.maven.web.test -DartifactId=maven-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

其他:

src\main\resources文件夹是用来存放资源文件的,maven工程默认没有resources文件夹,如果我们需要用到类似log4j.properties这样的配置文件,就需要在src\main文件夹下新建resources文件夹,并将log4j.properties放入其中。

test需要用到资源文件,类似放到src\test下

对于apache的log4j没有log4j.properties文件或是目录错误,会报如下异常 Xml代码 复制代码 收藏代码

  1. log4j:WARN No appenders could be found for logger (org.apache.commons.httpclient.HttpClient).
  2. log4j:WARN Please initialize the log4j system properly.

log4j:WARN No appenders could be found for logger (org.apache.commons.httpclient.HttpClient).

log4j:WARN Please initialize the log4j system properly.

4、maven常用参数和命令

主要介绍maven常用参数和命令以及简单故障排除

4.1 mvn常用参数

mvn -e 显示详细错误

mvn -U 强制更新snapshot类型的插件或依赖库(否则maven一天只会更新一次snapshot依赖)

mvn -o 运行offline模式,不联网更新依赖

mvn -N仅在当前项目模块执行命令,关闭reactor

mvn -pl module_name在指定模块上执行命令

mvn -ff 在递归执行命令过程中,一旦发生错误就直接退出

mvn -Dxxx=yyy指定java全局属性

mvn -Pxxx引用profile xxx

4.2 首先是2.4 Build L**ifecycle中介绍的命令**

mvn test-compile 编译测试代码

mvn test 运行程序中的单元测试

mvn compile 编译项目

mvn package 打包,此时target目录下会出现maven-quickstart-1.0-SNAPSHOT.jar文件,即为打包后文件

mvn install 打包并安装到本地仓库,此时本机仓库会新增maven-quickstart-1.0-SNAPSHOT.jar文件。

每个phase都可以作为goal,也可以联合,如之前介绍的mvn clean install

4.3 maven 日用三板斧

mvn archetype:generate 创建maven项目

mvn package 打包,上面已经介绍过了

mvn package -Prelease打包,并生成部署用的包,比如deploy//*.tgz

mvn install 打包并安装到本地库

mvn eclipse:eclipse 生成eclipse项目文件

mvn eclipse:clean 清除eclipse项目文件

mvn site 生成项目相关信息的网站

4.4 maven插件常用参数

mvn -Dwtpversion=2.0 指定maven版本

mvn -Dmaven.test.skip=true 如果命令包含了test phase,则忽略单元测试

mvn -DuserProp=filePath 指定用户自定义配置文件位置

mvn -DdownloadSources=true -Declipse.addVersionToProjectName=true eclipse:eclipse 生成eclipse项目文件,尝试从仓库下载源代码,并且生成的项目包含模块版本(注意如果使用公用POM,上述的开关缺省已打开)

4.5 maven简单故障排除

mvn -Dsurefire.useFile=false如果执行单元测试出错,用该命令可以在console输出失败的单元测试及相关信息

set MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=256m 调大jvm内存和持久代,maven/jvm out of memory error

mvn -X maven log level设定为debug在运行

mvndebug 运行jpda允许remote debug

mvn --help 这个就不说了。。

5、maven扩展

maven常用插件配置和使用

6、maven配置

为了修改maven创建项目默认以来的jdk版本,看了下maven配置,可参考http://www.iteye.com/topic/41612

maven2.0默认使用jdk1.5导致反省、@override 等annotation不可用。可用两种方法修改jdk版本

第一种:修改项目的pom.xml,影响单个项目,治标不治本 Java代码 复制代码 收藏代码

  1. org.apache.maven.plugins
  2. maven-compiler-plugin
  3. 1.6
  4. 1.6
  5. UTF-8
  6. org.apache.maven.plugins maven-compiler-plugin 1.6 1.6 UTF-8

pom中增加build配置,指定java版本为1.6

第二种:修改maven配置,影响maven建立的所有项目

到maven安装目录的conf文件夹下,修改settings.xml文件,如下: Java代码 复制代码 收藏代码

  1. jdk-1.6
  2. true
  3. 1.6
  4. 1.6
  5. 1.6
  6. 1.6


jdk-1.6

true
1.6


1.6
1.6
1.6


这样便能对所有默认的maven项目指定jdk为1.6

到此为止,休息会儿

参考资料:

Maven官方文档:http://maven.apache.org/guides/index.html

Maven权威指南:http://www.sonatype.com/books/maven-book/reference_zh/public-book.html

maven安装:http://maven.apache.org/download.html

http://www.infoq.com/cn/search.action?queryString=maven%E5%AE%9E%E6%88%98&searchOrder=relevance&search=maven%E5%AE%9E%E6%88%98

你可能还感兴趣:

Android公共库(缓存 下拉ListView 下载管理Pro 静默安装 root运行 Java公共类)

Android ImageCache图片缓存

Android系统下载管理DownloadManager功能介绍及使用示例

下拉刷新及滚动到底部加载更多的Listview使用

viewpager实现画廊(一屏多个Fragment)效果

Android APK root权限静默安装

写在前面, 自己搭博客why and target

maven 配置篇 之pom.xml

Posted on

maven 配置篇 之pom.xml

maven 配置篇 之pom.xml(一)

说完了settings.xml配置,下来说一下maven2的主要配置pom.xml

什么是pom? pom作为项目对象模型。通过xml表示maven项目,使用pom.xml来实现。主要描述了项目:包括配置文件;开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素。 快速察看: xml 代码

  1. 4.0.0modelVersion>
  2. ...groupId>
  3. ...artifactId>
  4. ...version>
  5. ...packaging>
  6. ...dependencies>
  7. ...parent>
  8. ...dependencyManagement>
  9. ...modules>
  10. ...properties>
  11. ...build>
  12. ...reporting>
  13. ...name>
  14. ...description>
  15. ...url>
  16. ...inceptionYear>
  17. ...licenses>
  18. ...organization>
  19. ...developers>
  20. ...contributors>
  21. ...issueManagement>
  22. ...ciManagement>
  23. ...mailingLists>
  24. ...scm>
  25. ...prerequisites>
  26. ...repositories>
  27. ...pluginRepositories>
  28. ...distributionManagement>
  29. ...profiles>
  30. project>
    基本内容: POM包括了所有的项目信息。 maven 相关: pom定义了最小的maven2元素,允许groupId,artifactId,version。所有需要的元素
  • groupId:项目或者组织的唯一标志,并且配置时生成的路径也是由此生成,如org.codehaus.mojo生成的相对路径为:/org/codehaus/mojo
  • artifactId: 项目的通用名称
  • version:项目的版本
  • packaging: 打包的机制,如pom, jar, maven-plugin, ejb, war, ear, rar, par
  • classifier: 分类 POM关系: 主要为依赖,继承,合成 依赖关系:

xml 代码

  1. junitgroupId>
  2. junitartifactId>
  3. 4.0version>
  4. jartype>
  5. testscope>
  6. trueoptional>
  7. dependency>
  8. ...
  9. dependencies>
  • groupId, artifactId, version:描述了依赖的项目唯一标志 可以通过以下方式进行安装:

  • 使用以下的命令安装:

  • mvn install:install-file –Dfile=non-maven-proj.jar –DgroupId=some.group –DartifactId=non-maven-proj –Dversion=1
  • 创建自己的库,并配置,使用deploy:deploy-file
  • 设置此依赖范围为system,定义一个系统路径。不提倡。

  • type:相应的依赖产品包形式,如jar,war

  • scope:用于限制相应的依赖范围,包括以下的几种变量:
  • compile :默认范围,用于编译
  • provided:类似于编译,但支持你期待jdk或者容器提供,类似于classpath
  • runtime:在执行时,需要使用
  • test:用于test任务时使用
  • system:需要外在提供相应得元素。通过systemPath来取得

  • systemPath: 仅用于范围为system。提供相应的路径

  • optional: 标注可选,当项目自身也是依赖时。用于连续依赖时使用 独占性
    外在告诉maven你只包括指定的项目,不包括相关的依赖。此因素主要用于解决版本冲突问题

xml 代码

  1. org.apache.mavengroupId>
  2. maven-embedderartifactId>
  3. 2.0version>
  4. org.apache.mavengroupId>
  5. maven-coreartifactId>
  6. exclusion>
  7. exclusions>
  8. dependency>
    表示项目maven-embedder需要项目maven-core,但我们不想引用maven-core 继承关系 另一个强大的变化,maven带来的是项目继承。主要的设置: 定义父项目 xml 代码
  1. 4.0.0modelVersion>
  2. org.codehaus.mojogroupId>
  3. my-parentartifactId>
  4. 2.0version>
  5. pompackaging>
  6. project>
    packaging 类型,需要pom用于parent和合成多个项目。我们需要增加相应的值给父pom,用于子项目继承。主要的元素如下:
  • 依赖型
  • 开发者和合作者
  • 插件列表
  • 报表列表
  • 插件执行使用相应的匹配ids
  • 插件配置
  • 子项目配置 xml 代码
  1. 4.0.0modelVersion>
  2. org.codehaus.mojogroupId>
  3. my-parentartifactId>
  4. 2.0version>
  5. ../my-parentrelativePath>
  6. parent>
  7. my-projectartifactId>
  8. project>
    relativePath可以不需要,但是用于指明parent的目录,用于快速查询。 dependencyManagement: 用于父项目配置共同的依赖关系,主要配置依赖包相同因素,如版本,scope。 合成(或者多个模块) 一个项目有多个模块,也叫做多重模块,或者合成项目。 如下的定义: xml 代码
  1. 4.0.0modelVersion>
  2. org.codehaus.mojogroupId>
  3. my-parentartifactId>
  4. 2.0version>
  5. my-project1
  6. my-project2
  7. modules>
  8. project>
    build 设置 主要用于编译设置,包括两个主要的元素,build和report build 主要分为两部分,基本元素和扩展元素集合 注意:包括项目build和profile build xml 代码
  1. ...build>
  2. ...build>
  3. profile>
  4. profiles>
  5. project>
    基本元素 xml 代码
  1. installdefaultGoal>
  2. ${basedir}/targetdirectory>
  3. ${artifactId}-${version}finalName>
  4. filters/filter1.propertiesfilter>
  5. filters>
  6. ...
  7. build>
  • defaultGoal: 定义默认的目标或者阶段。如install
  • directory: 编译输出的目录
  • finalName: 生成最后的文件的样式
  • filter: 定义过滤,用于替换相应的属性文件,使用maven定义的属性。设置所有placehold的值 资源(resources) 你项目中需要指定的资源。如spring配置文件,log4j.properties xml 代码
  1. ...
  2. META-INF/plexustargetPath>
  3. falsefiltering>
  4. ${basedir}/src/main/plexusdirectory>
  5. configuration.xmlinclude>
  6. includes>
  7. ////*.propertiesexclude>
  8. excludes>
  9. resource>
  10. resources>
  11. ...
  12. testResources>
  13. ...
  14. build>
  15. project>
  • resources: resource的列表,用于包括所有的资源
  • targetPath: 指定目标路径,用于放置资源,用于build
  • filtering: 是否替换资源中的属性placehold
  • directory: 资源所在的位置
  • includes: 样式,包括那些资源
  • excludes: 排除的资源
  • testResources: 测试资源列表 插件 在build时,执行的插件,比较有用的部分,如使用jdk 5.0编译等等 xml 代码
  1. ...
  2. org.apache.maven.pluginsgroupId>
  3. maven-jar-pluginartifactId>
  4. 2.0version>
  5. falseextensions>
  6. trueinherited>
  7. testclassifier>
  8. configuration>
  9. ...dependencies>
  10. ...executions>
  11. plugin>
  12. plugins>
  13. build>
  14. project>
  • extensions: true or false,是否装载插件扩展。默认false
  • inherited: true or false,是否此插件配置将会应用于poms,那些继承于此的项目
  • configuration: 指定插件配置
  • dependencies: 插件需要依赖的包
  • executions: 用于配置execution目标,一个插件可以有多个目标。 如下:

xml 代码

  1. maven-antrun-pluginartifactId>
  2. echodirid>
  3. rungoal>
  4. goals>
  5. verifyphase>
  6. falseinherited>
  7. Build Dir: ${project.build.directory}echo>
  8. tasks>
  9. configuration>
  10. execution>
  11. executions>
  12. plugin>
    说明:
  • id:规定execution 的唯一标志
  • goals: 表示目标
  • phase: 表示阶段,目标将会在什么阶段执行
  • inherited: 和上面的元素一样,设置false maven将会拒绝执行继承给子插件
  • configuration: 表示此执行的配置属性 插件管理 pluginManagement:插件管理以同样的方式包括插件元素,用于在特定的项目中配置。所有继承于此项目的子项目都能使用。主要定义插件的共同元素 扩展元素集合 主要包括以下的元素: Directories 用于设置各种目录结构,如下:

xml 代码

  1. ${basedir}/src/main/javasourceDirectory>
  2. ${basedir}/src/main/scriptsscriptSourceDirectory>
  3. ${basedir}/src/test/javatestSourceDirectory>
  4. ${basedir}/target/classesoutputDirectory>
  5. ${basedir}/target/test-classestestOutputDirectory>
  6. ...
  7. build>
    Extensions 表示需要扩展的插件,必须包括进相应的build路径。 xml 代码
  1. ...
  2. org.apache.maven.wagongroupId>
  3. wagon-ftpartifactId>
  4. 1.0-alpha-3version>
  5. extension>
  6. extensions>
  7. ...
  8. build>
  9. project>
    Reporting 用于在site阶段输出报表。特定的maven 插件能输出相应的定制和配置报表。

xml 代码

  1. ${basedir}/target/siteoutputDirectory>
  2. maven-project-info-reports-pluginartifactId>
  3. reportSet>
  4. reportSets>
  5. plugin>
  6. plugins>
  7. reporting>
    Report Sets 用于配置不同的目标,应用于不同的报表 xml 代码
  1. ...
  2. sunlinkid>
  3. javadocreport>
  4. reports>
  5. trueinherited>
  6. http://java.sun.com/j2se/1.5.0/docs/api/link>
  7. links>
  8. configuration>
  9. reportSet>
  10. reportSets>
  11. plugin>
  12. plugins>
  13. reporting>

maven 配置篇 之pom.xml(二) 更多的项目信息

  • name:项目除了artifactId外,可以定义多个名称
  • description: 项目描述
  • url: 项目url
  • inceptionYear:创始年份 Licenses xml 代码
  1. Apache 2name>
  2. http://www.apache.org/licenses/LICENSE-2.0.txturl>
  3. repodistribution>
  4. A business-friendly OSS licensecomments>
  5. license>
  6. licenses> Organization 配置组织信息

xml 代码

  1. Codehaus Mojoname>
  2. http://mojo.codehaus.orgurl>
  3. organization>
    Developers 配置开发者信息 xml 代码
  1. ericid>
  2. Ericname>
  3. eredmond@codehaus.orgemail>
  4. http://eric.propellors.neturl>
  5. Codehausorganization>
  6. http://mojo.codehaus.orgorganizationUrl>
  7. architectrole>
  8. developerrole>
  9. roles>
  10. -6timezone>
  11. http://tinyurl.com/prv4tpicUrl>
  12. properties>
  13. developer>
  14. developers>
    Contributors

xml 代码

  1. Noellename>
  2. some.name@gmail.comemail>
  3. http://noellemarie.comurl>
  4. Noelle Marieorganization>
  5. http://noellemarie.comorganizationUrl>
  6. testerrole>
  7. roles>
  8. -5timezone>
  9. some.name@gmail.comgtalk>
  10. properties>
  11. contributor>
  12. contributors>
    环境设置 Issue Management 定义相关的bug跟踪系统,如bugzilla,testtrack,clearQuest等

xml 代码

  1. Bugzillasystem>
  2. http://127.0.0.1/bugzillaurl>
  3. issueManagement>
    Continuous Integration Management 连续整合管理,基于triggers或者timings

xml 代码

  1. continuumsystem>
  2. http://127.0.0.1:8080/continuumurl>
  3. mailtype>
  4. truesendOnError>
  5. truesendOnFailure>
  6. falsesendOnSuccess>
  7. falsesendOnWarning>
  8. continuum@127.0.0.1address>configuration>
  9. notifier>
  10. notifiers>
  11. ciManagement>
    Mailing Lists

xml 代码

  1. User Listname>
  2. user-subscribe@127.0.0.1subscribe>
  3. user-unsubscribe@127.0.0.1unsubscribe>
  4. user@127.0.0.1post>
  5. http://127.0.0.1/user/archive>
  6. http://base.google.com/base/1/127.0.0.1otherArchive>
  7. otherArchives>
  8. mailingList>
  9. mailingLists>
    SCM 软件配置管理,如cvs 和svn

xml 代码

  1. scm:svn:http://127.0.0.1/svn/my-projectconnection>
  2. scm:svn:https://127.0.0.1/svn/my-projectdeveloperConnection>
  3. HEADtag>
  4. http://127.0.0.1/websvn/my-projecturl>
  5. scm>
    Repositories 配置同setting.xml中的开发库 Plugin Repositories 配置同 repositories Distribution Management 用于配置分发管理,配置相应的产品发布信息,主要用于发布,在执行mvn deploy后表示要发布的位置 1 配置到文件系统 xml 代码
  1. proficio-repositoryid>
  2. Proficio Repositoryname>
  3. file://${basedir}/target/deployurl>
  4. repository>
  5. distributionManagement>
    2 使用ssh2配置 xml 代码
  1. proficio-repositoryid>
  2. Proficio Repositoryname>
  3. scp://sshserver.yourcompany.com/deployurl>
  4. repository>
  5. distributionManagement>
    3 使用sftp配置 xml 代码
  1. proficio-repositoryid>
  2. Proficio Repositoryname>
  3. sftp://ftpserver.yourcompany.com/deployurl>
  4. repository>
  5. distributionManagement>
    4 使用外在的ssh配置 编译扩展用于指定使用wagon外在ssh提供,用于提供你的文件到相应的远程服务器。 xml 代码
  1. proficio-repositoryid>
  2. Proficio Repositoryname>
  3. scpexe://sshserver.yourcompany.com/deployurl>
  4. repository>
  5. distributionManagement>
  6. org.apache.maven.wagongroupId>
  7. wagon-ssh-externalartifactId>
  8. 1.0-alpha-6version>
  9. extension>
  10. extensions>
  11. build>
    5 使用ftp配置 xml 代码
  1. proficio-repositoryid>
  2. Proficio Repositoryname>
  3. ftp://ftpserver.yourcompany.com/deployurl>
  4. repository>
  5. distributionManagement>
  6. org.apache.maven.wagongroupId>
  7. wagon-ftpartifactId>
  8. 1.0-alpha-6version>
  9. extension>
  10. extensions>
  11. build>
    repository 对应于你的开发库,用户信息通过settings.xml中的server取得 Profiles 类似于settings.xml中的profiles,增加了几个元素,如下的样式:

xml 代码

  1. testid>
  2. ...activation>
  3. ...build>
  4. ...modules>
  5. ...repositories>
  6. ...pluginRepositories>
  7. ...dependencies>
  8. ...reporting>
  9. ...dependencyManagement>
  10. ...distributionManagement>
  11. profile>
  12. profiles>