maven使用.03.普通项目和多模块项目

来源:互联网 发布:whois域名查询 编辑:程序博客网 时间:2024/05/17 01:00

上两个POST将概念讲述的非常完备。并在第一篇POST中,建立了一个Hello World程序,那么这篇日志会在第一篇的基础上,实际的写一些代码,并通过maven进行测试。

编写一个简单地类

使用你喜欢的IDE打开之前新建的Hello world程序,在src/main/java目录下,添加对应的包和类。即可向程序中添加一个类。例如,添加如下的类

  1. package me.reyoung.mvntest;
  2. public class SimpleAdder {
  3. public static int add(int a,int b){
  4. return a+b;
  5. }
  6. }

并在src/test/java目录下,添加对应的测试文件。例如添加如下的测试类:

  1. package me.reyoung.mvntest;
  2. import junit.framework.TestCase;
  3. public class SimpleAdderTest extends TestCase {
  4. public void testAdd() throws Exception {
  5. assertEquals(3,SimpleAdder.add(1,2));
  6. assertEquals(5,SimpleAdder.add(2,3));
  7. assertEquals(0,SimpleAdder.add(0,0));
  8. }
  9. }

值得注意的是,在test目录下的所有测试,应该符合你所使用的测试框架的一些标准。比如,在默认情况下,maven会引入junit3。所以,每一个该目录下的文件,在默认情况下,均应该符合junit3 标准。

运行测试

写完之前的程序后,就可以在该项目的目录里,运行测试了。执行命令

  1. mvn test

程序就会输出一些编译过程的信息,在这些信息中,有一些测试结果的信息,类似于:

  1. -------------------------------------------------------
  2. T E S T S
  3. -------------------------------------------------------
  4. Running me.reyoung.mvntest.SimpleAdderTest
  5. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.025 sec
  6. Results :
  7. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

并且可以在target/surefire-reports中,看到这次测试的报告。

多模块的maven项目

Maven项目可以是多模块的项目,这样子的话,各个子项目(模块)之间可以互相依赖,而在根项目中,可以根据这些依赖自动的按照顺序构建各个的子项目。

一个简单地多模块项目的

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/maven-v4_0_0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.sonatype.mavenbook.multi</groupId>
  7. <artifactId>simple-parent</artifactId>
  8. <packaging>pom</packaging>
  9. <version>1.0</version>
  10. <name>Multi Chapter Simple Parent Project</name>
  11. <modules>
  12. <module>simple-weather</module>
  13. <module>simple-webapp</module>
  14. </modules>
  15. <build>
  16. <pluginManagement>
  17. <plugins>
  18. <plugin>
  19. <groupId>org.apache.maven.plugins</groupId>
  20. <artifactId>maven-compiler-plugin</artifactId>
  21. <configuration>
  22. <source>1.5</source>
  23. <target>1.5</target>
  24. </configuration>
  25. </plugin>
  26. </plugins>
  27. </pluginManagement>
  28. </build>
  29. <dependencies>
  30. <dependency>
  31. <groupId>junit</groupId>
  32. <artifactId>junit</artifactId>
  33. <version>3.8.1</version>
  34. <scope>test</scope>
  35. </dependency>
  36. </dependencies>
  37. </project>

可以看到,这个pom文件,和之前的hello word的pom文件有这么几点区别。

  • 这个项目文件的packaging是pom,因为这个项目是maven的多模块项目的主项目,而不会像之前一样,生成jar包
  • 这个项目有modules标签,而其中有两个子标签,simple-weather和simple-webapp,这两个就是分别的子项目。

NOTE: 在这个pom文件里,其他的设置,都是对这个项目的所有子项目的设置。例如,在这个pom中依赖junit,那么对于simple-weather和simple-webapp都会依赖与junit。

子项目simple-weather

simple-weather的pom文件如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/maven-v4_0_0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.sonatype.mavenbook.multi</groupId>
  8. <artifactId>simple-parent</artifactId>
  9. <version>1.0</version>
  10. </parent>
  11. <artifactId>simple-weather</artifactId>
  12. <packaging>jar</packaging>
  13. <name>Multi Chapter Simple Weather API</name>
  14. <build>
  15. <pluginManagement>
  16. <plugins>
  17. <plugin>
  18. <groupId>org.apache.maven.plugins</groupId>
  19. <artifactId>maven-surefire-plugin</artifactId>
  20. <configuration>
  21. <testFailureIgnore>true</testFailureIgnore>
  22. </configuration>
  23. </plugin>
  24. </plugins>
  25. </pluginManagement>
  26. </build>
  27. <dependencies>
  28. <dependency>
  29. <groupId>log4j</groupId>
  30. <artifactId>log4j</artifactId>
  31. <version>1.2.14</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>dom4j</groupId>
  35. <artifactId>dom4j</artifactId>
  36. <version>1.6.1</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>jaxen</groupId>
  40. <artifactId>jaxen</artifactId>
  41. <version>1.1.1</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>velocity</groupId>
  45. <artifactId>velocity</artifactId>
  46. <version>1.5</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.apache.commons</groupId>
  50. <artifactId>commons-io</artifactId>
  51. <version>1.3.2</version>
  52. <scope>test</scope>
  53. </dependency>
  54. </dependencies>
  55. </project>

可以看到这个pom.xml中,和之前的hello world的pom类似。里面也是具有依赖结构和maven的座标。唯一一个区别,就是多了一个parent标签。而这个parent标签就是指定这个项目的parent。

多模块项目的目录结构

  1. simple-parent
  2. |--- pom.xml
  3. |--- simple-weather
  4. | |--- pom.xml
  5. | |--- src
  6. | | |--- main
  7. | | `--- test
  8. | `--- target
  9. `--- simple-webapp
  10. |--- pom.xml
  11. |--- src
  12. | |--- main
  13. | `--- test
  14. `--- target

可以看出,对于多模块的maven项目,每个子项目放到相应的目录里。这样,如果要整体构建这个多模块项目,就可以使用 mvn compile 一下。

这样,对于maven多模块项目,就简单地介绍在这里。

引用

  • MultiModule