Maven学习总结系列三:Maven入门

1.编写POM

Maven项目的核心就是pom.xml,POM(Project Object Model,项目对象模型)定义了项目的基本信息,用于描述项目如何构建,声明项目依赖,等等。

还记得我们第一章遇到的问题吗?

在开发的过程中,我们需要到各个网站上下载第三方的JAR包,随着项目的慢慢增大,JAR包会变得越来越多,可能出现包之间的版本冲突,项目变得臃肿等问题。

然后Maven提出了坐标来为每一个artifact给定唯一的标识。从此artifact从乱混中解放出来,整个JAR包世界变得有秩序,有标准。

同时Maven自动识别artifact之间的依赖关系,从而自动下载依赖的JAR包,不再需要我们去网站上额外下载。

它还会检测一些未在项目上使用的JAR包,这样方便我们清除不需要包,对项目做一个瘦身。

而实现这些功能,Maven是通过一个pom.xml文件配置来实现的。

1.1 一个简单的pom.xml配置

首先,我们得先为自己定义一个坐标

-------------------------------------------------------------------------------------------

<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.juvenxu.mvnbook</groupId>

  <artifactId>hello-world</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>MavenHello World Project</name>

  <dependencies>

  <!-- https://mvnrepository.com/artifact/junit/junit-->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

  </dependencies>

</project>

-------------------------------------------------------------------------------------------

相关信息说明:

modelVersion:指定了当前POM的模型版本,对于Maven3来说,它只能是4.0.0

groupId, artifactId 和version:三个元素定义了一个项目的基本坐标。 在Maven的世界,任何的Jar,pom 或war都是以基于这些基本的坐标进行区分的。

groupId, 定义了项目属于哪个组,这个组往往和项目所在的组织或公司存在关联。

如:googlecode创建一个名为myapp的项目,则groupId为com.googlecode.myapp

artifactId, 定义了当前Maven项目在组中唯一的ID。

如:这个项目是myapp的一个模块,service层,则为myapp-service,为了区分,识别这个service是哪个组的,会使用  组名-模块  这样的命名方式,这里 组名为 myapp,模块名 service,所以artifactId为myapp-service

version:指定项目的当前版本。

name:声明一个对于用户更为友好的项目名称。

1.2 编写主代码

项目主代码会被打包到最终的构件中(如jar),而测试代码只在运行测试时用到,不会被打包。

Maven的约定目录结构:

 

其中:

项目主代码:src/main/java

项目主资源:src/main/resources

测试代码:src/test/java

测试资源:src/test/resources

helloWorld.java

----------------------------------------------------

packagecom.juvenxu.mvnbook.helloWorld;

publicclass HelloWorld{

public String sayHello(){

return "Hello World!";

}

public static void main(String []args){

System.out.println(new HelloWorld().sayHello());

}

}

----------------------------------------------------

目录结构如下:

E:\maven\workspace\HelloWorld>tree

FolderPATH listing

Volumeserial number is 000000E0 C4F5:2D3F

E:.

└─src

    ├─main

    │ └─java

    │     └─com

    │         └─juvenxu

    │              └─mvnbook

    │                  └─helloWorld

    └─test

        └─java

            └─com

                └─java1234

                    └─helloWorld

使用Maven进行编译

clean:告诉Maven清理输出目录target/

compile:告诉Maven编译项目主代码。

E:\maven\workspace\HelloWorld>mvn clean compile

[INFO]Scanning for projects...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO]Building Maven Hello World Project 0.0.1-SNAPSHOT

[INFO]------------------------------------------------------------------------

[INFO]

[INFO]--- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---

[INFO]

[INFO]--- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---

[WARNING]Using platform encoding (GBK actually) to copy filtered resources, i.e. buildis platform dependent!

[INFO]skip non existing resourceDirectoryE:\maven\workspace\HelloWorld\src\main\resources

[INFO]

[INFO]--- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---

[INFO]Changes detected - recompiling the module!

[WARNING]File encoding has not been set, using platform encoding GBK, i.e. build isplatform dependent!

[INFO] Compiling 1 source file toE:\maven\workspace\HelloWorld\target\classes

[INFO]------------------------------------------------------------------------

[INFO]BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO]Total time: 4.476 s

[INFO]Finished at: 2017-10-25T22:49:40+08:00

[INFO]Final Memory: 13M/243M

[INFO]------------------------------------------------------------------------

编译后目录结构,target产生编译代码:

E:\maven\workspace\HelloWorld>tree

FolderPATH listing

Volumeserial number is 00000027 C4F5:2D3F

E:.

├─src

│  ├─main

│  │ └─java

│  │     └─com

│  │         └─juvenxu

│  │             └─mvnbook

│  │                  └─helloWorld

│  └─test

│      └─java

│          └─com

└─target

    ├─classes

    │ └─com

    │     └─juvenxu

    │         └─mvnbook

    │              └─helloWorld

    └─maven-status

        └─maven-compiler-plugin

            └─compile

                └─default-compile

清理target目录

E:\maven\workspace\HelloWorld>mvn clean

[INFO]Scanning for projects...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO]Building Maven Hello World Project 0.0.1-SNAPSHOT

[INFO]------------------------------------------------------------------------

[INFO]

[INFO]--- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---

[INFO] Deleting E:\maven\workspace\HelloWorld\target

[INFO]------------------------------------------------------------------------

[INFO]BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO]Total time: 0.226 s

[INFO]Finished at: 2017-10-25T22:52:41+08:00

[INFO]Final Memory: 7M/243M

[INFO]------------------------------------------------------------------------

E:\maven\workspace\HelloWorld>tree

FolderPATH listing

Volumeserial number is 000000E0 C4F5:2D3F

E:.

└─src

    ├─main

    │ └─java

    │     └─com

    │         └─juvenxu

    │              └─mvnbook

    │                  └─helloWorld

    └─test

        └─java

            └─com

1.3 编写测试代码

Maven为了使项目结构保持清晰,主代码与测试代码分别位于独立的目录中。

Test the code:

1.Createtest folder

2.Set theJunit jar into pom.xml

3.codethe test source code

为了测试,我们需要在pom.xml添加一个Junit artifact,好让maven帮我们下载这个jar包。

pom.xml

---------------------------------------------------------------------------------------------------------------

<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <groupId>com.juvenxu.mvnbook</groupId>

 <artifactId>hello-world</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>Maven Hello WorldProject</name>

  <dependencies>

  <!-- https://mvnrepository.com/artifact/junit/junit-->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

  </dependencies>

</project>

-------------------------------------------------------------------------------------------------------

一个典型的单元测试包含三个步骤:

a.准备测试类及数据。

b.执行要测试的行为;

c.检查结果。

在Junit中,约定所有需要执行测试的方法都以test开头。

Create test class

在src/test/java下建立测式用例

HelloWorldTest.java

-----------------------------------------------------------------------

packagecom.juvenxu.mvnbook.helloWorld;

importorg.junit.Test;

publicclass HelloWorldTest{

@Test

public void testSayHello(){

HelloWorld helloWorld=new HelloWorld();

String result=helloWorld.sayHello();

System.out.println(result);

}

}

----------------------------------------------------------------------

Note:the method name must begin with 'test'.

Eg.testSayHello()

调用Maven执行测试: mvn clearn test

E:\maven\workspace\HelloWorld>mvn clean test

[INFO]Scanning for projects...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO]Building Maven Hello World Project 0.0.1-SNAPSHOT

[INFO]------------------------------------------------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.5:clean (default-clean)@ hello-world ---

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources(default-resources) @ hello-world ---

[WARNING]Using platform encoding (GBK actually) to copy filtered resources, i.e. buildis platform dependent!

[INFO]skip non existing resourceDirectoryE:\maven\workspace\HelloWorld\src\main\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile(default-compile) @ hello-world ---

[INFO]Changes detected - recompiling the module!

[WARNING]File encoding has not been set, using platform encoding GBK, i.e. build isplatform dependent!

[INFO] Compiling 1 source file toE:\maven\workspace\HelloWorld\target\classes

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources(default-testResources) @ hello-world ---

[WARNING]Using platform encoding (GBK actually) to copy filtered resources, i.e. buildis platform dependent!

[INFO]skip non existing resourceDirectoryE:\maven\workspace\HelloWorld\src\test\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile(default-testCompile) @ hello-world ---

[INFO]Changes detected - recompiling the module!

[WARNING]File encoding has not been set, using platform encoding GBK, i.e. build isplatform dependent!

[INFO] Compiling 1 source file toE:\maven\workspace\HelloWorld\target\test-classes

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test(default-test) @ hello-world ---

[INFO] Surefire report directory:E:\maven\workspace\HelloWorld\target\surefire-reports

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Runningcom.juvenxu.mvnbook.helloWorld.HelloWorldTest

Hello World!

Testsrun: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.058 sec

Results :

Testsrun: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]------------------------------------------------------------------------

[INFO]BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO]Total time: 3.524 s

[INFO]Finished at: 2017-10-25T23:07:12+08:00

[INFO]Final Memory: 15M/210M

[INFO]------------------------------------------------------------------------

由上面的输出我们可以看到,在执行test之前,它会先自动执行

项目主资源处理,主代码编译,测试资源处理,测试代码编译等工作,这是Maven生命周期的一个特性,执行生命周期中某个命令时,必须要先执行这个生命周期中它之前的命令。

mvn clean test will execute command:

--- maven-clean-plugin:2.5:clean

--- maven-resources-plugin:2.6:resources

--- maven-compiler-plugin:3.1:compile

--- maven-resources-plugin:2.6:testResources

--- maven-compiler-plugin:3.1:testCompile

--- maven-surefire-plugin:2.12.4:test

现在项目结构如下:

E:\maven\workspace\HelloWorld>tree /F

FolderPATH listing

Volumeserial number is 000000ED C4F5:2D3F

E:.

│  pom.xml

├─src

│  ├─main

│  │ └─java

│  │     └─com

│  │         └─juvenxu

│  │             └─mvnbook

│  │                  └─helloWorld

│  │                          HelloWorld.java

│  │

│  └─test

│      └─java

│          └─com

│              └─juvenxu

│                  └─mvnbook

│                      └─helloWorld

│                              HelloWorldTest.java

└─target

    ├─classes

    │ └─com

    │     └─juvenxu

    │         └─mvnbook

    │              └─helloWorld

    │                      HelloWorld.class

    │

    ├─maven-status

    │ └─maven-compiler-plugin

    │      ├─compile

    │     │  └─default-compile

    │     │          createdFiles.lst

    │     │          inputFiles.lst

    │     │

    │     └─testCompile

    │         └─default-testCompile

    │                  createdFiles.lst

    │                  inputFiles.lst

    │

    ├─surefire-reports

    │     com.juvenxu.mvnbook.helloWorld.HelloWorldTest.txt

    │     TEST-com.juvenxu.mvnbook.helloWorld.HelloWorldTest.xml

    └─test-classes

        └─com

            └─juvenxu

                └─mvnbook

                    └─helloWorld

                            HelloWorldTest.class

3.4 打包和运行 mvn clean package

将项目进行编译,测试之后,下一个重要步骤就是打包(package)。

当POM文件中没的指定打包类型时,maven的默认打包类型是jar.

E:\maven\workspace\HelloWorld>mvn clean package

[INFO]Scanning for projects...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO]Building Maven Hello World Project 0.0.1-SNAPSHOT

[INFO]------------------------------------------------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @hello-world ---

[INFO]Deleting E:\maven\workspace\HelloWorld\target

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources(default-resources) @ hello-world ---

[WARNING]Using platform encoding (GBK actually) to copy filtered resources, i.e. buildis platform dependent!

[INFO]skip non existing resourceDirectoryE:\maven\workspace\HelloWorld\src\main\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile)@ hello-world ---

[INFO]Changes detected - recompiling the module!

[WARNING]File encoding has not been set, using platform encoding GBK, i.e. build isplatform dependent!

[INFO]Compiling 1 source file to E:\maven\workspace\HelloWorld\target\classes

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources(default-testResources) @ hello-world ---

[WARNING]Using platform encoding (GBK actually) to copy filtered resources, i.e. buildis platform dependent!

[INFO]skip non existing resourceDirectoryE:\maven\workspace\HelloWorld\src\test\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile(default-testCompile) @ hello-world ---

[INFO]Changes detected - recompiling the module!

[WARNING]File encoding has not been set, using platform encoding GBK, i.e. build isplatform dependent!

[INFO]Compiling 1 source file to E:\maven\workspace\HelloWorld\target\test-classes

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @hello-world ---

[INFO]Surefire report directory:E:\maven\workspace\HelloWorld\target\surefire-reports

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Runningcom.juvenxu.mvnbook.helloWorld.HelloWorldTest

HelloWorld!

Testsrun: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.059 sec

Results :

Testsrun: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]

[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---

[INFO]Building jar: E:\maven\workspace\HelloWorld\target\hello-world-0.0.1-SNAPSHOT.jar

[INFO]------------------------------------------------------------------------

[INFO]BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO]Total time: 7.572 s

[INFO]Finished at: 2017-10-26T20:13:23+08:00

[INFO]Final Memory: 15M/200M

[INFO]------------------------------------------------------------------------

执行pacakge后,maven会把项目主代码打包成一个名为hello-world-0.0.1-SNAPSHOT.jar的文件。

该文件也位于target输出目录中。

命名规则:

artifact-version.jar

如果这个名不适用,可以用finalName来重新定义输出名称。

输出结果如下:

E:\maven\workspace\HelloWorld>tree/F

FolderPATH listing

Volumeserial number is 0000004C C4F5:2D3F

E:.

│  pom.xml

├─src

│  ├─main

│  │ └─java

│  │     └─com

│  │         └─juvenxu

│  │             └─mvnbook

│  │                  └─helloWorld

│  │                          HelloWorld.java

│  │

│  └─test

│      └─java

│          └─com

│              └─juvenxu

│                  └─mvnbook

│                      └─helloWorld

│                             HelloWorldTest.java

└─target

    │  hello-world-0.0.1-SNAPSHOT.jar

    ├─classes

    │ └─com

    │     └─juvenxu

    │         └─mvnbook

    │              └─helloWorld

    │                      HelloWorld.class

    │

    ├─maven-archiver

    │     pom.properties

    │

    ├─maven-status

    │ └─maven-compiler-plugin

    │      ├─compile

    │     │  └─default-compile

    │     │          createdFiles.lst

    │     │          inputFiles.lst

    │     │

    │     └─testCompile

    │         └─default-testCompile

    │                  createdFiles.lst

    │                  inputFiles.lst

    │

    ├─surefire-reports

    │     com.juvenxu.mvnbook.helloWorld.HelloWorldTest.txt

    │     TEST-com.juvenxu.mvnbook.helloWorld.HelloWorldTest.xml

   │

    └─test-classes

        └─com

            └─juvenxu

                └─mvnbook

                    └─helloWorld

                           HelloWorldTest.class


 

Note: Now, we cancopy this jar package to the classpath of other project to use it.

Another way, we can install it to the local repository instead ofcopy the jar manually.

安装JAR包到本地仓库 mvn clearn install

安装JAR到本地仓库后,其它项目可以使用这个JAR包:

Let other Project to use this jar package , we needinstall this jar to local repository.

E:\maven\workspace\HelloWorld>mvn clean install

[INFO]Scanning for projects...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO]Building Maven Hello World Project 0.0.1-SNAPSHOT

[INFO]------------------------------------------------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---

[INFO]Deleting E:\maven\workspace\HelloWorld\target

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---

[WARNING]Using platform encoding (GBK actually) to copy filtered resources, i.e. buildis platform dependent!

[INFO]skip non existing resourceDirectoryE:\maven\workspace\HelloWorld\src\main\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---

[INFO]Changes detected - recompiling the module!

[WARNING]File encoding has not been set, using platform encoding GBK, i.e. build isplatform dependent!

[INFO]Compiling 1 source file to E:\maven\workspace\HelloWorld\target\classes

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---

[WARNING]Using platform encoding (GBK actually) to copy filtered resources, i.e. buildis platform dependent!

[INFO]skip non existing resourceDirectoryE:\maven\workspace\HelloWorld\src\test\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world ---

[INFO]Changes detected - recompiling the module!

[WARNING]File encoding has not been set, using platform encoding GBK, i.e. build isplatform dependent!

[INFO]Compiling 1 source file to E:\maven\workspace\HelloWorld\target\test-classes

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---

[INFO]Surefire report directory:E:\maven\workspace\HelloWorld\target\surefire-reports

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Runningcom.juvenxu.mvnbook.helloWorld.HelloWorldTest

HelloWorld!

Testsrun: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.067 sec

Results :

Testsrun: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]

[INFO] --- maven-jar-plugin:2.4:jar(default-jar) @ hello-world ---

[INFO]Building jar: E:\maven\workspace\HelloWorld\target\hello-world-0.0.1-SNAPSHOT.jar

[INFO]

[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world ---

[INFO]Installing E:\maven\workspace\HelloWorld\target\hello-world-0.0.1-SNAPSHOT.jartoE:\maven\repository\com\juvenxu\mvnbook\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.jar

[INFO]Installing E:\maven\workspace\HelloWorld\pom.xml to E:\maven\repository\com\juvenxu\mvnbook\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.pom

[INFO]------------------------------------------------------------------------

[INFO]BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO]Total time: 2.945 s

[INFO]Finished at: 2017-10-26T20:21:36+08:00

[INFO]Final Memory: 16M/212M

[INFO]------------------------------------------------------------------------


 

到这里为止,我们按照Maven的对于结构目录的约定来创建 项目主代码,测试代码的目录及 主代码,测试代码。并而执行了 从 clean, compile , test, package, install 的生命周期中的操作。

问题:有没有觉得手工创建这些目录很累,有没有一种方式,可以快速创建一个Maven约定的项目骨构?省得我们一个个手工建?

项目骨架:指maven约定的基本目录结构和pom.xml文件内容。

方案:maven archetype, 可以快速的创建项目的骨架。

之所有我们一个个的手工建,是为了让我们在创建过程中体会默认约定背后的思想及体验整个过程。

Using ArcheType to build the artifact of prject

The following is the artifact of project:

The question is: how to build them quickly?

E:\maven\workspace>mvn archetype:generate

[INFO] Scanning forprojects...

Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml

Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml(825 B at 0.5 KB/sec)

Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.pom

Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.pom(10 KB at 17.7 KB/sec)

1959: remote ->us.fatehi:schemacrawler-archetype-maven-project (-)

1960: remote ->us.fatehi:schemacrawler-archetype-plugin-command (-)

1961: remote ->us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)

1962: remote ->us.fatehi:schemacrawler-archetype-plugin-lint (-)

Choose a number orapply filter (format: [groupId:]artifactId, case sensitive contains): 1057:1057

Chooseorg.apache.maven.archetypes:maven-archetype-quickstart version:

1: 1.0-alpha-1

2: 1.0-alpha-2

3: 1.0-alpha-3

4: 1.0-alpha-4

5: 1.0

6: 1.1

Choosea number: 6:6

Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom

Define value forproperty 'groupId': com.juvenxu.mvnbook

Define value forproperty 'artifactId': say-hello

Define value forproperty 'version' 1.0-SNAPSHOT: :

Define value forproperty 'package' com.juvenxu.mvnbook: : com.juvenxu.mvnbook.sayHello

Confirm properties configuration:

groupId:com.juvenxu.mvnbook

artifactId:say-hello

version:1.0-SNAPSHOT

package:com.juvenxu.mvnbook.sayHello

 Y: : y

[INFO]----------------------------------------------------------------------------

[INFO] Usingfollowing parameters for creating project from Old (1.x) Archetype:maven-archetype-quickstart:1.1

[INFO]----------------------------------------------------------------------------

[INFO] Parameter:basedir, Value: E:\maven\workspace

[INFO] Parameter:package, Value: com.juvenxu.mvnbook.sayHello

[INFO] Parameter:groupId, Value: com.juvenxu.mvnbook

[INFO] Parameter:artifactId, Value: say-hello

[INFO] Parameter:packageName, Value: com.juvenxu.mvnbook.sayHello

[INFO] Parameter:version, Value: 1.0-SNAPSHOT

[INFO] projectcreated from Old (1.x) Archetype in dir: E:\maven\workspace\say-hello

[INFO]------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO] Total time:10:10 min

[INFO] Finished at:2017-10-26T21:16:25+08:00

[INFO] Final Memory:14M/153M

[INFO]------------------------------------------------------------------------

我们可以看到,maven已经自动帮我们创建了maven项目的骨架出来。

E:\maven\workspace>cdsay-hello

E:\maven\workspace\say-hello>tree/F

Folder PATH listing

Volume serial numberis 00000063 C4F5:2D3F

E:.

│  pom.xml

└─src

    ├─main

    │ └─java

    │     └─com

    │         └─juvenxu

    │              └─mvnbook

    │                  └─sayHello

    │                          App.java

    │

    └─test

        └─java

            └─com

                └─juvenxu

                    └─mvnbook

                        └─sayHello

                                AppTest.java

同时,pom.xml也生成了默认的内容

pom.xml

------------------------------------------------------------------------------------------------------------------------

<projectxmlns="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.juvenxu.mvnbook</groupId>

  <artifactId>say-hello</artifactId>

  <version>1.0-SNAPSHOT</version>

  <packaging>jar</packaging>

  <name>say-hello</name>

 <url>http://maven.apache.org</url>

  <properties>

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

</project>

------------------------------------------------------------------------------------------------------------------------

说明:Archetype可以帮助我们迅速地构建起项目的骨架,在前面的例子中,我们完全可以在Archetype生成的骨架的基础上开发Hello World项目以节省大量时间。

3.6 Create maven project in eclipse

Import amaven project which has just be build name say-Hello

 

 




1.6.2 Create a new maven project

在eclipse中创建一个Maven项目也很简单,File ->New -> Other

 

 

 

由于eclipse实际上是在使用maven-archetype-plugin插件创建项目。因此这个过程与上面使用archetype创建项目骨架是一样的。

 

点击“Finish”后,系统自动创建骨构,如下:

 

项目主代码:

App.java

----------------------------------------------------------

packagecn.ss.maven.test.helloWorld;

/**

 * Hello world!

 *

 */

public class App

{

    public static void main( String[] args )

    {

        System.out.println( "HelloWorld!" );

    }

}

----------------------------------------------------------

pom.xml

----------------------------------------------------------

<projectxmlns="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>cn.ss.maven.test</groupId>

 <artifactId>helloWorld</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

  <name>helloWorld</name>

 <url>http://maven.apache.org</url>

  <properties>

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

     <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

</project>

----------------------------------------------------------

在Eclipse中,我们使用pom.xml-->Run As操作来执行maven命令。

You canexecute maven command in this way:

 

Or youcan build the customized Maven command such as mvnclean test by Maven build or press Alt + Shift + X,M

 

Click "Run" button: system will execute the command wehave defined 'clean test'

It like execute 'mvn clean test'

The result as below:

[INFO] Scanning forprojects...

[INFO]                                                                        

[INFO]------------------------------------------------------------------------

[INFO] BuildinghelloWorld 0.0.1-SNAPSHOT

[INFO]------------------------------------------------------------------------

[INFO]

[INFO] ---maven-clean-plugin:2.5:clean (default-clean) @ helloWorld ---

[INFO] DeletingD:\Workspaces\eclipse-jee-oxygen_x86_64\helloWorld\target

[INFO]

[INFO] ---maven-resources-plugin:2.6:resources (default-resources) @ helloWorld ---

[INFO] Using 'UTF-8'encoding to copy filtered resources.

[INFO] skip nonexisting resourceDirectoryD:\Workspaces\eclipse-jee-oxygen_x86_64\helloWorld\src\main\resources

[INFO]

[INFO] ---maven-compiler-plugin:3.1:compile (default-compile) @ helloWorld ---

[INFO] Changesdetected - recompiling the module!

[INFO] Compiling 1source file toD:\Workspaces\eclipse-jee-oxygen_x86_64\helloWorld\target\classes

[INFO]

[INFO] ---maven-resources-plugin:2.6:testResources (default-testResources) @ helloWorld---

[INFO] Using 'UTF-8'encoding to copy filtered resources.

[INFO] skip nonexisting resourceDirectoryD:\Workspaces\eclipse-jee-oxygen_x86_64\helloWorld\src\test\resources

[INFO]

[INFO] ---maven-compiler-plugin:3.1:testCompile (default-testCompile) @ helloWorld ---

[INFO] Changesdetected - recompiling the module!

[INFO] Compiling 1source file toD:\Workspaces\eclipse-jee-oxygen_x86_64\helloWorld\target\test-classes

[INFO]

[INFO] ---maven-surefire-plugin:2.12.4:test (default-test) @ helloWorld ---

[INFO] Surefirereport directory:D:\Workspaces\eclipse-jee-oxygen_x86_64\helloWorld\target\surefire-reports

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom(2 KB at 1.0 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar(26 KB at 26.7 KB/sec)

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Runningcn.ss.maven.test.helloWorld.AppTest

Tests run: 1,Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec

Results :

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

[INFO]------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO] Total time:6.752 s

[INFO] Finished at:2017-10-26T21:30:56+08:00

[INFO] Final Memory:19M/214M

[INFO]------------------------------------------------------------------------

小结:本章详细的叙述了如何手工创建符合maven骨构的Hello World项目,解释了POM的基本内容,Maven项目的基本结构及构建项目基本的Maven命令。

如何使用Archetype快速的创建maven骨架及如何在Eclipse创建maven项目。


猜你喜欢

转载自blog.csdn.net/arnolian/article/details/78808305
今日推荐