当前位置:编程学习 > JAVA >>

五分钟快速搞定maven(maven in 5 minutes)

Prerequisites
You must have an understanding of how to install software on your computer. If you do not know how to do this, please ask someone at your office, school, etc or pay someone to explain this to you. The Maven mailing lists are not the best place to ask for this advice.
 
预备知识
你必须清楚怎么在电脑里面安装软件。如果你不知道如何做到这一点,请向你的同事、同学等请教,或者你可以付费请别人教你一下。这个关于Maven的知识列表可不是你询问如何安装软件的好地方。
 
 
Installation
Maven is a Java tool, so you must have Java installed in order to proceed.
First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:
mvn --version
It should print out your installed version of Maven, for example:
Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)
Maven home: D:\apache-maven-3.0.3\bin\..
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
Java home: E:\Program Files\Java\jdk1.6.0_25\jre
Default locale: nl_NL, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
Depending upon your network setup, you may require extra configuration. Check out the Guide to Configuring Maven if necessary.
If you are using Windows, you should look at Windows Prerequisites to ensure that you are prepared to use Maven on Windows.
 
安装
Maven是一个Java工具,所以首先你得确保安装了Java,以便于后续安装的进行。
首先下载Maven并且遵循下载说明。然后,在终端或者命令行输入以下命令:
[html]  
mvn --version  
会打印出您的Maven的安装信息,如下:
[html] 
Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)  
Maven home: D:\apache-maven-3.0.3\bin\..  
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.  
Java home: E:\Program Files\Java\jdk1.6.0_25\jre  
Default locale: nl_NL, platform encoding: Cp1252  
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"  
 
这(以上结果)取决于你的网络设置,你可能需要额外的配置(maven的环境变量配置)。如有必要,请参见Maven配置指南。如果你使用的是Windows系统,请阅读Windows预备知识以确保你准备在Windows上使用Maven。
 
 
 
Creating a Project
You will need somewhere for your project to reside, create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete. Don't worry, there are ways to fix that.
You will notice that the generate goal created a directory with the same name given as the artifactId. Change into that directory.
cd my-app
Under this directory you will notice the following standard project structure.
my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
The src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml file is the project's Project Object Model, or POM.
The POM
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively. This project's POM is:
<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>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
What did I just do?
You executed the Maven goal archetype:generate, and passed in various parameters to that goal. The prefix archetype is the plugin that contains the goal. If you are familiar with Ant, you may conceive of this as similar to a task. This goal created a simple project based upon an archetype. Suffice it to say for now that a plugin is a collection of goals with a general common purpose. For example the jboss-maven-plugin, whose purpose is "deal with various jboss items".
Build the Project
mvn package
The command line will print out various actions, and end with the following:
 ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
Unlike the first command executed (archetype:generate) you may notice the second is simply a single word - package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,