0

I have very minimal knowledge of Java, coming from a mainly Python background, and I was wondering if it is possible to include the modules that I used with a .jar?

For example, my program uses Selenium Webdriver and Selenium Chromedriver, is it possible for me to make it so that my user doesn't need to have these installed?

Thank you and sorry if this has been asked before!

olyv
  • 3,699
  • 5
  • 37
  • 67

2 Answers2

0

You can include resources in your jar See packaging using maven or Ant

This will work for the Selenium dependency,but the ChromeDriver is an executable, so you will have to extract it before using it, see example here: Run *.exe file from inside JAR

Community
  • 1
  • 1
Ittiel
  • 1,104
  • 9
  • 12
0

If you are using maven then you can use maven-shade-plugin and you can configure what you like to include and exclude from library in your project build jar

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <filters>
            <filter>
              <artifact>junit:junit</artifact>
              <includes>
                <include>junit/framework/**</include>
                <include>org/junit/**</include>
              </includes>
              <excludes>
                <exclude>org/junit/experimental/**</exclude>
                <exclude>org/junit/runners/**</exclude>
              </excludes>
            </filter>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>

For more information see:

http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html

You can also use maven-assembly-plugin

The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive more

I looked your pom file. So i am posting a working sample pom file that i made based on one you provided in pastebin.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.selenium.pro</groupId>
    <artifactId>packjar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>packing library with jar</name>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.41.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>2.3</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <filters>
                                        <filter>
                                            <artifact>junit:junit</artifact>
                                            <includes>
                                                <include>junit/framework/**</include>
                                                <include>org/junit/**</include>
                                            </includes>
                                            <excludes>
                                                <exclude>org/junit/experimental/**</exclude>
                                                <exclude>org/junit/runners/**</exclude>
                                            </excludes>
                                        </filter>
                                        <filter>
                                            <artifact>*:*</artifact>
                                            <excludes>
                                                <exclude>META-INF/*.SF</exclude>
                                                <exclude>META-INF/*.DSA</exclude>
                                                <exclude>META-INF/*.RSA</exclude>
                                            </excludes>
                                        </filter>
                                    </filters>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Normally for your development you don't need to pack library but you can verify it. When you have to build your application to client , you could run

mvn clean package -Pprod   //profile prod

and here is portion of build log

[INFO] --- maven-shade-plugin:2.3:shade (default) @ packjar ---
Downloading: http://repo.maven.apache.org/maven2/org/ow2/asm/asm/5.0.2/asm-5.0.2.jar
Downloaded: http://repo.maven.apache.org/maven2/org/ow2/asm/asm/5.0.2/asm-5.0.2.jar (52 KB at 9.0 KB/sec)
[INFO] Including org.seleniumhq.selenium:selenium-java:jar:2.41.0 in the shaded jar.
[INFO] Including org.seleniumhq.selenium:selenium-chrome-driver:jar:2.41.0 in the shaded jar.
[INFO] Including org.seleniumhq.selenium:selenium-remote-driver:jar:2.41.0 in the shaded jar.
[INFO] Including cglib:cglib-nodep:jar:2.1_3 in the shaded jar.
[INFO] Including org.json:json:jar:20080701 in the shaded jar.
[INFO] Including com.google.guava:guava:jar:15.0 in the shaded jar.
[INFO] Including org.seleniumhq.selenium:selenium-htmlunit-driver:jar:2.41.0 in the shaded jar.
[INFO] Including net.sourceforge.htmlunit:htmlunit:jar:2.13 in the shaded jar.
[INFO] Including xalan:xalan:jar:2.7.1 in the shaded jar.
[INFO] Including xalan:serializer:jar:2.7.1 in the shaded jar.
[INFO] Including commons-collections:commons-collections:jar:3.2.1 in the shaded jar.
[INFO] Including org.apache.commons:commons-lang3:jar:3.1 in the shaded jar.
[INFO] Including org.apache.httpcomponents:httpmime:jar:4.3.1 in the shaded jar.
[INFO] Including commons-codec:commons-codec:jar:1.8 in the shaded jar.
[INFO] Including net.sourceforge.htmlunit:htmlunit-core-js:jar:2.13 in the shaded jar.
[INFO] Including xerces:xercesImpl:jar:2.11.0 in the shaded jar.
[INFO] Including xml-apis:xml-apis:jar:1.4.01 in the shaded jar.

Note: If you are using signed library jars then this solution might not work for you.

gyanu
  • 945
  • 1
  • 8
  • 20
  • I was already using maven, if I have a module under my dependencies, will it build with it included? For example, here is my pom.xml so far. http://pastebin.com/nykuA9HB Thanks! – user3518218 May 22 '14 at 02:18
  • I don't find you are using any plugin and configuration to accomodate library classess in your build jar. – gyanu May 22 '14 at 08:31
  • You should accept answer if it make sense to your question – gyanu May 24 '14 at 00:34