简体   繁体   English

Maven - 来自java项目的可执行文件

[英]Maven - Executable file from a java project

I need to use maven (for a school project) to create an executable file from a single maven command. 我需要使用maven(用于学校项目)从单个maven命令创建可执行文件。 I've never used maven and tried many solutions here on stackoverlow. 我从未使用过maven,并在stackoverlow上尝试了很多解决方案。 The solutions created a jar file, but the file never opened. 解决方案创建了一个jar文件,但该文件从未打开过。

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. 我知道这不是maven惯例,但现在更改它意味着我必须调整导入(因为intelliJ不能完美地重构所有内容)大约40个类。

<?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? 我需要做什么来制作可执行文件?

You need to add plugin to your pom.xml file 您需要将插件添加到pom.xml文件中

<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 并运行程序: mvn clean install exec:java

... here is the link for doc http://www.mojohaus.org/exec-maven-plugin/usage.html ...这里是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 可能有不同的解决方案,取决于您的要求: https//www.baeldung.com/executable-jar-with-maven

TimurJD's answer is correct however I would like to explain step by step what is actually happening and why. TimurJD的答案是正确的,但我想逐步解释实际发生的事情和原因。

  • To have a jar be executable the JVM needs to know where your main method is. 要使jar可执行,JVM需要知道main方法的位置。

  • For that you need a file called META-INF/MANIFEST.MF inside the jar you create. 为此,您需要在您创建的jar中使用名为META-INF / MANIFEST.MF的文件。

  • This file must contain a reference to the class containing your main method which is done like this: 此文件必须包含对包含main方法的类的引用,如下所示:

      Main-Class: com.example.ClassContainingMainMethod 
  • There are many ways of creating said file but since you are using maven here is the plugin that will help you create this manifest file 有很多方法可以创建所述文件,但是因为你在这里使用maven是一个插件,可以帮助你创建这个清单文件

     <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.1</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.my.packege.Main</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> 
  • Once you have the plugin in your pom.xml simply run the maven install goal, either from your IDE or the command line. pom.xml插入插件后,只需从IDE或命令行运行maven install目标即可。 After you should find a folder called target in your project. 在项目中找到名为target的文件夹之后。 That folder will contain the executable jar. 该文件夹将包含可执行jar。

  • To run the jar you can call from the command line: 要运行jar,您可以从命令行调用:

     java -jar MyProject.jar 

It should also be noted that unless you abide by the maven standard of keeping your source code in src/main/java you will have to specify your source folder explicitly. 还应该注意,除非您遵守将源代码保存在src/main/java的maven标准,否则您必须明确指定源文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM