首页 > 代码库 > 3.将maven项目jar纳入maven仓库,Mave项目依赖另外一个Maven项目的案例

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>

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

4src/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);             

 

        }

}

6HelloFriend目录下执行命令mvn命令(注意到HelloFriend文件夹)

7重新在HelloFriend目录下执行命令mvnpackage

 

3.将maven项目jar纳入maven仓库,Mave项目依赖另外一个Maven项目的案例