How to create a manifest file with Maven

Source: Internet
Author: User

This tutorial'll show you and maven-jar-plugin to create a manifest file, and package/add it into the final Jar file. The manifest file is normally used to define following Tasks:define the entry point of the ' application, make the Jar exe Cutable. Add Project Dependency Classpath.

When you run the command MVN package to package project into a Jar, the following META-INF/MANIFEST.MF file would be genera Ted and added into the final Jar file automatically. Meta-inf/manifest.mf

manifest-version:1.0
built-by: ${user.name}
build-jdk: ${java.version}
created-by:apache Maven
Archiver-version:plexus archiver
Copy 1. Make the Jar executable

Define Maven-jar-plugin in Pom.xml, and configure the manifest file via configuration tag. Pom.xml

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId> maven-jar-plugin</artifactid>
        <version>2.4</version>
        <configuration>
          < archive>
            <manifest>
                <mainClass>com.mkyong.core.App</mainClass>
            </manifest >
          </archive>
        </configuration>
    </plugin>
Copy

Following manifest file would be generated. If you run this Jar, it'll execute the COM.MKYONG.CORE.APP. Meta-inf/manifest.mf

anifest-version:1.0
built-by:mkyong
build-jdk:1.6.0_35
created-by:apache Maven
main-class: COM.MKYONG.CORE.APP
Archiver-version:plexus archiver
Copy 2. Add Project dependency classpath.

Most Java projects need dependency, and it can define in manifest file easily. Normally, you'll use Maven-dependency-plugin to copy project dependencies to somewhere else. Pom.xml

  <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-jar-plugi
            n</artifactid> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>
            Com.mkyong.core.app</mainclass> <classpathPrefix>dependency-jars/</classpathPrefix>
        </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> &LT;ARTIFACTID&GT;MAVEN-DEPENDENCY-PLUGIN&LT;/ARTIFAC
                tid> <version>2.5.1</version> <executions> <execution>
                    <id>copy-dependencies</id> <phase>package</phase> <goals> <Goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/dependency-jars/&L
  t;/outputdirectory> </configuration> </execution> </executions> </plugin>
Copy

Following manifest file would be generated. The project dependencies is copied to {project}/target/dependency-jars/. Meta-inf/manifest.mf

manifest-version:1.0
built-by:mkyong
build-jdk:1.6.0_35
class-path:dependency-jars/ Log4j-1.2.17.jar
created-by:apache Maven
main-class:com.mkyong.core.app
Archiver-version:plexus Archiver
Copy Download Source CodeDownload It–generate-manifest-using-maven.zip (7 KB). ReferencesMaven manifest references maven manifest examples How to create a Jar file with Maven

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.