maven

maven 安装:

1.下载maven的压缩文件:http://maven.apache.org/download.cgi

2.解压到想要安装的目录,可以是任意的位置

3.新建一个maven的repository的文件夹(可以是任意位置)【用来存放jar包的仓库】

4.修改maven软件中的setting.xml文件,添加<localRepository>标签

5.添加环境maven的位置到环境变量



maven和myeclipse结合



开始建立  maven工程




今天创建一个maven项目,

在命令行:

mvn  archetype:create -DgroupId=com.jt.xiaoyang -DartifactId=xiaoyang-app -DarchetypeArtifactId=maven-archetype-webapp 

直接报错:maven-archeype-plugin:2.3:create (default-cli) @ standalone-pom



解决办法:把create命令,换为generate

mvn archetype:generate -DgroupId=com.jt.xiaoyang -DartifactId=xiaoyang-app -DarchetypeArtifactId=maven-archetype-webapp 


总结,因为用的是maven3.3.3版本,所以命令做了调整吧!

以上是使用命令行操作,其实很少用,主要还是用eclipse来操作

========================maven的一些概念====================

maven有一套build的生命周期,是按照一套顺序走下来的,这一套顺序叫一个生命周期(lifecycle).

maven有 三种生命周期[一个生命周期分为多个build phase.]

1.default   2.clean  3.site 

default 生命周期build  phase

  • validatevalidate the project is correct and all necessary information is available.
  • initializeinitialize build state, e.g. set properties or create directories.
  • generate-sourcesgenerate any source code for inclusion in compilation.
  • process-sourcesprocess the source code, for example to filter any values.
  • generate-resourcesgenerate resources for inclusion in the package.
  • process-resourcescopy and process the resources into the destination directory, ready for packaging.
  • compilecompile the source code of the project.
  • process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
  • generate-test-sourcesgenerate any test source code for inclusion in compilation.
  • process-test-sourcesprocess the test source code, for example to filter any values.
  • generate-test-resourcescreate resources for testing.
  • process-test-resourcescopy and process the resources into the test destination directory.
  • test-compilecompile the test source code into the test destination directory
  • process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
  • testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
  • prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
  • packagetake the compiled code and package it in its distributable format, such as a JAR.
  • pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
  • integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
  • post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.
  • verifyrun any checks to verify the package is valid and meets quality criteria.
  • installinstall the package into the local repository, for use as a dependency in other projects locally.
  • deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
这些build phase是按照 顺序执行的, 如果执行后期的build phase,则前面的build phase也会被执行。

如 执行:mvn  deploy 

则前面的verify,  install......都会被执行


clean  生命周期build  phase;

【每一个phase是由goal【任务】组成,一个goal可以关联到一个build phase,也可以不关联到任何build phase,不关联到任何phase的goal可以独立执行,

如:mvn clean depedency:copy-dependencies package

上面的命令会导致先执行clean这个phase,然后拷贝依赖项,最后打包

这里clean这个goal是clean这个lifecycle里面的一个goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起执行的。

如果一个goal被绑定到多个phase上,那么goal会被执行多次。

phase的顺序是已经固定的,如果一个phase没有绑定任何goal,那么phase就不会被执行。

一个goal可以通过两种方式绑定到一个phase,一个是指定packaging,另一个是plugin

每一个build phase会绑定到goal。goal是由plugin提供的。 

每一种packaging的值都表明了一定的phase和goal之间的绑定关系。

site     生命周期build  phase:



packaging&plugin

plugin就是用来向maven提供goal的。

一个plugin里面可以有多个goal,这就是为什么我们在指明goal时,前面会用一个冒号与plugin的名字。

一个plugin自己可以指定自己的goal绑定到哪个lifecycle的哪一个Phase上,

另外也可以配置一个goal绑定到哪个phase上。可以在pom.xml里面配置。 看两个配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< plugin >
    < groupId >org.codehaus.modello</ groupId >
    < artifactId >modello-maven-plugin</ artifactId >
    < version >1.4</ version >
    < executions >
      < execution >
        < configuration >
          < models >
            < model >src/main/mdo/maven.mdo</ model >
          </ models >
          < version >4.0.0</ version >
        </ configuration >
        < goals >
          < goal >java</ goal >
        </ goals >
      </ execution >
    </ executions >
  </ plugin >

这个就在当前的lifecycle里面添加了一个名字叫java的goal,这goal会根据自己的配置去绑定到一个phase,在phase执行的时候这个goal会执行。并且在这个配置里面,可以指定多个execution来让这个goal执行多次。

看另一个示例配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
< plugin >
    < groupId >com.mycompany.example</ groupId >
    < artifactId >display-maven-plugin</ artifactId >
    < version >1.0</ version >
    < executions >
      < execution >
        < phase >process-test-resources</ phase >
        < goals >
          < goal >time</ goal >
        </ goals >
      </ execution >
    </ executions >
  </ plugin >

这个名为time的goal把自己绑定到了process-test-resource这个phase上


在默认情况下,并不是所有的phase都绑定了goal,比如clean这个lifecycle是有三个phase的,但是只有其中的一个名为clean的phase默认绑定了一个clean:clean goal,其它两个phase默认没有绑定任何goal。


之前已经提到过packaging

在pom.xml可以指定packaging,

每种packaging都设定了一组phase和goal之间的绑定关系。

在default lifecycle下,当packaging为 ejb/ejb3/jar/par/rar/war 其中之一的值的时候,只有以下的phase绑定了goal,具体如下:

process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
install install:install
deploy deploy:deploy



总结:

另外一个很重要的就是dependency,我们要在项目中引用一个依赖,只需要在pom.xml指定依赖的名字和版本,maven会自动去远程的repository下载,然后放到本地的repository里面,这样以后所有的project都可以共用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值