Search code examples
mavenapache-storm

How to Exclude Certain Jar Dependencies while creating jar without using maven?


I am working on a core Java project. I am writing an Apache Storm topology and need to exclude storm jars while binding the topology into jar. Is there any way to do this without using maven? I know with maven we can use <scope>provided</scope> but I need an alternative to this.

PS: I am using Eclipse.


Solution

  • If you are using Maven instead of Gradle, and you come here for excluding lib of Storm in building, I have the solution and a working example.

    The documentation of Storm tells us to exclude the Storm dependency and provides a link to a page of Maven, which explains how to do it but it lacks a full example. It turns out that we have to create another XML file as the descriptor of assembly configuration, instead of putting the configuration directly in the pom.xml, even when Eclipse does not complain about putting the two files together.

    Here is how:

    We have our pom.xml with assembly plugins, pointing to another descriptor xml file:

    <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>     
    

    Note the part of: (should be complete path)

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

    And in this path:

    <?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>
    

    Here we add the settings of Maven doc page.

    The most important thing: format of exclusion: should be: (ref)

    groupId:artifactId:type[:classifier]:version
    

    And the scope! runtime is default, I changed it to compile and it works.

    At last, when you compile, use:

    clean assembly:assembly
    

    And turn on debug output to see full output in console. If you:

    • have a successful build
    • search the output and haven't found anything like: [WARNING]The following patterns were never triggered in this artifact inclusion filter: o 'org.apache.storm:storm-core:jar:1.1.1'.
    • the jar contains no default.yaml

    Then you know you have succeeded.

    Thanks for the inspiration of another question and answers: How to exclude dependencies from maven assembly plugin : jar-with-dependencies?