Hemant Rajput
Hemant Rajput

Reputation: 259

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.

Upvotes: 2

Views: 2461

Answers (2)

WesternGun
WesternGun

Reputation: 12787

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?

Upvotes: 0

David
David

Reputation: 11

I use Gradle for compiling the JAR files for topologies. It allows you to exclude certain files when generating JAR files.

The example below shows the set-up that I use in my build.gradle file

   apply plugin: 'java'
    apply plugin: 'eclipse'


    configurations {
        provided
        compile.extendsFrom provided
    }

    jar {
        dependsOn configurations.runtime

        from {
            (configurations.runtime - configurations.provided).collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        manifest {
            attributes 'Main-Class': 'com.example.myclass'
        }
    }

    dependencies {
        provided files('./lib/asm-4.0.jar')
        provided files('./lib/carbonite-1.4.0.jar')
        # ... The rest of the Storm jars found in the lib directory of your storm installation
    }

By default the directory structure Gradle expects is

MyProjectName
    - build.gradle
    - src
       - main
            - java
                - [Your Java Files]
            - resources
                - resources
                    - [Mutlilang files / other resources]

From the command line when you run gradle build in the directory containing your build.gradle file a JAR file should be generated under .\build\libs

There is also a Gradle plugin for eclipse

Upvotes: 1

Related Questions