Marven筆記貼

本篇只是做爲自學Marven的筆記貼,基本上都是網上的各類資料的彙總,方便本身和須要的人,不用一個個去找浪費時間了。html

 

  1. 什麼是Marven

You want to start with a project, say a J2ee or Spring MVC and you are lost what is the minimum set of Jars, how should I create the folder structure, Should it be a WAR, EAR, What are the 「Best Practices」 for a particular kind of project ..and many others.java

Maven can be described as 「Project Management Tool」 which helps in cleaning, building, dependency resolution, integration, running test cases among many other activities related to project lifecycle management.
All of these tasks can be carried out by good old ANT scripts as well, but it involves manual steps to write script for all of these activities. For example, listing all the dependencies , then downloading them and maintaining a repository, if a version changes manage it manually — Maven provides many of the task as 「off the shelf」 plugin.spring

 

  1. Marven快速入門

很是全的blogapache

http://tangyanbo.iteye.com/category/220107windows

兩個簡單的例子oracle

http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.htmlapp

http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html框架

 

  1. Marven文檔官網

https://maven.apache.org/index.htmlless

 

  1. 官方how-to

https://maven.apache.org/guides/getting-started/index.html#What_is_Maveneclipse

 

  1. 官方pom configuration guide

https://maven.apache.org/pom.html

 

 

  1. 核心概念

a)         Two Key Concepts

POM.xml: –This is heart of maven, all dependencies definition go here and it’s instrumental in controlling the lifecycle activities of the project.

Archetype: It could be understood as 「off the prototype」, for example for generating a simple Web project what should be the project structure, what are the required dependencies or for generating a spring MVC project what are the required details – Some one have already defined all these, just use the appropriate archetype and project setup will be done 「automagically」. Typical step

  • Use archetype to indicate what type of project to use
  • It will generate standard project folders
  • It will generate POM.xml

Use maven predefined command to download the dependencies. Nonetheless to say, one need to be connected to the net for getting the jars.
There are 「off the shelf」 archetypes defined, I will explain below how to get some of them in eclipse.

關於archetype的用法,在http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html的例子中有說到。

 

b)     How dependencies are resolved?

Heart of Maven is POM.xml file. All the dependencies are defined here.

<dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

 

First these dependencies are looked into the local repository, pointed by M2_REPO vaiable. To see the value of this variable go to windows->preferences->Java->BuildPath. If the needed JAR is not found in the local repository, then Maven site is contacted to download needed JAR.

 

c)      Local repository vs Remote repository

A repository in Maven is used to hold build artifacts and dependencies of varying types.

There are strictly only two types of repositories: local and remote. The local repository refers to a copy on your own installation that is a cache of the remote downloads, and also contains the temporary build artifacts that you have not yet released.

Remote repositories refer to any other type of repository, accessed by a variety of protocols such as file:// and http://. These repositories might be a truly remote repository set up by a third party to provide their artifacts for downloading (for example, repo.maven.apache.org anduk.maven.org house Maven's central repository). Other "remote" repositories may be internal repositories set up on a file or HTTP server within your company, used to share private artifacts between development teams and for releases.

The local and remote repositories are structured the same way so that scripts can easily be run on either side, or they can be synced for offline used. In general use, the layout of the repositories is completely transparent to the Maven user, however.

一般咱們是從center repository裏面去取出jar文件的。若是有必要咱們可使用就近的mirror,具體的作法在這裏http://maven.apache.org/guides/mini/guide-mirror-settings.html

 

可是對於直接的企業應用,一般來講會搭建一個internal repository。這個操做能夠經過repository manager來實現,好處有以下幾點。

 

Proxying speeds up builds throughout your organization by installing a local cache for all artifacts from the Central Maven repository.

 

a repository manager provides an organization with control over what is downloaded by Maven. You can include or exclude specific artifacts from the public repository, and having this level of control over what is downloaded from the central Maven repository is a prerequisite for organizations which need strict control over what dependencies are used throughout an organization. 

 

a repository manager also provides something essential to full adoption of Maven. Unless you expect every member of your organization to download and build every single internal project, you will want to provide a mechanism for developers and departments to share both SNAPSHOT and releases for internal project artifacts. Over time, this central deployment point for internal projects becomes the fabric for collaboration between different development teams.

 

d)     項目開發中的maven

根據個人瞭解,maven開發的過程基本上是這樣的:

  1. 使用maven命令行mvn工具(或者eclipse插件)建立maven項目的基本框架
  2. 使用svn將整個生成的框架都check in(特別要的是pom.xml)
  3. 其餘工程師從svn中check out以後,eclipse的maven插件會自動下載須要的jar包放置到local repository中。

 

 

e)     Maven 生命週期

maven 能支持不一樣的生命週期,可是最經常使用的是默認的Maven生命週期 (default Maven lifecycle )。若是你沒有對它進行任何的插件配置或者定製的話,那麼命令 mvn package 會依次執行默認生命週期中直到包括 package 階段前的全部階段的插件目標(其中冒號前的是插件名,後面的是target/goal(s)):

  1. process-resources 階段:resources:resources
  2. compile 階段:compiler:compile
  3. process-classes 階段:(默認無目標)
  4. process-test-resources 階段:resources:testResources
  5. test-compile 階段:compiler:testCompile
  6. test 階段:surefire:test
  7. prepare-package 階段:(默認無目標)
  8. package 階段:jar:jar

 

f)      自建jar加入maven repository

二者均可以在官方how-to裏面找到

  1. deploying jars to an external repository
  2. mvn install

http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-2-405568-zhs.html也有一個例子

   

g) maven中snapshot快照庫和release發佈庫的區別和做用

講到底就是一句話,

若是是snapshot版本,必定會去到remote repository的SNAPSHOT倉庫中去找指定公共庫的指定版本的最新SNAPSHOT。

若是是非snapshot版本(release版本),先到local repository裏面去找指定公共庫的指定版本,沒有的話,再去remote repository裏面找此庫

http://www.mzone.cc/article/277.html

 

 

 

參考資料:

http://tangyanbo.iteye.com/category/220107

http://www.mzone.cc/article/277.html

https://maven.apache.org/pom.html

http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html

http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-2-405568-zhs.html

https://maven.apache.org/guides/getting-started/index.html#How_do_I_deploy_my_jar_in_my_remote_repository

https://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project

https://maven.apache.org/guides/introduction/introduction-to-archetypes.html

http://chandanpandey.com/2012/11/01/maven-basic-concept-set-up-in-eclipse-and-first-project/

相關文章
相關標籤/搜索