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>
Maven Archiver can add the classpath of your project to the manifest. This is done with the <addClasspath> configuration element.
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.
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.
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.
It was my mistake, I had to put
<Class-Path>./conf/</Class-Path>
and not
<Class-Path>.conf/</Class-Path>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With