一、Maven工程文件pom.xml的编写
Maven通过pom.xml进行代码文件搜寻、包依赖处理,需要注意的是:
-
设置项目的文件编码和所使用的JDK版本
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
-
设置编译采用的插件工具,这里推荐采用
maven-assembly-plugin<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>cn.com.superengine.AppMainTopologyRunner</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>如果采用的是
maven-jar-plugin编译生成的jar不带依赖包<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <mainClass>cn.com.superengine.AppMainTopologyRunner</mainClass> </manifest> </archive> </configuration> </plugin>执行出现以下结果:
Error: Unable to initialize main class cn.com.superengine.AppMainTopologyRunner Caused by: java.lang.NoClassDefFoundError: org/apache/storm/topology/IRichSpout`
-
需要指定依赖的
storm插件版本,不同的版本引用方式不同:-
storm 最新正式版
import org.apache.storm.*;<dependency> <groupId>org.apache.storm</groupId> <artifactId>storm-core</artifactId> <version>1.1.1</version> </dependency>
-
storm兼容版
import backtype.storm.*;<dependency> <groupId>org.apache.storm</groupId> <artifactId>storm-core</artifactId> <version>0.9.3</version> </dependency>
-
二、项目中Package和Class文件的创建顺序
- 使用File → New Project → Maven (此处不用使用Maven内置模板创建) → Next … → 完成
- 在
src/main/java下新建项目的包信息,例如cn.com.superengine - 在
cn.com.superengine目录下新建Topology主文件 - 在
cn.com.superengine目录下新建topology文件夹 - 在topology文件夹内新建对应的Class,实现Spout和相应Bolts
- 如有外部链接需要,可以建立service文件夹,编写相应的外部接口服务类
三、本地资源文件的加载
- 使用
ClassLoader.getSystemResourceAsStream("baidutop.txt")就能加载src/main/java/resource下的资源文件。 - 同时注意Spout实现中的
outputCollector消息的传递











网友评论