cannot set classpath with maven-assembly-plugin

14,400

Solution 1

It was my mistake, I had to put

<Class-Path>./conf/</Class-Path>

and not

<Class-Path>.conf/</Class-Path>

Solution 2

I usually do not use the assembly plugin to generate classpath entry in MANIFEST but rather the maven-jar-plugin with this configuration :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
      <archive>
        <index>true</index>
        <manifest>
          <addClasspath>true</addClasspath>
          <addExtensions>false</addExtensions>
          <mainClass>com.my.test.App</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>

I only use the assembly plugin to copy dependencies (including transitive ones) into my build directory, and creating the distribution archive. You can also use the dependency plugin do do this. If you want to copy you dependencies into a sub directory of your distribution tree, use the classpathPrefix in the maven-jar-plugin configuration to match you assembly descriptor dependencies destination.

Regard

Share:
14,400
D.R.
Author by

D.R.

Updated on June 04, 2022

Comments

  • D.R.
    D.R. almost 2 years

    I am creating console application. I want to have configuration files outside the jar file in conf folder and want to register this folder as a classpath for my application.

    I run mvn assembly:single command , get a jar file, BUT when I try to run this JAR with java -jar MyApplication.jar, it can't read configuration files.

    I have this snippet in my pom.xml

    <build>
        <finalName>MyApplication</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.7</version>  
                <configuration>
                    <projectNameTemplate>
                        [artifactId]-[version]
                    </projectNameTemplate>
                    <wtpmanifest>true</wtpmanifest>
                    <wtpapplicationxml>true</wtpapplicationxml>
                    <wtpversion>2.0</wtpversion>
                    <manifest>
                        ${basedir}/src/main/resources/META-INF/MANIFEST.MF
                    </manifest>
                </configuration>
    
            </plugin>
    
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <appendAssemblyId>false</appendAssemblyId>
                    <archive>
                        <manifest>
                            <mainClass>com.my.test.App</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>