Maven学习笔记(二)

文章摘要
1、Maven 第一个工程,HelloWorld创建详情
2、POM元素、archetype自动创建工程。
3、IDE工具导入、创建

1、POM

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

2、POM元素

  • modelVersion:指定当前POM模型版本
  • groupId:定义项目属于哪个组。
  • artifactId:当前Maven项目在组的唯一ID。
  • version:指定当前项目的版本号。
  • name:项目更加友好的外部名称。
<modelVersion>4.0.0</modelVersion>
<groupId>com.hib.demo</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>

3、Maven HelloWorld项目

3.1、新建工程hello-world,新建pom.xml,填充内容如上POM元素介绍。

3.2、工程中,默认代码文件存放位置。

Maven会自动搜寻目录下的文件,并打包。位置如下:

  • 主代码:src/main/java
  • 测试代码:src/main/test

在主代码文件夹中,新建HelloWorld.java

package com.hib.demo;

public class HelloWorld {
    public String sayHello(){
        return "Hello Maven";
    }
    
    public static void main(String args[]){
        System.out.println(new HelloWorld().sayHello());
    }
}

3.3、使用mvn clean compile编译

  • 3.3.1、clean:告诉编译器,清理输出文件夹target目录。
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
  • 3.3.2、resources:告诉编译器,编译项目资源。
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
  • 3.3.3、compile:告诉编译器,编译项目主代码,输出到target/classes目录。
[INFO] skip non existing resourceDirectory /Users/ifei/workspace/webWork/HibDemo/hello-world/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---

3.4、编写测试代码

测试代码,需要用到Junit,我们需要在POM中增加依赖。

3.4.1、pom.xml

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>
</dependencies>

这个配置,会去访问如下链接下的资源:

http://repo1.maven.org/maven2/junit/junit/4.7/

3.4.2、执行代码编译、测试

编译测试代码,命令:

mvn clean test

代码执行过程:

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting /Users/ifei/workspace/webWork/HibDemo/hello-world/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/ifei/workspace/webWork/HibDemo/hello-world/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 UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/ifei/workspace/webWork/HibDemo/hello-world/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/ifei/workspace/webWork/HibDemo/hello-world/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 UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/ifei/workspace/webWork/HibDemo/hello-world/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---

经过clean、resources、compile、testResources、testCompile,最后执行sureFire来执行运行测试。

3.4.3、附:HelloWorldTest.java

package com.hib.demotest.helloworld;

import static org.junit.Assert.*;

import org.junit.Test;

import com.hib.demo.HelloWorld;

public class HelloWorldTest {

    @Test
    public void testSayHello() {
        HelloWorld helloWorld = new HelloWorld();
        String result = helloWorld.sayHello();
//        fail("Not yet implemented");
        assertEquals("Hello Maven", result);
    }
}

3.5、打包和运行

  • 打包命令:
mvn clean package

备注:如果需要打包的jar能够直接运行,需要指定MainClass,则在pom.xml中的配置如下:

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>om.hib.demo.HelloWorld</Main-Class>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  • 安装命令:
mvn clean install

安装后,就被加入本地maven库了,即可以被其他工程引用、使用了。

4、使用Archetype创建工程

4.1、创建命令:

mvn archetype:generate

我们需要在向导指定groupId、artifactId等。

Define value for property 'groupId': com.maven.demo1
Define value for property 'artifactId': demo1
Define value for property 'version' 1.0-SNAPSHOT: : 
Define value for property 'package' com.maven.demo1: : 
Confirm properties configuration:
groupId: com.maven.demo1
artifactId: demo1
version: 1.0-SNAPSHOT
package: com.maven.demo1
 Y: : 

生成目录以及文件:

.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── maven
    │               └── demo1
    │                   └── App.java
    └── test
        └── java
            └── com
                └── maven
                    └── demo1
                        └── AppTest.java

11 directories, 3 files

附:工程创建过程:

[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /Users/ifei/workspace/webWork/HibDemo/hello-world/mavenDemo
[INFO] Parameter: package, Value: com.maven.demo1
[INFO] Parameter: groupId, Value: com.maven.demo1
[INFO] Parameter: artifactId, Value: demo1
[INFO] Parameter: packageName, Value: com.maven.demo1
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/ifei/workspace/webWork/HibDemo/hello-world/mavenDemo/demo1
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:54 min
[INFO] Finished at: 2017-07-11T19:48:44+08:00
[INFO] Final Memory: 16M/206M
[INFO] ------------------------------------------------------------------------

5、IDE工程的导入和创建

和Java工程类似,我们可以使用IDE进行Maven的导入和创建。不在详述

    原文作者:Android那些事儿
    原文地址: https://www.jianshu.com/p/a9b0431d2696
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞