10.1 account-captcha

在讨论maven-surefire-plugin之前,本章先介绍实现账户注册服务的account-captcha模块,该模块负责处理账户注册时验证码的key生成、图片生成以及验证等。读者可以回顾第4章的背景案例以获得更具体的需求信息。

10.1.1 account-captcha的POM

该模块的POM(Project Object Model,项目对象模型)还是比较简单的,内容见代码清单10-1。

代码清单10-1 account-captcha的POM


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

<parent>

<groupId>com.juvenxu.mvnbook.account</groupId>

<artifactId>account-parent</artifactId>

<version>1.0.0-SNAPSHOT</version>

</parent>

<artifactId>account-captcha</artifactId>

<name>Account Captcha</name>

<properties>

<kaptcha.version>2.3</kaptcha.version>

</properties>

<dependencies>

<dependency>

<groupId>com.google.code.kaptcha</groupId>

<artifactId>kaptcha</artifactId>

<version>${kaptcha.version}</version>

<classifier>jdk15</classifier>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

</dependency>

</dependencies>

<repositories>

<repository>

<id>sonatype-forge</id>

<name>Sonatype Forge</name>

<url>http://repository.sonatype.org/content/groups/forge/</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>false</enabled>

</snapshots>

</repository>

</repositories>

</project>


首先POM中的第一部分是父模块声明,如同account-email、account-persist一样,这里将父模块声明为account-parent。紧接着是该项目本身的artifactId和名称,groupId和version没有声明,将自动继承自父模块。再往下声明了一个Maven属性kaptcha.version,该属性用在依赖声明中,account-captcha的依赖除了SpringFramework和JUnit之外,还有一个com.google.code.kaptcha:kaptcha。Kaptcha是一个用来生成验证码(Captcha)的开源类库,account-captcha将用它来生成注册账户时所需要的验证码图片,如果想要了解更多关于Kaptcha的信息,可以访问其项目主页:http://code.google.com/p/kaptcha/。

POM中SpringFramework和JUnit的依赖配置都继承自父模块,这里不再赘述。Kaptcha依赖声明中version使用了Maven属性,这在之前也已经见过。需要注意的是,Kaptcha依赖还有一个classifier元素,其值为jdk5,Kaptcha针对Java 1.5和Java 1.4提供了不同的分发包,因此这里使用classifier来区分两个不同的构件。

POM的最后声明了Sonatype Forge这一公共仓库,这是因为Kaptcha并没有上传到中央仓库,我们可以从Sonatype Forge仓库获得该构件。如果有自己的私服,就不需要在POM中声明该仓库了,可以代理Sonatype Forge仓库,或者直接将Kaptcha上传到自己的仓库中。

最后,不能忘记把account-captcha加入到聚合模块(也是父模块)account-parent中,见代码清单10-2。

代码清单10-2 将account-captcha加入到聚合模块account-parent


<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.juvenxu.mvnbook.account</groupId>

<artifactId>account-parent</artifactId>

<version>1.0.0-SNAPSHOT</version>

<packaging>pom</packaging>

<name>Account Parent</name>

<modules>

<module>account-email</module>

<module>account-persist</module>

<module>account-captcha</module>

</modules>

</project>