Skip to content

Building a Driver JAR with Dependencies

Terry Chow edited this page Jun 12, 2023 · 6 revisions

Remove unwanted dependencies before building (Optional step)

Dependencies are managed within the pom.xml file. The file is found at the root of the driver project. In the pom.xml file, look for the <dependencies></dependencies> tag pair. Within this tag pair are the driver's listed dependencies. Each dependency is denoted by a <dependency></dependency> tag pair. Remove unwanted dependencies by removing the <dependency>....</dependency> per dependency.

  1. Navigate to the project root and open the pom.xml in a text editor.

  2. Look for the <dependencies></dependencies> tag pair. Within here, there should be dependencies listed and denoted within <dependency></dependency> tag pairs.

  3. Identify and remove any unwanted dependencies and then save the pom.xml file.

For example, the start of the dependency list within the pom.xml file should look something like the following:

	<dependencies>
		<dependency>
			<groupId>com.azure</groupId>
			<artifactId>azure-security-keyvault-keys</artifactId>
			<version>${azure-security-keyvault-keys.version}</version>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.azure</groupId>
			<artifactId>azure-identity</artifactId>
			<version>${azure-identity.version}</version>
			<optional>true</optional>
			<exclusions>
				<exclusion>
					<groupId>stax</groupId>
					<artifactId>stax-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.microsoft.azure</groupId>
			<artifactId>msal4j</artifactId>
			<version>1.13.3</version>
			<optional>true</optional>
		</dependency>
                ...
                ...
                ...
                ...
                ...
	</dependencies>

If the azure-security-keyvault-keys dependency is removed, the resulting list should now look like the following:

	<dependencies>
		<dependency>
			<groupId>com.azure</groupId>
			<artifactId>azure-identity</artifactId>
			<version>${azure-identity.version}</version>
			<optional>true</optional>
			<exclusions>
				<exclusion>
					<groupId>stax</groupId>
					<artifactId>stax-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.microsoft.azure</groupId>
			<artifactId>msal4j</artifactId>
			<version>1.13.3</version>
			<optional>true</optional>
		</dependency>
                ...
                ...
                ...
                ...
                ...
	</dependencies>

Building the shaded driver (Building a driver JAR with dependencies)

Building a shaded driver JAR requires adding a build profile to the pom.xml of the driver project. To do so, we'll need to copy and paste one of the following four build profiles into the pom.xml file. Specifically, to add our shaded driver JAR build profile, we need to paste the profile within the the <profiles></profiles> tag pair in the pom.xml file. After the profile is added, we can then build the driver project with mvn clean install -DskipTests -T C1 -Pshadedjre11 (note that this example uses the jre11 shaded profile). The four profiles are shadedjre8, shadedjre11, shadedjre17 and shadedjrelatest as listed below.

For shadedjrelatest replace the XX placeholders with the latest JDK version eg. As of writing this guide, the current latest JDK is 19 so replace XX with 19.

For clarity, the exact steps are the following:

  1. After cloning the driver project, navigate to the root of the project directory. In here should be a file called pom.xml. Open this file in your favourite text editor.

  2. Copy your desired shaded driver build profile. In other words, copy the desired build profile that builds the driver with dependencies for your desired JDK version. Again, the corresponding profiles are listed below within code blocks.

  3. Paste your copied profile within the <profiles></profiles> tag pair eg. paste your profile on a new line under the first <profiles> tag. (Note that it's <profiles> and not <profile> -- there is a 's'. If you do a search eg. CTRL-F for <profiles>, place your copied profile right under the first match.

  4. Save the pom.xml file.

  5. Run mvn clean install -DskipTests -T C1 -P<your-shaded-profile>

  • To build shadedjre8, run mvn clean install -DskipTests -T C1 -Pshadedjre8
  • To build shadedjre11, run mvn clean install -DskipTests -T C1 -Pshadedjre11

shadedjre8

		<profile>
			<id>shadedjre8</id>
			<build>
				<finalName>${project.artifactId}-${project.version}.jre8${releaseExt}</finalName>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-compiler-plugin</artifactId>
						<version>3.8.0</version>
						<configuration>
							<excludes>
								<exclude>**/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java</exclude>
								<exclude>**/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java</exclude>
								<exclude>**/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java</exclude>
							</excludes>
							<testExcludes>
								<exclude>**/com/microsoft/sqlserver/jdbc/connection/ConnectionWrapper43Test.java</exclude>
								<exclude>**/com/microsoft/sqlserver/jdbc/connection/RequestBoundaryMethodsTest.java</exclude>
								<exclude>**/com/microsoft/sqlserver/jdbc/JDBC43Test.java</exclude>
							</testExcludes>
							<source>1.8</source>
							<target>1.8</target>
						</configuration>
					</plugin>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-javadoc-plugin</artifactId>
						<configuration>
							<source>8</source>
						</configuration>
					</plugin>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-jar-plugin</artifactId>
						<version>3.1.1</version>
						<configuration>
							<archive>
								<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
							</archive>
						</configuration>
					</plugin>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-surefire-plugin</artifactId>
						<version>3.0.0-M1</version>
						<configuration>
							<!-- Exclude [xJDBC42] For tests not compatible with JDBC 4.2 Specifications -->
							<excludedGroups>${excludedGroups}, xJDBC42</excludedGroups>
						</configuration>
					</plugin>
					<plugin>
                		<groupId>org.apache.maven.plugins</groupId>
                		<artifactId>maven-assembly-plugin</artifactId>
                		<version>3.1.1</version>

                		<configuration>
                		    <descriptorRefs>
                		        <descriptorRef>jar-with-dependencies</descriptorRef>
                		    </descriptorRefs>
                		</configuration>

                		<executions>
                		    <execution>
                		        <id>make-assembly</id>
                		        <phase>package</phase>
                		        <goals>
                		            <goal>single</goal>
                		        </goals>
                		    </execution>
                		</executions>
            		</plugin>
				</plugins>
			</build>
		</profile>

shadedjre11

		<profile>
			<id>shadedjre11</id>
			<build>
				<finalName>${project.artifactId}-${project.version}.jre11${releaseExt}</finalName>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-compiler-plugin</artifactId>
						<version>3.8.0</version>
						<configuration>
							<excludes>
								<exclude>**/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java</exclude>
							</excludes>
							<source>11</source>
							<target>11</target>
						</configuration>
					</plugin>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-jar-plugin</artifactId>
						<version>3.1.1</version>
						<configuration>
							<archive>
								<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
								<manifestEntries>
									<Automatic-Module-Name>com.microsoft.sqlserver.jdbc</Automatic-Module-Name>
								</manifestEntries>
							</archive>
						</configuration>
					</plugin>
					<plugin>
                		<groupId>org.apache.maven.plugins</groupId>
                		<artifactId>maven-assembly-plugin</artifactId>
                		<version>3.1.1</version>

                		<configuration>
                		    <descriptorRefs>
                		        <descriptorRef>jar-with-dependencies</descriptorRef>
                		    </descriptorRefs>
                		</configuration>

                		<executions>
                		    <execution>
                		        <id>make-assembly</id>
                		        <phase>package</phase>
                		        <goals>
                		            <goal>single</goal>
                		        </goals>
                		    </execution>
                		</executions>
            		</plugin>
				</plugins>
			</build>
		</profile>

shadedjre17

		<profile>
			<id>shadedjre17</id>
			<build>
				<finalName>${project.artifactId}-${project.version}.jre17${releaseExt}</finalName>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-compiler-plugin</artifactId>
						<version>3.8.0</version>
						<configuration>
							<excludes>
								<exclude>**/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java</exclude>
							</excludes>
							<source>17</source>
							<target>17</target>
						</configuration>
					</plugin>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-jar-plugin</artifactId>
						<version>3.1.1</version>
						<configuration>
							<archive>
								<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
								<manifestEntries>
									<Automatic-Module-Name>com.microsoft.sqlserver.jdbc</Automatic-Module-Name>
								</manifestEntries>
							</archive>
						</configuration>
					</plugin>
					<plugin>
                		<groupId>org.apache.maven.plugins</groupId>
                		<artifactId>maven-assembly-plugin</artifactId>
                		<version>3.1.1</version>

                		<configuration>
                		    <descriptorRefs>
                		        <descriptorRef>jar-with-dependencies</descriptorRef>
                		    </descriptorRefs>
                		</configuration>

                		<executions>
                		    <execution>
                		        <id>make-assembly</id>
                		        <phase>package</phase>
                		        <goals>
                		            <goal>single</goal>
                		        </goals>
                		    </execution>
                		</executions>
            		</plugin>
				</plugins>
			</build>
		</profile>

shadedjrelatest

		<profile>
			<id>shadedjrelatest</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<build>
				<finalName>${project.artifactId}-${project.version}.jre19${releaseExt}</finalName>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-compiler-plugin</artifactId>
						<version>3.8.0</version>
						<configuration>
							<excludes>
								<exclude>**/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java</exclude>
							</excludes>
							<source>XX</source>
							<target>XX</target>
						</configuration>
					</plugin>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-jar-plugin</artifactId>
						<version>3.1.1</version>
						<configuration>
							<archive>
								<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
								<manifestEntries>
									<Automatic-Module-Name>com.microsoft.sqlserver.jdbc</Automatic-Module-Name>
								</manifestEntries>
							</archive>
						</configuration>
					</plugin>
					<plugin>
                		<groupId>org.apache.maven.plugins</groupId>
                		<artifactId>maven-assembly-plugin</artifactId>
                		<version>3.1.1</version>

                		<configuration>
                		    <descriptorRefs>
                		        <descriptorRef>jar-with-dependencies</descriptorRef>
                		    </descriptorRefs>
                		    <archive>
                		        <manifest>
                		            <mainClass>Main</mainClass>
                		        </manifest>
                		    </archive>
                		</configuration>

                		<executions>
                		    <execution>
                		        <id>make-assembly</id>
                		        <phase>package</phase>
                		        <goals>
                		            <goal>single</goal>
                		        </goals>
                		    </execution>
                		</executions>
            		</plugin>
					<plugin>
                		<groupId>org.apache.maven.plugins</groupId>
                		<artifactId>maven-assembly-plugin</artifactId>
                		<version>3.1.1</version>

                		<configuration>
                		    <descriptorRefs>
                		        <descriptorRef>jar-with-dependencies</descriptorRef>
                		    </descriptorRefs>
                		</configuration>
                		<executions>
                		    <execution>
                		        <id>make-assembly</id>
                		        <phase>package</phase>
                		        <goals>
                		            <goal>single</goal>
                		        </goals>
                		    </execution>
                		</executions>
            		</plugin>
				</plugins>
			</build>
		</profile>
Clone this wiki locally