Automatically generate jar manifest
Keeps the manifest up to date with the build and eliminates unneeded configuration by determining the correct attribute values and creating the manifest at compile time.
The attribute names must be passed to javac
as a comma separated list: -Aautomanifest=Automatic-Module-Name,Main-Class
. If no arguments are given the default is -Aautomanifest=Main-Class
. The jar manifest file is created at META-INF/MANIFEST.MF
or updated if it already exists. A jar file is not created and the generated manifest file must later be added into the actual jar file, examples below.
Supported attributes:
Automatic-Module-Name
: The common package of all classes (includingpackage-info.java
files). If there is no common package a warning is emitted. This may give bad results if the package structure was not designed with modules in mind.Main-Class
: The class containing themain
method. If none or multiple are found a warning is emitted.Premain-Class
: The class containing thepremain
method. If none or multiple are found a warning is emitted.Agent-Class
orLauncher-Agent-Class
: The class containing theagentmain
method. If none or multiple are found a warning is emitted.
Supported system property based attributes:
Created-By
orBuild-Jdk
:${java.version} (${java.vendor})
Built-By
:${user.name}
Build-Jdk-Spec
:${java.specification.version}
Build-Os
:${os.name} (${os.version}; ${os.arch})
Any other attribute names on their own will produce a warning. Custom attribute values can be used with any attribute name by including a colon along with the value: Custom-Attribute:custom_value
. These values may not contain any commas. Only main attributes are supported, not per-entry attributes.
More information:
- https://docs.oracle.com/en/java/javase/17/docs/specs/jar/jar.html
- https://docs.oracle.com/en/java/javase/17/docs/api/java.instrument/java/lang/instrument/package-summary.html
- https://maven.apache.org/shared/maven-archiver
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.hunterwb</groupId>
<artifactId>automanifest</artifactId>
<version>0.1.0</version>
</path>
</annotationProcessorPaths>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Aautomanifest=Main-Class,Custom-Attribute:custom_value</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
dependencies {
annotationProcessor 'com.hunterwb:automanifest:0.1.0'
}
compileJava {
options.compilerArgs.add('-Aautomanifest=Main-Class,Custom-Attribute:custom_value')
}
jar {
manifest {
from compileJava.destinationDirectory.file('META-INF/MANIFEST.MF')
}
}