Skip to content

Use Apache Maven With DreamBot Scripting

What Is Maven?

Maven is a build automation tool developed by the Apache Software Foundation. It can be used to manage project dependencies, and build Java projects consistently in the same manner.

Why Use Maven?

Maven makes dependency management a breeze by automatically pulling in required dependencies for a project. In our case using Maven will allow us to skip the step of adding the DreamBot API to our project each time. This also allows us to import other 3rd party libraries that may be of use such as JavaCord, MQ frameworks, SQL adapters, and more.

Maven can also package the included dependencies with the jar file its self. This is referred to as an Uber or Fat jar. This means you will no longer have to place libs needed for scripts under /Libs/. This makes sharing a script is as simple as sending the .jar file.

Maven Properties

To start we well set our compiler target/sources to Java version 1.8. This will compile our scripts with the specified version of Java for compatibility.

By default DreamBot is placed in the user's home directory. We can access this path via ${user.home}. We can create a reference to the folder to help save us from typing it each time.

    <properties>
        <!-- java properties -->
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>

        <!-- dreambot properties -->
        <dreambot.path>${user.home}/DreamBot/</dreambot.path>
    </properties>

Adding The DreamBot Dependency

In order to access the client we must add the DreamBot Content Deliver Network (CDN) repository. This allows us to pull the client from the CDN for use in our projects.

From there we can add then add the client dependency.

    <!-- dreambot cdn -->
    <repositories>
        <repository>
            <id>dreambot</id>
            <name>dreambot</name>
            <url>https://cdn.dreambot.org/maven/</url>
        </repository>
    </repositories>

    <!-- dependencies -->
    <dependencies>
        <dependency>
            <groupId>org.dreambot</groupId>
            <artifactId>client</artifactId>
            <version>3.0.0</version>
        </dependency>
    </dependencies>

Adding Other Dependencies

Adding other dependencies such as SQL connectors or MQ frameworks is as simple as copy and pasting the dependency into the dependencies group.

    <!-- dependencies -->
    <dependencies>
        <dependency>
            <groupId>org.dreambot</groupId>
            <artifactId>client</artifactId>
            <version>3.0.0</version>
        </dependency>

        <!-- mysql connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>

        <!-- rabbit mq client -->
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.9.0</version>
        </dependency>
    </dependencies>

Build Plugin For Uber/Fat/Shaded Jars

Now that we have all of our dependencies added to our project we need to actually build our project. While using Maven we can avoid keeping copies of our dependencies on hand and just add them to the .jar file when we build it. There are a couple ways to do this but I prefer the shade plugin provided by Apache.

When the build is executed it will automatically dump the script in your /Scripts/ folder inside of DreamBot (yay for task automation!).

    <!-- build jar file with dependencies -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>dep-included</shadedClassifierName>
                    <outputDirectory>${dreambot.path}/Scripts</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Executing The Build

Depending on your IDE you can execute the build inside the IDE or via the command line buy just running a mvn install. Then all of the above will happen automatically for you!

Troubleshooting