专业知识

1、maven入门(maven笔记)

2016-06-17  本文已影响503人  yjaal

一、安装

首先maven的配置安装非常简单:将下载下来的压缩包解压之后将bin目录的路径加入到path环境变量下即可,使用mvn –version可以验证。

二、手工演示maven构建java项目

package cn.edu.zttc.hello;
 public class Hello{
     
     public String sayHello(String name){
         return "Hello:" + name;
     }
     
     public static void main(String[] args){
         System.out.println("Hello World");
     }
 }

注意:这里我们需要将此文件放在01\src\main\java\cn\edu\zttc\hello目录下(根据包名),当然不存在的目录需要我们手工新建出来。

<?xml version="1.0" encoding="UTF-8"?>

<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>cn.edu.zttc.maven.hello</groupId>  
    <!--配置项目的一个模块-->
    <artifactId>hello-first</artifactId>
    <!--版本信息:这是一个快照版本-->
    <version>SNAPSHOT-0.0.1</version>
</project>

这样我们就配置好了一个java项目的基本信息。

package cn.edu.zttc.hello;
import org.junit.*;
import static junit.framework.Assert.*;
import cn.edu.zttc.hello.*;

public class TestHello{
    
    @Test
    public void testHello(){
        Hello h = new Hello();
        assertEquals( h.sayHello("tom"), "Hello:tom");
    }
}

注意:路径的后半部分是根据包名而定的。
同时,显然此测试类需要用到junit的相关包,所以我们需要在pom.xml文件中配置junit:

<?xml version="1.0" encoding="UTF-8"?>

<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>cn.edu.zttc.maven.hello</groupId>  
    <!--配置项目的一个模块-->
    <artifactId>hello-first</artifactId>
    <!--版本信息:这是一个快照版本-->
    <version>SNAPSHOT-0.0.1</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

说明:这里暂时不要管如何配置,我们先对maven有个直观的感受。
之后我们在工程01目录下运行命令:mvn test
此时我们会发现在target目录中多出了一些测试报告之类的文件,我们可以查看测试结果。当然我们如果使用命令:mvn clean可以将编译的文件全部删掉。

package cn.edu.zttc.world;
//这里我们是导入的之前的工程(01)
import cn.edu.zttc.hello.*;
public class World{
    public static void main(String[] args){
        Hello h = new Hello();
        h.sayHello("Jerry");
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<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>cn.edu.zttc.maven.hello</groupId>  
    <artifactId>hello-second</artifactId>
    <version>SNAPSHOT-0.0.1</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>cn.edu.zttc.maven.hello</groupId>
            <artifactId>hello-first</artifactId>
            <version>SNAPSHOT-0.0.1</version>
            <scope>complie</scope>
        </dependency>
    </dependencies>
</project>
public String hello(){
    return “Hello”;
}

这时我们只需要使用命令:mvn clean install重新清理、编译、打包、发布。在02工程中我们就可以直接使用此方法:h.hello();然后再使用命令:mvn clean package即可。

三、使用MyEclipse创建mavne工程

上一篇 下一篇

猜你喜欢

热点阅读