如何使用maven制作可执行jar?

use*_*349 7 java jar classloader maven

我的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
Run Code Online (Sandbox Code Playgroud)

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

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我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>
Run Code Online (Sandbox Code Playgroud)

Ami*_*ila 15

您可以配置 maven jar插件来实现此目的.

尝试添加以下依赖项部分:

  <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>
Run Code Online (Sandbox Code Playgroud)

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

例如,您可以自定义库路径<classpathPrefix>lib/</classpathPrefix>.这意味着您的库应该放在相对/lib目录中.

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