Maven学习小结(二 项目构建过程)
1.创建Maven项目
1.1 创建Maven项目的约定目录结构
1.2 编辑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.test.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- 需要依赖junit 通过 groupId+artifactId+version来查找,如果本地没有则到中央仓库下载 --> <dependency> <!-- 当前jar所属的命名空间 --> <groupId>junit</groupId> <!-- 依赖的项目模块的名称 --> <artifactId>junit</artifactId> <!-- 依赖的版本号 --> <version>4.9</version> <!-- 依赖的范围, 有 test compile privlege --> <scope>test</scope> </dependency> </dependencies> </project>
1.3创建java文件
//MavenTest1\src\main\java\cn\test\maven package cn.test.maven; public class Hello { public String sayHello(String name){ return "Hello "+name+" !"; } }
1.4创建测试文件
//MavenTest1\src\test\java\cn\maven package cn.maven; import cn.test.maven.Hello; import org.junit.Test; import static junit.framework.Assert.*; public class HelloTest{ @Test public void testHello(){ Hello hello =new Hello(); String results=hello.sayHello("Maven"); assertEquals("Hello Maven !",results); } }
2.使用Maven编译、清理、测试、打包、部署项目
2.1使用“mvn compile”编译项目,Maven会根据pom.xml中的添加的“junit”依赖到Maven仓库下载对应的jar文件。编译后生成target文件夹,和之前的一致。
2.2 使用“mvn clean”清理编译后的文件。
2.3 使用”mvn test”测试项目:
会在项目根目录下生成target文件夹,里面有classes目录和test-classes;也就是说“mvn test”是先编译再测试;
2.4使用“mvn package”打包项目
成功后在target下有Hello-0.0.1-SNAPSHOT.jar文件
2.5使用“mvn install”部署项目
部署成功后,在target下有Hello-0.0.1-SNAPSHOT.jar文件
同时,在本地仓库也有一份Hello-0.0.1-SNAPSHOT.jar文件
所以“mvn install”就是把用Maven来部署项目的
清理—>编译—>测试—>打包这几个步骤一起执行,并把创建的jar文件发布到本地Maven仓库。
3.在其他项目中使用通过Maven部署生成的jar包
3.1编辑pom.xml文件,和使用“junit”依赖一样;
<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.test.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- 需要依赖junit 通过 groupId+artifactId+version来查找,如果本地没有则到中央仓库下载 --> <dependency> <!-- 当前jar所属的命名空间 --> <groupId>junit</groupId> <!-- 依赖的项目模块的名称 --> <artifactId>junit</artifactId> <!-- 依赖的版本号 --> <version>4.9</version> <!-- 依赖的范围, 有 test compile privlege --> <scope>test</scope> </dependency> <dependency> <groupId>cn.test.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> </project>
3.1创建java文件
//MavenTest1\src\main\java\cn\maven package cn.maven; import cn.test.maven.Hello; public class HelloWorld { public String sayHi(String name){ Hello hello=new Hello(); String str=hello.sayHello(name) + " how are you ?"+this.getMyName()+",said"; return str; } public String getMyName(){ return "007"; } }
创建测试文件:
//MavenTest1\src\test\java\cn\maven package cn.maven; import cn.test.maven.Hello; import org.junit.Test; import static junit.framework.Assert.*; public class HelloWorldTest{ @Test public void testHelloWorld(){ HelloWorld helloWorld =new HelloWorld(); String results=helloWorld.sayHi("Dr.Q"); assertEquals("Hello Dr.Q ! how are you ?007,said",results); } }
使用“mvn package”测试成功: