没有比人更高的山

maven中文教程——一个简单的应用实例

maven中文教程——一个简单的应用实例8.0101

我突然有个想法,我们使用hibernate search来构建一个全文检索(本文的内容不会重点放在hibernate search,对此感兴趣可以下载本文的代码)。假设我们的数据库中有一些数码相机的数据,对于这些数据的维护采用hibernate,这样我们可以使用hibernate search架设一个全文检索系统,以提供更好的产品检索服务。

心动不如行动,马上动手吧。

第一步,在命令行中敲入“mvn archetype:generate”,这个命令好处是可以不用我们记那么多的参数,在前面的文章中我已经说明了。

第二步,选择 “internal -> maven-archetype-quickstart ()”这一项,自需要选择编号,我这里的编号是17,这个号码在每个电脑上可能有不同。

第三部,按照提示输入相关信息,之后,maven即生成了项目的结构,如下:
Define value for groupId: : com.zhlwish
Define value for artifactId: : jpastudy
Define value for version:  1.0-SNAPSHOT: :
Define value for package:  com.zhlwish: : com.zhlwish.jpastudy
Confirm properties configuration:
groupId: com.zhlwish
artifactId: jpastudy
version: 1.0-SNAPSHOT
package: com.zhlwish.jpastudy
 Y: : y

第四步,修改pom.xml文件,增加依赖项,我们使用mysql数据,那么需要增加mysql的jdbc驱动依赖,我们在mvn的仓库(如:http://mvnrepository.com/)找到对应的版本。同样找到hibernate-core、hibernate-search,因为hibernate-search还依赖于hibernate-annotation,因此我们也要把这些项目加入到依赖中。整个文件变成如下:
<dependencies>
  <!– 测试 –>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>

  <!– MySQL 驱动 –>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
  </dependency>

  <!– Hibernate –>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.3.1.GA</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.4.0.GA</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-search</artifactId>
    <version>3.1.0.GA</version>
  </dependency>

  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.4.2</version>
  </dependency>

  <dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.4.GA</version>
  </dependency>

  <!– lucene –>
  <dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-snowball</artifactId>
    <version>2.4.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-analyzers</artifactId>
    <version>2.4.0</version>
  </dependency>
</dependencies>

第五步,加入代码,然后运行“mvn compile”编译程序,结果发现出现错误“(请使用 -source 5 或更高版本以启用泛型)”,因为我们用到了泛型和annotation,这些都是jdk5才具有的新特性,我们需要设置compile插件,具体可以参考Setting the -source and -target of the Java Compiler,根据说明,我们继续向pom文件中加入:
<build>
   <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>5</source>
          <target>5</target>
        </configuration>
      </plugin>
   </plugins>
</build>

第六步,运行“mvn complie”,这次编译成功过了哦。运行“mvn package”,在target目录下生成了jpastudy-1.0-SNAPSHOT.jar,但是我们还不能直接运行这个jar,因为所有的运行时依赖没有加入到classpath中。

第七步,我们想将所有的依赖库都打包,直接交给用户,这样用户不需要在做其他设置了,这里需要使用Assembly插件了,其说明参考Pre-defined Descriptor Files,这个参考文件也说明了有四种默认定义的打包方式,我们选择jar-with-dependencies,继续添加pom文件,在<plugins>节点下添加如下内容:
<plugin>
   <artifactId>maven-assembly-plugin</artifactId>
   <configuration>
      <descriptorRefs>
         <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
   </configuration>
</plugin>

第八步,首先清理一下前面生成的一些东西,运行“mvn clean”,然后运行“mvn assembly:assembly”(具体请参见本系列文章第一篇),在target目录中生成了jpastudy-1.0-SNAPSHOT-jar-with-dependencies.jar。好!

第九步,可以来看我们的成果了,打开MySQL数据库,运行“java -cp target/jpastudy-1.0-SNAPSHOT-jar-with-dependencies.jar com.zhlwish.jpastudy.App”,输出结果了吧:
一共检索到3条数据:
产品名称 | 产品型号
Cannon   | 1000D
Cannon   | 450D
Cannon   | 500D

我的工作完成了,大家只需要从google code的svn上checkout代码,然后运行第八步和第九步的命令就行了,把包的依赖关系和生命周期交给maven吧。本文代码svn地址:http://zhlwish.googlecode.com/svn/trunk/

VN:F [1.7.5_995]
Rating: 8.0/10 (1 vote cast)
VN:F [1.7.5_995]
Rating: 0 (from 0 votes)

No Comments yet »

发表评论:

Switch to our mobile site