用maven2重写zookeeper客户端测试程序

之前的zk_client.java用一种很取巧的办法来编译和运行,把jar包解压放到当前目录下。这种办法不太好,也不美观。

现在使用maven2重构整个代码。

1.首先运行create_mvn_project.sh com.luoyan.zookeepertest zookeepertest,本质上是运行以下的命令:

mvn archetype:generate \

-DarchetypeGroupId=org.apache.maven.archetypes \

-DgroupId=com.luoyan.zookeepertest \

-DartifactId=zookeepertest

 

该命令会自动生成pom.xmlsrc下的代码。

2.然后把原来的zk_client.java拷贝到src/main/java/com/luoyan/zookeepertest

3.pom.xml中加上

<build>

<plugins>

<!–

Bind the maven-assembly-plugin to the package phase

this will create a jar file without the storm dependencies

suitable for deployment to a cluster.

–>

<plugin>

<artifactId>maven-assembly-plugin</artifactId>

<configuration>

<descriptorRefs>

<descriptorRef>jar-with-dependencies</descriptorRef>

</descriptorRefs>

<archive>

<manifest>

<mainClass></mainClass>

</manifest>

</archive>

</configuration>

<executions>

<execution>

<id>make-assembly</id>

<phase>package</phase>

<goals>

<goal>single</goal>

</goals>

</execution>

</executions>

</plugin>

</plugins>

</build>

 

4.在zk_client.java中加入package com.luoyan.zookeepertest;

5.添加依赖关系,主要是依赖zookeeper 3.4.5

<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>3.4.5</version>

</dependency>

如果直接运行mvn package,会报错

The following artifacts could not be resolved: javax.jms:jms:jar:1.1, com.sun.jdmk:jmxtools:jar:1.2.1, com.sun.jmx:jmxri:jar:1.2.1: Failure to find javax.jms:jms:jar:1.1 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]

所以要在zookeeper的依赖关系中再加上exclusion

 

<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>3.4.5</version>

<exclusions>

<exclusion>

<groupId>com.sun.jmx</groupId>

<artifactId>jmxri</artifactId>

</exclusion>

<exclusion>

<groupId>com.sun.jdmk</groupId>

<artifactId>jmxtools</artifactId>

</exclusion>

<exclusion>

<groupId>javax.jms</groupId>

<artifactId>jms</artifactId>

</exclusion>

</exclusions>

</dependency>

 

6.运行mvn package生成 jar

7.java -cp target/zookeepertest-1.0-SNAPSHOT-jar-with-dependencies.jar com.luoyan.zookeepertest.zk_client

就可以执行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值