积少成多

垃圾堆

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  29 Posts :: 1 Stories :: 5 Comments :: 0 Trackbacks
Maven的功能
  • 遵循最佳实践,简单的建立项目-在分秒之间建立一个新的项目或者模块。
  • 在多个项目之间统一使用。
  • 依附环境的管理,包括自动的升级。
  • 方便地在同一时间参加多个项目。
  • 大且不断增长的库和元数据可供食用。
  • 可扩展,用java或脚本语言开发插件
  • 加入新的功能只需要很少或不需要额外的配置
  • Ant任务支持依附环境管理,在Maven之外进行发布。
  • 在大多数情况下,maven不需要脚本就可以编译多个项目,打包成jar, war或基于元数据进行项目发布
  • 清晰的项目信息:Maven能够建立一个站点或PDF文档,其中包括项目的一些基本信息和报表。
  • 版本管理和项目发布:不需要太多的额外配置,maven会和你的代码管理器整合,然后基于一定的tag,来帮助管理你的版本。它也可以为其他的项目发布到一个地方。maven也支持发布成jar或其他归档的文件或是源代码。
  • 依附管理:maven建议集中管理jar库。项目客户可以到集中的jar库中下载任何包。这允许maven用户可以在多个项目中公用jar,并鼓励项目之间的交流。

 

Maven如何帮助改进我们的开发过程?

通过标准的约定和实践Maven对你的build流程提供帮助,加速你的开发周期并同时帮助你的项目成功。

下面我们提供一些例子让你来运行maven!

如何建立Maven?

基本上,maven默认的配置已经足够面对大部分情况了,但是如果你需要修改缓存策略或者设置http代理,你就需要自己建立一个配置。详细请参考Guide to Configuring Maven

 

如何建立我的第一个maven项目?

我们会使用maven的archetype 机制来建立我们的第一个maven项目。maven中,archetype是项目模版,在加上一些用户的输入来建立一个maven的项目。我们会向你展示archetype机制如何工作。更多请参考

 


创建最简单的maven项目,只需要执行下面的命令就可以了

mvn archetype:generate \   -DarchetypeGroupId=org.apache.maven.archetypes \   -DgroupId=com.mycompany.app \   -DartifactId=my-app 

执行命令后,首先,你会发现一个叫my-app的文件夹建立了,其中有pom.xml文件,内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

pom.xml里有项目的project object model。pom是maven的基本工作单元。This is important to remember because Maven is inherently project-centric in that everything revolves around the notion of a project. 简单的说,pom包含了所有项目的重要信息。更多请参考Introduction to the POM.

This is a very simple POM but still displays the key elements every POM contains, so let's walk through each of them


to familiarize you with the POM essentials:

  • project This is the top-level element in all Maven pom.xml files.
  • modelVersion This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
  • groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plug-ins.
  • artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
  • packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.
  • version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.
  • name This element indicates the display name used for the project. This is often used in Maven's generated documentation.
  • url This element indicates where the project's site can be found. This is often used in Maven's generated documentation.
  • description This element provides a basic description of your project. This is often used in Maven's generated documentation.

For a complete reference of what elements are available for use in the POM please refer to our POM Reference. Now let's get back to the project at hand.

After the archetype generation of your first project you will also notice that the following directory structure has been created:

my-app |-- pom.xml `-- src     |-- main     |   `-- java     |       `-- com     |           `-- mycompany     |               `-- app     |                   `-- App.java     `-- test         `-- java             `-- com                 `-- mycompany                     `-- app                         `-- AppTest.java 

As you can see, the project created from the archetype has a POM, a source tree for your application's sources and a source tree for your test sources. This is the standard layout for Maven projects (the application sources reside in ${basedir}/src/main/java and test sources reside in ${basedir}/src/test/java, where ${basedir} represents the directory containing pom.xml).

If you were to create a Maven project by hand this is the directory structure that we recommend using. This is a Maven convention and to learn more about it you can read our Introduction to the Standard Directory Layout.

Now that we have a POM, some application sources, and some test sources you are probably asking ...


 









 

posted on 2012-04-16 17:05 思无 阅读(502) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: