1、进入到maven根目录,执行mvn compile命令会在根目录生成target文件(参照maven之helloworld案例),如下图:

2、执行mvn clean可将根目录下生成的target文件移除
3、mvn install命令
首先同maven之helloworld案例一样,新建maven2项目

Speak.java代码

package com.maven;import com.maven.HelloWorld;publicclass Speak{
public String sayHi(){
returnnew HelloWorld().sayHello();
}
}


SpeakTest.java代码

package com.maven;importorg.junit.*;importorg.junit.Assert.*;publicclass SpeakTest{
@Test
publicvoid testSayHi(){
Assert.assertEquals("hello world!",new Speak().sayHi());
}
}

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>

进到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

此时,我们进入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>

此时,进入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

执行mvn test顺利通过
注:maven通过install将本地工程打包成jar包,放入到本地仓库中,再通过pom.xml配置依赖引入到当前工程。
pom.xml中引入的坐标首先在本地maven仓库中查找,若没有则去maven的网上中央仓库查找,并放到本地仓库供项目使用。
网友评论