【问题标题】:Apache Storm - How to exclude storm jar in Maven in production clusterApache Storm - 如何在生产集群中的 Maven 中排除 Storm jar
【发布时间】:2018-07-12 22:47:58
【问题描述】:

根据documentation,当我们将 Storm 拓扑部署到生产集群时,我们必须从 Maven 中排除 Storm 的 jar,因为它已经在类路径中,如果我们不这样做,将会像multiple default.yaml in classpath 这样的错误。

那么我们该怎么做呢?文档提供了一些细节,但不够清楚。当我们配置 maven 并构建 jar 时,仍然包含 org.apache.storm jar,我们在 jar 中有一个 default.yaml,实际上,如果我们在构建中打开调试输出,我们会看到如下警告:

[WARNING]The following patterns were never triggered in this artifact inclusion filter: o 'org.apache.storm:storm-core:jar:1.1.1'.

将依赖org.apache.storm的作用域改成provided,还是不行。

<dependency>
    <groupId>org.apache.storm</groupId>
    <artifactId>storm-core</artifactId>
    <type>jar</type>
    <version>1.1.1</version>
    <scope>provided</scope>
</dependency>

【问题讨论】:

    标签: java maven apache-storm


    【解决方案1】:

    documentation of Storm 告诉我们排除 Storm 依赖并提供 a link to a page of Maven,它解释了如何执行此操作,但缺少完整示例。事实证明,我们必须创建另一个 XML 文件作为程序集配置的描述符,而不是将配置直接放在 pom.xml 中,即使 Eclipse 不会抱怨将两个文件放在一起。

    并且,在第二个 xml 文件中,范围应该是 compile 而不是 runtime

    方法如下:

    我们有 pom.xmlassembly 插件,指向另一个描述符 xml 文件:

    <plugin>                                                                                
        <artifactId>maven-assembly-plugin</artifactId>                                      
        <executions>                                                                        
            <execution>                                                                     
                <id>make-assembly</id> <!-- this is used for inheritance merges -->         
                <phase>package</phase> <!-- bind to the packaging phase -->                 
                <goals>                                                                     
                    <goal>single</goal>                                                     
                </goals>                                                                    
            </execution>                                                                    
        </executions>                                                                       
        <configuration>                                                                     
            <descriptors>                                                                   
                <descriptor>/src/main/resources/exclude-storm.xml</descriptor>              
            </descriptors>                                                                  
            <archive>                                                                       
                <manifest>                                                                  
                    <mainClass>path.to.main.class</mainClass> 
                </manifest>                                                                 
            </archive>                                                                      
            <descriptorRefs>                                                                
                <descriptorRef>jar-with-dependencies</descriptorRef>                        
            </descriptorRefs>                                                               
        </configuration>                                                                    
    </plugin>     
    

    注意部分:(应该是完整路径)

    <descriptors>                                                                   
        <descriptor>/src/main/resources/exclude-storm.xml</descriptor>              
    </descriptors>
    

    在这条路上:

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
        <id>exclude-storm</id>
        <formats>
            <format>jar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <outputDirectory>/</outputDirectory>
                <useProjectArtifact>false</useProjectArtifact>
                <unpack>true</unpack>
                <scope>compile</scope>   <!-- note here!!!! -->
                <excludes>
                    <exclude>org.apache.storm:storm-core:jar:1.1.1</exclude>
                </excludes>
            </dependencySet>
        </dependencySets>
        <fileSets>
            <fileSet>
                <outputDirectory>/</outputDirectory>
                <directory>${project.build.outputDirectory}</directory>
            </fileSet>
        </fileSets>
    </assembly>
    

    这里我们添加 Maven 文档页面的设置。

    最重要的是:排除格式:应该是:(ref)

    groupId:artifactId:type[:classifier]:version
    

    还有范围! runtime 是默认的,我把它改成compile 就可以了。

    最后,编译时使用:

    clean assembly:assembly
    

    并打开调试输出以在控制台中查看完整输出。如果你:

    • 构建成功
    • 搜索输出并没有找到类似的东西:[WARNING]The following patterns were never triggered in this artifact inclusion filter: o 'org.apache.storm:storm-core:jar:1.1.1'.
    • 罐子里没有default.yaml

    那你就知道你成功了。

    感谢另一个问答的启发:How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

    【讨论】:

    • 而且,如果您运行 storm jar jar-file main-class-path 并失败并出现 cannot load main class 错误,请将 storm 更改为 Storm 可执行文件的完整路径,并将 jar-file 更改为完整路径。
    【解决方案2】:

    我很高兴您知道如何使用程序集插件来做到这一点。一个更简单的解决方案是使用 shade 插件来创建你的 fat jar。在 Storm 示例目录(也包含在 Storm 二进制发行版中)中有完整的如何配置 pom 的示例

    例如见https://github.com/apache/storm/blob/80cc88112bf4fab34571ebff03782b759d112288/examples/storm-kafka-client-examples/pom.xml#L94

    当您使用 shade 插件时,在创建 fat jar 时将尊重提供的storm-core 范围。

    【讨论】:

    • 感谢您的评论。有了fat jar,会不会有stackoverflow.com/questions/1729054/…提到的问题?我的意思是,class files with same path overwriting each other?
    • 是的,但是无论您使用的是汇编插件还是阴影,这都会成为一个问题。他们都生产了一个胖罐子。
    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 2013-03-24
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    相关资源
    最近更新 更多