How to set additional Class-Path entries in manifest with onejar Maven plugin?

1.4k views Asked by At

Is there a way to add an arbitrary classpath entry to a JAR file manifest using onejar-maven-plugin?

I found the way to configure maven-jar-plugin to do this, but it appears that there is no such option for onejar-maven-plugin.

This is not done to find additional classes (otherwise why use the onejar plugin, right?), but rather to locate a configuration file that must be external to the JAR.

Is there a direct solution or a workaround for this?

3

There are 3 answers

0
A_Di-Matteo On

Is the usage of the one-jar plugin really required? You can achieve the same goal (packaging in one single jar your application AND all the required dependencies, including transitive ones, AND add configuration for Class-Path AND using a more stable/standard plugin) applying the following approach:

  • Configure the Class-Path entry in your application Jar using the Maven Jar Plugin and the approach you mentioned in the question
  • Use the Maven Assembly Plugin to package one single JAR including dependencies, as explained here, in another stackoverflow question/answer.

An example of one-jar executable file (without using the one-jar plugin) could be as following:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <!-- your further configuration here -->
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.sample.MainApp</mainClass>
                        <!-- your further configuration here -->
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

If you need to further play with classpath and Maven, I would suggest to also check this question here on stackoverflow.

0
approxiblue On

Adding arbitrary manifest entries is possible in 1.4.5:

<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.5</version>
    <executions>
        <execution>
            <configuration>
                <manifestEntries>
                    <Build-Status>Yes</Build-Status>
                </manifestEntries>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The onejar-maven-plugin project doesn't seem to be in active development anymore, so you might want to switch to other solutions (e.g. maven-assembly-plugin) eventually.


The plugin is not available on Maven Central. Someone else put up a version of it to Maven Central with a different group ID.

0
Praneeth Reddy G On

Additional libraries can be added to the classpath at the time of launch. The property one-jar.class.path can be used

one-jar.class.path
Extra classpaths to be added to the execution environment. Use platform independent path separator '|'

Example: --one-jar.class.path="./lib/two.jar|/opt/lib/three.jar"

Source: http://one-jar.sourceforge.net/index.php?page=details