Home > Mobile >  How to add classpath to maven
How to add classpath to maven

Time:11-05

I want to create an executable jar with dependencies that are going to be outside the archive in folder lib. I found out that i can ask maven to copy dependencies into the folder and modify manifest file in my executable to know where to look for needed classes. The problem is that it does not add any classpaths to my manifest. Pom:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.sav</groupId>
    <artifactId>console-app-01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.sav.app.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.kenai.nbpwr</groupId>
            <artifactId>joda-time</artifactId>
            <version>1.6.2-201110292322</version>
        </dependency>
    </dependencies>

</project>

After running mvn install my manifest looks like this:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.3
Built-By: abigo
Build-Jdk: 11.0.12
Main-Class: org.sav.app.App

But it won't run. It tells me that it cannot find needed classes. As soon as i add class-path manually it runs with no problems

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.3
Built-By: abigo
Build-Jdk: 11.0.12
Main-Class: org.sav.app.App
Class-Path: lib/joda-time-1.6.2.jar lib/joda-time-1.6.2-201110292322.jar

Application:

package org.sav.app;

import org.joda.time.LocalTime;

public class App {

    public static void main(String[] args) {
        LocalTime currentTime = new LocalTime();
        System.out.printf("Current time is: %s%n", currentTime);
    }

}

CodePudding user response:

I'n in the same trouble now, and I can't find other workaround than downgrading maven version. I'm using maven 3.8.3, and I will download 3.6

CodePudding user response:

Used older version of maven and it all worked fine.

  • Related