How to put maven project version in war file manifest? - java

I need to have Maven insert the version number from the POM file into the manifest located in the WAR file under /WEB-INF/manifest.mf.
How do I do this? I was able to easily file documentation for doing this in a JAR file using the maven-jar-plugin, but that does not work on a WAR file.
Thanks for the help!

Figured it out using the maven-war-plugin. See the configuration below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<archive>
<manifestEntries>
<version>${project.version}</version>
</manifestEntries>
</archive>
</configuration>
</plugin>

Or you can use the addDefaultImplementationEntries or addDefaultSpecificationEntries flags which will add several entries including the project.version property.
addDefaultImplementationEntries
Implementation-Title: ${project.name}
Implementation-Version: ${project.version}
Implementation-Vendor-Id: ${project.groupId}
Implementation-Vendor: ${project.organization.name}
Implementation-URL: ${project.url}
addDefaultSpecificationEntries
Specification-Title: ${project.name}
Specification-Version: ${project.version}
Specification-Vendor: ${project.organization.name}
Default value for both is false. If a property is not defined (e.g. project.organization.name), then that line will be excluded from the manifest.
This could go into the maven-war-plugin configuration as follows:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>

Put ${project.version} in your manifest.mf where you want the version to be. In order for this to work, I believe you need the resources plugin so that manven will 'filter' resources as they are included in your war file.

Related

maven is not creating META-INF for spring boot project

When I build my Spring boot project, it creates an target folder and
target/classes also, but it doesn't create any META-INF. I have also included dependency -
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
<key>value</key>
</manifestEntries>
</archive>
</configuration>
Two ways to do it.
Form the maven-jar-plugin documentation :
Please note that the following parameter has been completely removed
from the plugin configuration:
useDefaultManifestFile
If you need to define your own MANIFEST.MF file you can simply achieve
that via Maven Archiver configuration like in the following example:
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
where in you can place your MANIFEST.MF under src/main/resources/META-INF folder of your project. The command
mvn clean package
would build the project jar with the src/main/resources by default.
The notes at usage of the plugin states that
Starting with version 2.1, the maven-jar-plugin uses Maven Archiver
3.1.1. This means that it no longer creates the Specification and Implementation details in the manifest by default. If you want them
you have to say so explicitly in your plugin configuration.
Which can be done using:
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>

How to refer maven dependency jars from an executable jar?

I have an executable jar which needs to be placed in an Eclipse Maven project (Lets assume in the first folder of the project). In the manifest file of this jar, I need to refer to the maven dependency jars. How can I specify that in MANIFEST.MF file using pom.xml? Is it possible?
Yes, you can specify jars using maven-jar-plugin.
you can specify dependent jars in manifest tag
E.g.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>dependency-jars/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>

Missing Main-Class attibute in MANIFEST.MF in Maven generated jar file

I am currently experimenting with Maven in Eclipse (m2e-Plugin) and tried to build and run the Hello World example project. However, when launching the generated jar, nothing happens. I checked the MANIFEST.MF and noticed that the Main-Class attribute was missing. After adding the attribute, the jar could be launched.
Why does Maven not add this attribute?
Have a look at this link:
https://maven.apache.org/shared/maven-archiver/examples/classpath.html#aAdd
You can find there how to configure your maven project to run specific class. You have to add maven-jar-plugin configuration with mainClass defined.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>

Modify Manifest file

I have an issue that after read two pages in google about it I can still not found the solution.
I want just create a manifest file into my war file and introduce the version of my application. I´m using mave-war-plugin of apache.
Using version 2.0.2 I achieve to create the manifest file, but just with the default information:
Manifest-Version: 1.0
Built-By: ***********
Build-Jdk: 1.7.0_25
Created-By: Apache Maven 3.1.1
Archiver-Version: Plexus Archiver
The code of 2.0.2 is this one:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Version>2.1.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
But like I said, I cannot introduce the new attribute "Implementation-Version"
And if I try to use the last version of the plugin I cannot create the manifest file at all.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>2.1.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
I´m using Maven 3 and I dont have create any MANIFEST.FM file in META-INF.
I try to create my own MANIFEST.FM in my META-INF. with this template, to see if would be possible to just set the values but nothing as well. Like I said I try everything!
Specification-Title: ****
Specification-Vendor: Company Name
Implementation-Title: Portal web
Implementation-Version: ${Implementation-Version}
Implementation-Vendor:*****
Build-Jdk: ${jdk.version}
After continue investigating, I see that my war file has 2 overlays. So after read about it I add into the main overlay, the plugin with the modification of the Manifest.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<archive>
<manifestEntries>
<Version>${project.version}</Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
And in the child wars the plugin like.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
</plugin>
But then the manifest is created with the default values, and the maven folder with the pom.properties, but in the child wars nothing is created in the META-INF
Regards.

exclude entries in MANIFEST.MF

I am currently building and EAR file using maven-ear-plugin
I have a requirement to exclude some of the Class-Path: entries from MANIFEST.MF file.
For example I have 3 dependencies hibernate-core.jar, quartz-1.6.5.jar and poi-3.7.jar in my class path (in my dependency of pom.xml)
When I build my EAR file all these entries will be automatically added to Class-Path entry of my MANIFEST.MF file.
My maven build looks as follows
<build>
<finalName>MyEAR</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<addExtensions />
<classpathPrefix />
</manifest>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF </manifestFile>
</archive>
.....
.....
</configuration>
</plugin>
</plugins>
</build>
In the above build file
<addClasspath>true</addClasspath>
is responsible for adding the MANIFEST.MF entries.
My requirement is to add only 2 jars in dependency quartz-1.6.5.jar and poi-3.7.jar and exlcude the remaning.
If I give false all the entries are excluded from the MANIFEST.MF classpath.
If the remove the depependecy entries from pom I get compilation errors.
how can I achieve the above scenario.
thanks,
Add scope "provided" to non-required dependencies:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>..</version>
<scope>provided</scope>
</dependency>

Categories