java 如何使用 maven 制作一个可执行的 jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32558478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:23:34  来源:igfitidea点击:

How to make an executable jar using maven?

javamavenjarclassloader

提问by user1950349

My maven project is like this and I have a quartz.propertiesfile in /src/main/resourcesfolder as shown below

我的 maven 项目是这样的,我quartz.properties/src/main/resources文件夹中有一个文件,如下所示

my-app
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |       `-- mycompany
    |   |           `-- app
    |   |               `-- App.java
    |   `-- resources
    |       `-- quartz.properties
    `-- test
        |-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

Now I want to make an executable jar using maven so that I can run it like this java -jar abc.jar. Below is my main method code which works fine in my laptop in my eclipse IDE but I want to run it on my ubuntu machine using java -jarcommand:

现在我想使用 maven 制作一个可执行的 jar 以便我可以像这样运行它java -jar abc.jar。下面是我的主要方法代码,它在我的 eclipse IDE 中的笔记本电脑中运行良好,但我想使用以下java -jar命令在我的 ubuntu 机器上运行它:

public static void main(String[] args) {
    StdSchedulerFactory factory = new StdSchedulerFactory();
    try {
        factory.initialize(App.class.getClassLoader().getResourceAsStream("quartz.properties"));
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
    } catch (SchedulerException ex) {
        System.out.println("error= " + ExceptionUtils.getStackTrace(ex));
    }
}

And here is my pom.xmlfile as of now. What changes I need to have in my pom.xmlfile to make an executable jar so that I can run it with java -jar?

这是我目前的pom.xml文件。我需要在我的pom.xml文件中进行哪些更改才能制作可执行 jar 以便我可以使用它运行它java -jar

<?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>
        <groupId>com.host.domain</groupId>
        <artifactId>DataPlatform</artifactId>
        <version>4.2.8-RELEASE</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>abc</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>
</project>

回答by Amila

You can configuremaven jar plugin to achieve this.

您可以配置maven jar 插件来实现这一点。

Try adding following just above dependencies section:

尝试在依赖项部分上方添加以下内容:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.mycompany.app.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

Here, <addClasspath>true</addClasspath>will add classpath entries to the manifest of your jar, so as long as you have those jars in same directory, your application will run fine.

在这里,<addClasspath>true</addClasspath>会将类路径条目添加到 jar 的清单中,因此只要您将这些 jar 放在同一目录中,您的应用程序就可以正常运行。

You can customize the library path with, for example, <classpathPrefix>lib/</classpathPrefix>. It means your libraries should be placed in relative /libdirectory.

例如,您可以使用<classpathPrefix>lib/</classpathPrefix>. 这意味着您的库应该放在相对/lib目录中。

You can also use maven dependency plugin if you want to automatically copy your libraries to output directory.

如果你想自动将你的库复制到输出目录,你也可以使用 maven 依赖插件。

回答by Craig

Actually you'll just need to add

其实你只需要添加

<packaging>jar</packaging>

to your header, In other words:

到您的标题,换句话说:

<?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>
    <groupId>com.host.domain</groupId>
    <artifactId>DataPlatform</artifactId>
    <version>4.2.8-RELEASE</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>abc</artifactId>
  <version>1.0.0</version>
<!-- This is where the magic happens -->
  <packaging>jar</packaging>

Then when you invoke on the command line make sure you include the fully qualified name of your class, like this:

然后,当您在命令行上调用时,请确保包含类的完全限定名称,如下所示:

java -cp NameOfFile.jar com.mycompany.app.App

java -cp NameOfFile.jar com.mycompany.app.App

The advantage of this is that you can multiple class files with main()methods that can be executed in one .jar file.

这样做的好处是您可以使用main()可以在一个 .jar 文件中执行的方法的多个类文件。

You can also include the suggestion of @Amila so you don't have to include the name when executing the jar, but you'll have to use this syntax instead:

您还可以包含@Amila 的建议,以便在执行 jar 时不必包含名称,但您必须使用以下语法:

java -jar NameOfFile.jar

java -jar NameOfFile.jar

回答by wemu

depending on what will end up in the jar file or what is required to run it it is probably not that easy. There is a good collection here: How can I create an executable JAR with dependencies using Maven?

取决于最终会出现在 jar 文件中的内容或运行它所需的内容,这可能并不那么容易。这里有一个很好的集合:如何使用 Maven 创建具有依赖项的可执行 JAR?

I would add Spring-Boot to that list. They have created a maven plugin to achieve this including a classloader that can read jar files inside a jar file.

我会将 Spring-Boot 添加到该列表中。他们创建了一个 maven 插件来实现这一点,包括一个可以读取 jar 文件中的 jar 文件的类加载器。

There is also the maven shade plugin. Depending on what you do that might just work or it may get cumbersome (if there are signed jars for example).

还有 maven shade 插件。根据您所做的工作,这可能会奏效,也可能会变得很麻烦(例如,如果有签名的 jars)。