- 创建空目录
- F:jtDevelopmaventestmyapp
- 创建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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>myapp</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
-
- 创建目录
- F:jtDevelopmaventestmyappsrcmainjava
- 编写java文件
- F:jtDevelopmaventestmyappsrcmainjavaCal.java
-
public class Cal{ private String name; public void setName(String name){ this.name=name; } public String getName(){ return "Hello "+this.name; } public static void main(String[] args){ System.out.println("Hello Maven"); } }
-
使用Maven编译
-
F:jtDevelopmaventestmyapp>cd F:jtDevelopmaventestmyapp F:jtDevelopmaventestmyapp>mvn compile
[INFO] BUILD SUCCESS
-
- 编写测试类文件
- F:jtDevelopmaventestmyappsrc estjavaCalTest.java
-
import org.junit.BeforeClass; import org.junit.Test; import junit.framework.TestCase; public class CalTest { static Cal c=null; @BeforeClass public static void init(){ c=new Cal(); c.setName("jt"); } @Test public void testGetName(){ String v=c.getName(); TestCase.assertEquals("Hello jt", v); } }
- 运行测试
-
T E S T S ------------------------------------------------------- Running CalTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ---------------------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] ---------------------------------------------------------------------- [INFO] Total time: 2.124 s [INFO] Finished at: 2018-11-15T21:50:52+08:00 [INFO] ---------------------------------------------------------------------- F:jtDevelopmaventestmyapp>mvn test
-
- 打包
-
执行"mvn package"命令
- 多了一个文件 F:jtDevelopmaventestmyapp argetmyapp-1.0-SNAPSHOT.jar
-
- 安装
- 执行
执行"mvn install"命令
- 多了两个文件
- F:jtDevelopmaventest epositoryscommycompanyappmyapp1.0-SNAPSHOTmyapp-1.0-SNAPSHOT.jar
- F:jtDevelopmaventestmyapp argetmyapp-1.0-SNAPSHOT.jar
- 执行
- 在其他项目中使用本项目的jar包
- 新建maven项目
- 编辑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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>myapp2</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>com.mycompany.app</groupId> <artifactId>myapp1</artifactId> <version>1.0-SNAPSHOT</version> <scope>test</scope> </dependency> </dependencies> </project>
-