maven之clean、install命令
一、maven之helloworld案例
1. maven目录结构
src
-main
-java
-package
-test
-java
-package
-resources
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2. 新建目录
在任意指定盘下建文件夹
我的目录结构为:D:\code\maven
按maven的目录机构构建文件件,如下图
3.D:\code\maven\src\main\Java\com\maven下建HelloWorld.java
package com.maven;
public class HelloWorld{
public String sayHello(){
return "hello world!";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
4. D:\code\maven\src\test\java\com\maven下建HelloWorldTest.java
package com.maven;
import org.junit.*;
import org.junit.Assert.*;
public class HelloWorldTest{
@Test
public void testHello(){
Assert.assertEquals("hello world!",new HelloWorld().sayHello());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
5、D:\code\maven下建pom.xml文件,同src根目录同级
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven</groupId>
<artifactId>maven-model</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
住:
com.maven为我的工程目录(D:\code\maven)
junit
junit
4.10
test
为单元测试依赖
6、进入命令行,进入D:\code\maven目录
执行mvn compile(第一次执行会下载各种插件、依赖包慢点)
执行mvn test
二. maven clean、maven install
(1)进入到maven根目录,执行mvn compile命令会在根目录生成target文件
(2)执行mvn clean可将根目录下生成的target文件移除
(3)mvn install命令
首先同maven之helloworld案例一样,新建maven2项目
Speak.java
package com.maven;
import com.maven.HelloWorld;
public class Speak{
public String sayHi(){
return new HelloWorld().sayHello();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
在maven2-src-test-java-com-maven创建测试类SpeakTest.java
package com.maven;
import org.junit.*;
import org.junit.Assert.*;
public class SpeakTest{
@Test
public void testSayHi(){
Assert.assertEquals("hello world!",new Speak().sayHi());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
pom.xml代码
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven2</groupId>
<artifactId>maven-model2</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
进到maven2目录下执行mvn compile命令,报错如下(找不到HelloWorld类):
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/code/maven2/src/main/java/com/maven2/Speak.java:[3,17] 找不到符号
符号: 类 HelloWorld
位置: 程序包 com.maven
[ERROR] /D:/code/maven2/src/main/java/com/maven2/Speak.java:[7,28] 找不到符号
符号: 类 HelloWorld
位置: 类 com.maven.Speak
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
此时,我们进入maven项目的根目录maven下,执行mvn install命令,将包含HelloWorld类的jar文件发布到本地仓库中。
maven本地默认仓库路径
接着配置pom.xml,添加新的依赖,将maven项目的坐标引用进来
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven2</groupId>
<artifactId>maven-model2</artifactId>
<version>0.0.2</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.maven</groupId>
<artifactId>maven-model</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
此时,进入maven2工程根目录maven2,执行mvn compile命令,提示项目构建成功,如下
[INFO] Compiling 1 source file to D:\code\maven2\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.548 s
[INFO] Finished at: 2016-06-06T21:13:05+08:00
[INFO] Final Memory: 13M/155M
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
执行mvn test顺利通过
注:maven通过install将本地工程打包成jar包,放入到本地仓库中,再通过pom.xml配置依赖引入到当前工程。
pom.xml中引入的坐标首先在本地maven仓库中查找,若没有则去maven的网上中央仓库查找,并放到本地仓库供项目使用。
打包:mvn package
编译:mvn compile
编译测试程序:mvn test-compile
清空:mvn clean
运行测试:mvn test
生成站点目录: mvn site
生成站点目录并发布:mvn site-deploy
安装当前工程的输出文件到本地仓库: mvn install