首页 > 代码库 > 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>

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项目的案例