3.將maven項目jar納入maven倉庫,Mave項目依賴另外一個Maven項目的案例
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <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>
<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文件
package cn.toto.maven;
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
package cn.toto.maven;
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
3.將maven項目jar納入maven倉庫,Mave項目依賴另外一個Maven項目的案例