Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot set classpath with maven-assembly-plugin

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>
like image 511
D.R. Avatar asked Mar 28 '13 15:03

D.R.


People also ask

Does Maven add to classpath?

Maven Archiver can add the classpath of your project to the manifest. This is done with the <addClasspath> configuration element.

How does Maven create the classpath?

Maven creates this classpath by considering the project's dependencies. The reported classpath consists of references to JAR files cached in local Maven repository. Even the JAR artifact of the project is referenced from local Maven cache and not from the /target directory one or the other might expect.

What is Maven Assembly plugin?

The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files. Your project can easily build distribution "assemblies" using one of the prefabricated assembly descriptors.

What is Maven Jar plugin?

Maven Jar plugin is responsible for configuring where the project's main class is so it can add it to the Jar's manifest file. If you do not include this plugin when you try to run the jar file this error will appear : Exception in thread "main" java.lang.NoClassDefFoundError.


2 Answers

It was my mistake, I had to put

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

and not

<Class-Path>.conf/</Class-Path>
like image 73
D.R. Avatar answered Oct 21 '22 18:10

D.R.


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

like image 26
Marc Avatar answered Oct 21 '22 17:10

Marc