Maven - Executable file from a java project

Yoey :

I need to use maven (for a school project) to create an executable file from a single maven command. I've never used maven and tried many solutions here on stackoverlow. The solutions created a jar file, but the file never opened.

This is my project structure

src
    com
        project
                code
                       swing
                             programm
                                      interface
                                               Main.class

I know this isn't maven convention, however changing it now would mean I would have to adjust the imports (as intelliJ doesn't refactor everything perfectly) for around 40 classes.

<?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>MyGroup</groupId>
<artifactId>myProgramm</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Hello World</name>
<description>Course Project</description>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.25.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit5-engine</artifactId>
        <version>5.0.0-ALPHA</version>
    </dependency>

</dependencies>

<build>

</build>

What do I have to put inside to make an executable file?

t-jd :

You need to add plugin to your pom.xml file

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

and to run the program: mvn clean install exec:java

... here is the link for doc http://www.mojohaus.org/exec-maven-plugin/usage.html

There are possible different solutions, depends on your requirements: https://www.baeldung.com/executable-jar-with-maven

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=94481&siteId=1