Maven – Add Java classpath to manifest for runtime

Previously we have talked about how to package a .jar artifact with dependencies included.

 

An alternative solution is to put your dependencies in the src/main/resources folder and they will be copied to the target folder together with the packaged .jar file. Then we can add the class paths to these dependencies in the Manifest with the help of the maven-jar-plugin.

The following project is a real life example which i use it to test the connection between the JBoss server and the MSSQL server.

1. Create the src/main/java/info.ykyuen.eureka.mssql.ConnectMSSQLServer.java.

package info.ykyuen.eureka.mssql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import java.net.URL;
import java.net.URLClassLoader;

public class ConnectMSSQLServer {

  public void dbConnect(String db_connect_string, String db_userid, String db_password) {
    try {

      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);

      System.out.println("-----------------------------");
      System.out.println("| Database server connected |");
      System.out.println("-----------------------------");
      System.out.println("");
      Statement statement = conn.createStatement();
      String queryString = "SELECT @@Version";
      ResultSet rs = statement.executeQuery(queryString);
    while (rs.next()) {
      System.out.println(rs.getString(1));
    }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    ConnectMSSQLServer connServer = new ConnectMSSQLServer();
    connServer.dbConnect("jdbc:sqlserver://<sqlserver host>:<port>;databaseName=<database name>", "<username>", "<password>");
  }
}

 

2. The program needs the sqljdbc4-4.0.jar in runtime but it is not available on the Maven Central repository. You can download it from here and save it at src/main/resources/runtime-lib/sqljdbc4-4.0.jar.

3. Create the following pom.xml at your project root.

<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>info.ykyuen.eureka</groupId>
  <artifactId>test-connection</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>   
          <archive>
            <manifest>
              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
              <addClasspath>true</addClasspath>
              <mainClass>info.ykyuen.eureka.mssql.ConnectMSSQLServer</mainClass>
            </manifest>
            <manifestEntries>
              <!-- Add the sqljdbc in runtime for the Class.forName() reflection call. -->
              <Class-Path>./classes/runtime-lib/sqljdbc4-4.0.jar</Class-Path>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 

3. Package the .jar by mvn clean package.
 

4. After the build, the runtime-lib folder will be copied to target/classes automatically.
 

5. Now you can run java -jar .\target\test-connection-1.0-SNAPSHOT.jar to test the connection and the program knows where to find the sqljdbc4-4.0.jar as stated in the Manifest.
maven-add-classpath-to-manifest-for-runtime
 

Done =)

Reference:

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.