exclude entries in MANIFEST.MF - java

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>

Related

Maven - Load jar contained in dependency

I'm new to maven and I know that this is a common problem but can't make my project work.
I have an jar A which contains only a manifest and a lib folder with a jar B.
I'm able to add A as dependency in this way:
<dependency>
<groupId>A</groupId>
<artifactId>A</artifactId>
<version>1.0.0</version>
<scope>compile</compile>
</dependency>
Maven successfully builds but at runtime I get a ClassNotFoundException for a class contained in B.
So, how can I include B?
My plugins section is like this:
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
IMHO you should create two project A and B. Having module B I would install it in local Maven repo (mvn install) and the pull it like regular Maven dependency.
Probably your IDE see B's classes as you have jar in lib folder.

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>

How to instruct maven to automatically add dependency jars when they include a artifact that i created?

Well i have reusable code which i developed using Maven and the result artifact is a JAR.
Now to use it in another project , i have simply added the this dependency to that project's POM.xml,
but maven is not auto detecting and including the dependencies for the jar.
How do i go about do this ?
Appreciate any pointers in this regard .
You should try to build the developed(reusable) code with dependencies. I'm not quite sure if this will help (I had some issues with the build with dependencies, too), but I think it's worth a try.
So you should add this to the pom.xml (of the reusable project):
<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>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
For details, have a look at this question and the maven-documenation
I solved this by manually adding the pom file for the jar I created.
[file structure]
GroupIdFolder
ArtifactIdFolder
VersionFolder
ownjar.jar [artifact I created]
ownjar.pom [file I mannually created]
Then in the .pom file put in the dependencies.
<project>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
</project>

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