首页 > 技术文章 > maven系列(2)-第一个maven的项目

wardensky 2015-05-25 15:27 原文

上一篇简单了介绍了maven和maven的安装,这篇介绍如何用maven创建项目。

1. 命令行创建maven项目

maven创建项目很简单,直接调用mvn archetype:generate命令即可,最简单的一个创建例子如下【来自maven-in-five-minutes】:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

在windows的命令行下,运行以上命令,会自动创建一个maven项目。如果创建失败,或者创建的时间非常久,请参考
创建之后,目录结构如下:

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

可以看到,在目录下面有一个pom.xml文件,这个文件是maven 的配置文件。除此之外,有一个src目录,下面又包括main目录和test目录,分别代码项目源代码和测试源代码。

2. 编译部署项目

用任何编辑器打开App.java,在里面写一些简单代码,例子如下:

package com.wisdombud;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

写完之后,回到包含pom.xml的目录,编译项目,命令如下:

mvn package

输出一般为:

[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethrea
ded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building chzhaotest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ chzhaotest
 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\tmp\chzhaotest\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ chzhaotest --
-
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to D:\tmp\chzhaotest\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ch
zhaotest ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\tmp\chzhaotest\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ chzha
otest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ chzhaotest ---
[INFO] Surefire report directory: D:\tmp\chzhaotest\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.wisdombud.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ chzhaotest ---
[INFO] Building jar: D:\tmp\chzhaotest\target\chzhaotest-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.680 s
[INFO] Finished at: 2015-05-25T15:04:52+08:00
[INFO] Final Memory: 13M/80M
[INFO] ------------------------------------------------------------------------

可以看到Building jar: D:\tmp\chzhaotest\target\chzhaotest-1.0-SNAPSHOT.jar,也就是说,已经编译成jar文件。
可以通过java命令测试编译出来的文件是否正确。

java -cp target\chzhaotest-1.0-SNAPSHOT.jar com.wisdombud.App

3. pom.xml文件介绍

本例中,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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wisdombud</groupId>
  <artifactId>chzhaotest</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>chzhaotest</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文件里面定义了项目的一些基本信息和依赖。
如groupId,artifactId,version等。
依赖定义在dependencies下面,一般包括groupId、artifactId、version等,scope不是强制需要的。

推荐阅读