Skip to main content

Fixed: mvn exec:java Runs the pom's mainClass Instead of -Dexec.mainClass

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

mvn exec:java "-Dexec.mainClass=com.example.Report" ran com.example.Main, the class named in pom.xml, instead of the one on the command line. The pom set mainClass in the plugin's <configuration>, and that value won. Setting the default as the exec.mainClass property instead lets the command line choose again.

What I Was Doing​

I was building the Java example of a JWT tutorial with Maven. Its pom.xml set up the exec plugin with <mainClass>com.example.Main</mainClass>, so mvn -q compile exec:java ran the tutorial's program. Next to it, I wrote a second class with its own main method: a small program of mine that checks how the JWT library treats different tokens. I ran it with -Dexec.mainClass, and got the tutorial program's output instead.

The example below shows the same thing with nothing else in the way: two classes that each print their own name.

What I Expected​

Report ran: the output of the class the command names.

What Happened Instead​

Create a folder named demo, with this pom.xml in it:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

The exec-maven-plugin entry names com.example.Main as the class to run, in its <configuration>.

Then the two classes, in src/main/java/com/example/:

src/main/java/com/example/Main.java
package com.example;

public class Main {

public static void main(String[] args) {
System.out.println("Main ran");
}
}
src/main/java/com/example/Report.java
package com.example;

public class Report {

public static void main(String[] args) {
System.out.println("Report ran");
}
}

In the demo folder, run Report:

Terminal
mvn -q compile exec:java "-Dexec.mainClass=com.example.Report"
Output
Main ran

A zsh terminal where mvn -q compile exec with -Dexec.mainClass set to com.example.Report prints Main ran.

-q keeps Maven quiet, compile builds the classes and exec:java runs one. It ran Main, with no error and no warning. The command is the same in zsh, bash and PowerShell: the quotes keep PowerShell from splitting the option at its first dot. PowerShell printed Main ran too.

What I Checked​

  • The exec:java goal's documentation. Its page for exec:java lists one parameter, mainClass, for the class to run, with "User Property: exec.mainClass". So <mainClass> in the pom and -Dexec.mainClass on the command line set the same parameter.
  • Maven's guide to configuring plugins. The Guide to Configuring Plug-ins says: "For Mojos that are intended to be executed directly from the CLI, their parameters usually provide a means to be configured via system properties instead of a <configuration> section in the POM." A property is a way to set a parameter instead of the <configuration> section.
  • Where a property's value can come from. The documentation of Maven's Parameter annotation, with which a plugin declares its parameters, says of a parameter's property: "Property to use to retrieve a value. Can come from -D execution, setting properties or pom properties."
  • The plugin's own description of its goals. The exec plugin's jar describes them in a file named META-INF/maven/plugin.xml. In version 3.7.0, the java goal's entry for the parameter is <mainClass implementation="java.lang.String">${exec.mainClass}</mainClass>: ${exec.mainClass} is the value the parameter falls back on.

None of the documentation pages says which value wins when the <configuration> section and a -D property both set the parameter. The plugin's file and the run fit together: the property is only the parameter's fallback, and the pom's <mainClass> took its place, so the property was never used. The Parameter page also points to the fix: the property can come from the pom's own <properties> too.

The Fix​

Name the default class in the pom's <properties> as exec.mainClass, and take <configuration> out of the plugin:

pom.xml with an exec.mainClass property set to com.example.Main in properties, under a comment that it's the class exec runs when the command line doesn't name one, and an exec-maven-plugin entry with no configuration.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- The class exec:java runs when the command line doesn't name one. -->
<exec.mainClass>com.example.Main</exec.mainClass>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
</plugin>
</plugins>
</build>
</project>

Two things changed: <exec.mainClass>com.example.Main</exec.mainClass> is new in <properties>, and the plugin's <configuration> section is gone.

The Result​

Run the program without naming a class, then with Report:

Terminal
mvn -q compile exec:java
Output
Main ran
Terminal
mvn -q compile exec:java "-Dexec.mainClass=com.example.Report"
Output
Report ran

A zsh terminal where mvn -q compile exec prints Main ran, and the same command with -Dexec.mainClass set to com.example.Report prints Report ran.

Without -D, the pom's property chose Main. With it, the command line's value won over the pom's property, and Report ran. PowerShell gave the same two results.

How to Avoid It​

  • Give a plugin parameter that the command line should change a default with its property, in <properties>, instead of in the plugin's <configuration>.
  • Look up a parameter's property on the goal's documentation page, under "User Property".
  • When a -D option seems to do nothing, look for the same parameter in the plugin's <configuration>.

Troubleshooting​

SymptomLikely causeFix
-Dexec.mainClass=… runs the pom's classThe plugin's <configuration> sets mainClassMove the value to <exec.mainClass> in <properties>
[ERROR] Unknown lifecycle phase ".mainClass=com.example.Report" in PowerShellThe option isn't in quotes, so PowerShell split it at its first dotQuote the whole option

FAQ​

Why didn't Maven warn me?​

Maven doesn't report it. Run without -q, the same command printed only Maven's [INFO] lines, Main ran and BUILD SUCCESS: nothing said that the command line's value went unused.

Can the pom leave the class out entirely?​

Yes. Then every run names the class with -Dexec.mainClass, as the plugin's usage page shows. The exec.mainClass property only saves typing it for the class you run most.

Conclusion​

When a plugin's <configuration> in pom.xml sets a parameter, the same parameter's -D property on the command line doesn't change it. Put the default in <properties> under the parameter's property name instead, here exec.mainClass, and -D overrides it whenever you need another class.

Additional Resources​