SkyWalker
SkyWalker

Reputation: 14327

How to add prefix to Class-path MANIFEST entries with the Maven assembly plugin?

I'm using the assembly plugin in Maven 3.x to create a tar ball that includes the project jars along with their dependencies. For convention I put the project jars on the root and all dependency jars under dir e.g.

├── hpcmom-cmaes-1.1.9-SNAPSHOT.jar
└── lib
    ├── akka-actor_2.10-2.2.3.jar
    ├── akka-agent_2.10-2.2.3.jar
    ├── akka-kernel_2.10-2.2.3.jar
    ├── akka-remote_2.10-2.2.3.jar
    ├── akka-slf4j_2.10-2.2.3.jar
    ├── akka-zeromq_2.10-2.2.3.jar

The assembly that generates this is defined as:

<assembly>
    <id>cmaes-bin</id>
    <baseDirectory>${pom.version}/hpcmom-cmaes</baseDirectory>  
    <formats>
        <format>tar.gz</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory></directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>COPYING*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>hpcmom-cmaes/target</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly> 

But the MANIFEST.MF generated within hpcmom-cmaes-1.1.9-SNAPSHOT.jar does not generate the ./lib/ prefix only for the third-party dependencies but like if all jars were in the same folder. How can I modify the MANIFEST.MF via the assembly plugin so that the lib prefix is accounted for there.

The generated Class-path MANIFEST.MF looks like this:

 Class-Path: akka-actor_2.10-2.2.3.jar akka-agent_2.10-2.2.3.jar akka-kernel_2.10-2.2.3.jar akka-remote_2.10-2.2.3.jar akka-slf4j_2.10-2.2.3.jar akka-zeromq_2.10-2.2.3.jar

and the one I would like to get is:

 Class-Path: lib/akka-actor_2.10-2.2.3.jar lib/akka-agent_2.10-2.2.3.jar lib/akka-kernel_2.10-2.2.3.jar lib/akka-remote_2.10-2.2.3.jar lib/akka-slf4j_2.10-2.2.3.jar lib/akka-zeromq_2.10-2.2.3.jar

UPDATE:

The assembly plugin configuration looks like this in the parent pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>                       
            <descriptor>src/main/assembly/cmaes-bin.xml</descriptor>
            <descriptor>src/main/assembly/cmaes-src.xml</descriptor>
        </descriptors>
    <archive>
        <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
        </manifest>
    </archive>                    
    </configuration> 
</plugin>

Upvotes: 1

Views: 7115

Answers (2)

SM. Hosseini
SM. Hosseini

Reputation: 291

I had the same problem. then add the below line inside the manifest tag of maven-jar-plugin solved my problem:

<classpathPrefix>libs/</classpathPrefix>

the complete part is:

  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.pmg.TestManager</mainClass>
                    <classpathPrefix>libs/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>

then when I view the MANIFEST.MF file inside the jar file:

    Manifest-Version: 1.0 
    Archiver-Version: Plexus Archiver Created-By:
    Apache Maven 3.8.6 
    Built-By: SM.Hosseini 
    Build-Jdk: 17.0.1 
    Class-Path:
    libs/jcip-annotations-1.0.jar libs/okhttp-5.0.0-alpha.3.jar  ....

so you can see that each dependency starts with libs/

Upvotes: 1

Cephalopod
Cephalopod

Reputation: 15145

Try adding the following plugin to your pom.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath> <-- don't know if this is needed -->
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>

Upvotes: 4

Related Questions