Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate non-versioned jar-entries in manifest's classpath with Maven

To achieve an executable jar-file, I add the following to the pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>my.main.Class</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

The result is (MANIFEST.MF)

...
Class-Path: library1-8.10-SNAPSHOT.jar hessian-4.0.7.jar library2-8.10-SNAPSHOT.jar json-lib-2.2.3-jdk15.jar ezmorph-1.0.6.jar library3-8.10-SNAPSHOT.jar commons-io-1.4.jar
...

and so forth

That means, the jar's version is included with the file-name. I'd like it to have without it:

Class-Path: library.jar hessian.jar library2.jar json-lib.jar ezmorph.jar library3.jar commons-io.jar

simply because that's the way they're named in the lib-folder.

We are using Maven 2.2, so classpathLayoutType and customClasspathLayout aren't available (if they'd help at all).

Can anyone help?

like image 509
Stefan Avatar asked Jan 08 '13 16:01

Stefan


1 Answers

Why are they without a version in the lib directory? Aren't you using proper dependency management, something that Maven provides (and is one of the main reasons to use Maven in the first place)?

It is a bad idea to have jars without a version, since you can't easily tell which version of a library is installed on a system. The problem isn't that the Class-Path is wrong, but that the libraries should be versioned. There's a problem in the rest of the build. So, how do you declare dependencies, and how do you build the final package?

If you really want to do that, then you can customize the maven-archiver, specifying that the classpathLayoutType should be custom, and the customClasspathLayout should be ${artifact.artifactId}.${artifact.extension}. For example:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathLayoutType>custom</classpathLayoutType>
                    <customClasspathLayout>lib/${artifact.artifactId}${artifact.dashClassifier?}.${artifact.extension}</customClasspathLayout>
                    <mainClass>my.main.Class</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

(note that I added a lib/ prefix, feel free to remove it or replace it with whatever you need)

like image 146
Sergiu Dumitriu Avatar answered Oct 16 '22 15:10

Sergiu Dumitriu