How to create a manifest file with Maven

﹏ヽ暗。殇╰゛Y 2022-05-26 07:43 376阅读 0赞

#

This tutorial will show you how to use the 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 :

  1. Define the entry point of the Application, make the Jar executable.
  2. Add project dependency classpath.

When you run the command mvn package to package project into a Jar, the following meta-inf/manifest.mf file will be generated and added into the final Jar file automatically.

meta-inf/manifest.mf

  1. Manifest-Version: 1.0
  2. Built-By: ${user.name}
  3. Build-Jdk: ${java.version}
  4. Created-By: Apache Maven
  5. 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

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <version>2.4</version>
  5. <configuration>
  6. <archive>
  7. <manifest>
  8. <mainClass>com.mkyong.core.App</mainClass>
  9. </manifest>
  10. </archive>
  11. </configuration>
  12. </plugin>

Copy

Following manifest file will be generated. If you run this Jar, it will execute the com.mkyong.core.App.

meta-inf/manifest.mf

  1. anifest-Version: 1.0
  2. Built-By: mkyong
  3. Build-Jdk: 1.6.0_35
  4. Created-By: Apache Maven
  5. Main-Class: com.mkyong.core.App
  6. 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 will use maven-dependency-plugin to copy project dependencies to somewhere else.

pom.xml

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <version>2.4</version>
  5. <configuration>
  6. <archive>
  7. <manifest>
  8. <addClasspath>true</addClasspath>
  9. <mainClass>com.mkyong.core.App</mainClass>
  10. <classpathPrefix>dependency-jars/</classpathPrefix>
  11. </manifest>
  12. </archive>
  13. </configuration>
  14. </plugin>
  15. <plugin>
  16. <groupId>org.apache.maven.plugins</groupId>
  17. <artifactId>maven-dependency-plugin</artifactId>
  18. <version>2.5.1</version>
  19. <executions>
  20. <execution>
  21. <id>copy-dependencies</id>
  22. <phase>package</phase>
  23. <goals>
  24. <goal>copy-dependencies</goal>
  25. </goals>
  26. <configuration>
  27. <outputDirectory>
  28. ${project.build.directory}/dependency-jars/
  29. </outputDirectory>
  30. </configuration>
  31. </execution>
  32. </executions>
  33. </plugin>

Copy

Following manifest file will be generated. The project dependencies will be copied to {project}/target/dependency-jars/.

meta-inf/manifest.mf

  1. manifest-Version: 1.0
  2. Built-By: mkyong
  3. Build-Jdk: 1.6.0_35
  4. Class-Path: dependency-jars/log4j-1.2.17.jar
  5. Created-By: Apache Maven
  6. Main-Class: com.mkyong.core.App
  7. Archiver-Version: Plexus Archiver

Copy

Download Source Code

Download it – Generate-Manifest-Using-Maven.zip (7 KB).

References

  1. Maven manifest references
  2. Maven manifest examples
  3. How to create a Jar file with Maven

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读