`
wanghaisheng
  • 浏览: 89907 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

maven学习6——定制pom

阅读更多

定制 POM

Maven 2 通过该 pom.xml 文件了解您的项目。该文件由 Archetype 按照 NumOps 生成,如清单 8 所示:


清单 8. Archetype 生成的 POM - 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ibm.devworks</groupId>
  <artifactId>NumOps</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 

请注意:在测试阶段中(通过 <scope> 标记),Archetype 如何定义模块的坐标、如何将类型定义为 JAR 归档以及如何将 JUnit 指定为一个依赖项。要为您的新项目定制这个 pom.xml 文件,请参照清单 9 中突出显示的部分,稍作改动。


清单 9. 为 NumOps 项目定制生成的 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ibm.devworks</groupId>
  <artifactId>NumOps</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Intro to Maven 2 Example 1</name>
                      <url>http://www.ibm.com/java</url>
                       <build>
                        <plugins>
                          <plugin>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <configuration>
                              <source>1.5</source>
                              <target>1.5</target>
                            </configuration>
                           </plugin>
                        </plugins>
                      </build>
        <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 

额外的 <build> 标记是必要的,用来覆盖源代码,以达到 Java 代码的水平。默认情况下,采用的是 JDK 1.4,但您的代码使用了泛型,因而需要 JDK 5.0 编译。

编译定制的项目

现在可以使用 mvn compile 命令编译 NumOps 项目。 这个命令使 Maven 2 引擎从构建生命周期一直运行到编译阶段,并执行相应的 mojo。您应该看到构建成功的报告,报告中在目标目录树里创建了三个类文件(如清单 10 所示)。如果这是第一次运行,那会花点时间,因为一些依赖项需要经过 Internet 从中央存储库下载。


清单 10. NumOps 项目中 mvn 编译的输出

                    
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Intro to Maven 2 Example 1
[INFO]    task-segment: [compile]
[INFO] -------------------------------------------------------------------------
---
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
Compiling 3 source files to C:\temp\maven\NumOps\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sat Dec 02 22:52:16 EST 2006
[INFO] Final Memory: 3M/7M
[INFO] ------------------------------------------------------------------------

 


添加单元测试

项目开发的最佳实践通常要求对所有代码模块进行单元测试。Maven 2 为您创建了一个占位符 AppTest.java 单元测试文件。现在,将文件名改为 NumOpsTest.java,请参照清单 11 中突出显示的部分,对生成的单元测试做出改动。也可以从源代码下载处复制单元测试的源代码(参见 下载)。


清单 11. 为项目添加 NumOpsTest 单元测试

                    
package com.ibm.devworks;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class NumOpsTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public NumOpsTest( String testName )
    {
        super( testName );
    }

    ...

    public void testNumOps()
                        {
                        NumOps nops = new NumOps();
                        assertTrue( nops.size() == 1);
                            assertTrue( 
                             nops.getOp(0).getDesc().equals("plus"));
                            assertTrue( nops.getOp(0).op(2,1) == 3);
                    
                        }
}

 

现在可以使用 mvn test 命令运行所有的 mojo 一直到测试阶段。

Maven 2 编译源码和单元测试。然后运行测试,同时报告成功、失败和错误的数目,如清单 12 所示:


清单 12. 执行 mvn 测试来编译项目和运行单元测试

                    
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Intro to Maven 2 Example 1
[INFO]    task-segment: [test]
[INFO] -------------------------------------------------------------------------
---
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
Compiling 1 source file to C:\temp\maven\NumOps\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\temp\maven\NumOps\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.ibm.devworks.NumOpsTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec

Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Sat Dec 02 23:04:27 EST 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics