javamavenpom.xmlmaven-pluginmaven-jar-plugin

How to configure the properties of the maven plug-ins in the "properties" section


To configure the main class declared in the manifest file for the jar and assembly plugins I have to open configuration tags in the plugin declaration as in the example below:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.example.Main</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

I would have to do something similar to configure the version of java used by the build plugin:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>11</source>
            <target>11</target>
        </configuration>
    </plugin>

But, normally, to avoid these configuration tags in the plugin section to make pom.xml easier and more pleasant to read, I configure the version of java used in the java compiler plugin with the following properties in the properties section:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

Would it be possible to configure the main class declared in the manifest file generated by the jar and assembly plug-ins in the same way?

Checking this old maven documentation about the jar plugin: https://maven.apache.org/archives/maven-1.x/plugins/jar/properties.html

The property "maven.jar.mainclass" is mentioned, I believe that at some point it was possible to define the main class of the manifest file by the project properties section as follows:

    <properties>
        <maven.jar.mainclass>com.example.Main</maven.jar.mainclass>
    </properties>

But when I try to run the jar generated in this way, I get the error message "no main manifest attribute, in example.jar". So is there any way to configure the main class without having to open those configuration tags directly in the plugins section?


Solution

  • <properties>
        <maven.jar.mainclass>com.example.Main</maven.jar.mainclass>
    </properties>
    

    ...

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>${maven.jar.mainclass}</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    

    You can always use your own placeholders anywhere and specify them as a property. Just be aware that now the value is not a constant anymore, but became a variable. In other words, you can now use mvn package -Dmaven.jar.mainclass=path.to.nonexstingClass. It is powerfull, but in this case your jar has become useless because of the non-existing class, and such issue is almost impossible to track back.