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

来源:互联网 发布:中信银行淘宝卡 编辑:程序博客网 时间:2024/06/06 16:34

之前的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
原创粉丝点击