先采用记事本方式,熟悉一下Maven项目的文件结构。
新建一个文件夹:HelloWorld 按照如下的结构
package com.java.helloWorld;
public class HelloWorld{
public void sayHello(){
System.out.println("Hello World!!!");
}
}
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.java1234.HelloWorld</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
</dependencies>
</project>
modelVersion:POM 模型版本 4.0.0 固定
groupId:一般指某个公司或者某个组织的某个项目 比如 org.springframework
artifactId:一般指某个具体项目的某个具体模块 比如 spring-context Version:项目的版本
<dependencies></dependencies>:依赖。
用Maven运行这个项目:
常见的命令:
Mvn compile 编译
Mvn clean 清空
Mvn test 测试
Mvn package 打包
先编译:mvn compile
会把一些基本的东西从远程仓库下载到了本地仓库:
如果想迁出默认的地址,把本地仓库迁移到别的地方:需要改一下setting文件。
<localRepository>D:\maven\repository</localRepository>
编译好的文件在刚生成的target中
mvc clean 是把目标文件删除
我们在Test文件夹下也按照上面的结构建测试文件:
package com.java.helloWorld;
import org.junit.Test;
public class HelloWorld{
@Test
public void testSayHello(){
HelloWorld helloWorld = new HelloWorld;
helloWorld.sayHello();
}
}
引入了junit,在pom.xml加入依赖:
去中央仓库找:http://mvnrepository.com/, 以后我们找jar包都要去这里找
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
命令mvn test:
mvn package: