Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 687
Gene M Avatar asked Jun 11 '15 16:06

Gene M


2 Answers

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.

like image 113
A_Di-Matteo Avatar answered Sep 30 '22 00:09

A_Di-Matteo


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.

like image 29
approxiblue Avatar answered Sep 30 '22 00:09

approxiblue