maven之clean、install命令

xiaoxiao2021-02-28  14

一、maven之helloworld案例

1. maven目录结构

src -main -java -package -test -java -package -resources 12345678

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!"; } } 1234567

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()); } } 1234567891011

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> 1234567891011121314151617181920

住: 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(); } } 123456789

在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()); } } 1234567891011

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> 123456789101112131415161718

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

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

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

执行mvn test顺利通过

注:maven通过install将本地工程打包成jar包,放入到本地仓库中,再通过pom.xml配置依赖引入到当前工程。

  pom.xml中引入的坐标首先在本地maven仓库中查找,若没有则去maven的网上中央仓库查找,并放到本地仓库供项目使用。

转载请注明原文地址: https://www.6miu.com/read-2250113.html

最新回复(0)