Home > OS >  Run java from a single source in maven
Run java from a single source in maven

Time:10-16

Since java 11 (https://openjdk.org/jeps/330), a single source file can be run directly by java without having to compile it in an extra step.

Is the same possible in maven (with exec-maven-plugin)?

CodePudding user response:

Yes, you can run a single source file with exec-maven-plugin. In the argument, you need to give a filename including .java.

Both pom.xml and HelloWorld.java are in the same folder.

The sample program used HelloWorld.java

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

}

There are 2 ways you can achieve this.

  1. Run direct command without modifying pom.xml
mvn exec:exec -Dexec.executable="java" -Dexec.args="HelloWorld.java"

The output will be:

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example:single-run-test >---------------------
[INFO] Building Single Source Run 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.1.0:exec (default-cli) @ single-run-test ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.107 s
[INFO] ------------------------------------------------------------------------
  1. Configure pom.xml with exec-maven-plugin.

Maven 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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>single-run-test</artifactId>
  <name>Single Source Run</name>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <exec-maven-plugin.version>3.1.0</exec-maven-plugin.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>${exec-maven-plugin.version}</version>
        <configuration>
          <executable>java</executable>
          <arguments>
            <argument>-classpath</argument>
            <!-- automatically creates the classpath using all project dependencies, also adding the project build directory -->
            <classpath />
            <argument>HelloWorld.java</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Once you run mvn exec:exec command you will get the output

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example:single-run-test >---------------------
[INFO] Building Single Source Run 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.1.0:exec (default-cli) @ single-run-test ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.924 s
[INFO] ------------------------------------------------------------------------
  • Related