1
若想让maven项目依赖另外一个maven项目。被依赖的项目要在maven仓库中有对应的jar包,所以要对依赖的项目运行mvninstall命令。
2
新建第二个项目模块HelloFriend文件夹及约定的文件夹结构
HelloFriend --src -----main ----------java ----------resources -----test ---------java ---------resources --pom.xml |
3
在项目HelloFriend根文件夹建立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 <modelVersion>4.0.0</modelVersion> <groupId>cn.toto.maven</groupId> <artifactId>HelloFriend</artifactId> <version>0.0.1-SNAPSHOT</version> <name>HelloFriend</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>cn.toto.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> </project> |
4
在src/main/java/cn/toto/maven文件夹下新建文件HelloFriend.java文件
import cn.toto.maven.Hello; public class HelloFriend { public String sayHelloToFriend(String name){ Hello hello = new Hello(); String str = hello.sayHello(name)+" I am "+this.getMyName(); System.out.println(str); return str; } public String getMyName(){ return "John"; } } |
5
在/src/test/java/cn/toto/maven文件夹下新建測试文件HelloFriendTest.java
import static junit.framework.Assert.assertEquals; import org.junit.Test; import cn.toto.maven.Hello; public class HelloFriendTest { @Test public void tesHelloFriend(){ HelloFriend helloFriend = new HelloFriend(); String results = helloFriend.sayHelloToFriend("tuzuoquan"); assertEquals("Hello tuzuoquan! I am John",results); } } |
6
在HelloFriend文件夹下运行命令mvn命令(注意到HelloFriend文件夹)
7
又一次在HelloFriend文件夹下运行命令mvnpackage
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">