Preparing Ear file in maven - java

We have replaced ejbs with Spring transaction and security(service facade) in our project. Earlier we were using maven ejb plugin to include dependent libs in the classpath.
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
It was generating MANIFEST.MF file with all jar dependencies in ejb artifact (all jar files in ear folder in Jboss 4.2).
Since we have removed the ejbs now, session facade is an jar artifact. I don't know, how to generate MANIFEST.MF in the session facade with all jar dependencies using maven.
I have an option to specify includeInApplicationXml attribute to include jar files in application.xml file as java module but I would have to specify it for each and every jar which is a cumbersome process as jar files dependency kept on changing.
Do we have any way to include jar file dependency without listing them in application.xml. Lets say we include only sessionfacade.jar dependency in application.xml and generate MANIFEST.MF file having dependency of all other jars (the way it was happening using maven-ejb-plugin).
Any help will be highly appreciated. Thanks in advance.

you can use the same maven configuration, but with the maven-jar-plugin:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
This will generate the MANIFEST.MF with all dependencies on the class-path element.

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>

Executable Jar with external jar and dependency using maven

There are two Java applications: application 1 and application 2.
I want to use jar file of application 1 in application 2. I want to create executable jar of application 2 so that I can execute application 2 as jar by command line.
Application one: There are classes in application 1 as ClassA.java, ClassB.java
pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
I created jar file of application 1 using mvn clean compile assembly:single
Now I have added jar file of application 1 created before as an external jar in application 2.
In application 2 There is a main class : migration.DataMigration.java
There are dependencies also in pom.xml of application 2.
DataMigration class is using ClassA.java and ClassB.java.
Now I want to create a executable jar of application.
I tried to create this using maven-assembly-plugin but I got error : ClassA.class not not found, ClassB.class not found : it means jar of application 1 is not being available during executable jar creation.
but when I run application 2 in eclipse it executes correctly without error.
Can any one suggest how to create executable jar of application 2
In application2.jar you can specify to add application1.jar to its classpath.
In the META-INF/manifest.mf file of application2.jar add:
Class-Path:application1.jar
if the application1.jar is stored aside (in the same directory) of application2.jar it will be added to the classpath by the runtime.
To realize this in your maven-build:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
...
</plugin>
Source: http://maven.apache.org/shared/maven-archiver/examples/classpath.html

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>

How to put maven project version in war file manifest?

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.

Categories