I'm beginner with maven and do not understand many things. I can build simple executable jar, but how to build multimodule maven project into executable jar is magic for me. So, I have three projects. Parent:
<?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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>Main</module> <module>Dep</module> </modules> </project>
And two child projects:
<?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/xsd/maven-4.0.0.xsd"> <parent> <artifactId>Test</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Main</artifactId> <dependencies> <dependency> <groupId>org.example</groupId> <artifactId>Dep</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
and:
<?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/xsd/maven-4.0.0.xsd"> <parent> <artifactId>Test</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Dep</artifactId> </project>
Main module has Main class with main method(lol)
public class Main { public static void main(String[] args) { Hello hello = new Hello(); System.out.println(hello.sayHello()); } }
Class Hello is defined in the Dep module. What should I add into my poms to build executable jar with Main class from Main module as entry point?
You could combine the maven-shade-plugin and maven-jar-plugin . The maven-shade-plugin packs your classes and all dependencies in a single JAR file. Configure the maven-jar-plugin to specify the main class of your executable JAR file (see Set Up The Classpath, chapter "Make The Jar Executable").
You need to change your pom for artifactid Main. You need to add the maven-assembly-plugin
In the configuration of it you have an option to specify the mainClass in the manifest. This should be the fully qualified name of the Main class.
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.example</groupId> <artifactId>Test</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>Main</artifactId> <properties> <!-- plugins --> <maven.assembly.plugin.version>2.4</maven.assembly.plugin.version> <!-- dependencies --> <dep.version>1.0-SNAPSHOT</dep.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>${maven.assembly.plugin.version}</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> <dependencies> <dependency> <groupId>org.example</groupId> <artifactId>Dep</artifactId> <version>${dep.version}</version> </dependency> </dependencies> </project>
After you start your build on the parent project, it should create the executable jar in the target subfolder of the module Main. (a jar with the name Main-1.0-SNAPSHOT-jar-with-dependencies.jar was what i got)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With