AdSense

網頁

2020/4/30

IntelliJ IDEA Maven 打包可執行的jar檔 package executable jar

在IntelliJ使用maven將專案打包成可執行的jar的方法如下。


範例環境:

  • Windows 64 Bit
  • Java 8
  • IntelliJ IDEA 2019.3.2 (Community Edition)
  • Maven

pom.xml<plugins>新增以下maven-jar-plugin設定可打包為skinny jar,也就是不含依賴的jar。

<build>
    <finalName>app</finalName> <!-- 打包的檔案名稱 -->
    ...
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.abc.app.App</mainClass> <!-- main類別的full qualified name -->
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
    ...
</build>

打包成jar的方式:

  1. 在IntelliJ IDEA右側的maven點擊[package]兩次或按[Run Maven Build]。。
  2. 在命令介面輸入mvn package

如果要連同依賴的其他函式庫一起打包,則在pom.xml<plugins>新增以下maven-assembly-plugin設定打包成fat jar。

<build>
    <finalName>app</finalName>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.abc.app.App</mainClass> <!-- main類別的full qualified name -->
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>assemble-app</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        ...
    </plugins>
    ...
</build>

maven專案的pom.xml完整範例如下。

pom.xml

<?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>com.abc</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.10</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>app</finalName> <!-- 打包的檔案名稱 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.abc.app.App</mainClass> <!-- main類別的full qualified name -->
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-app</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< org.abc:demo >----------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
...
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ app ---
[INFO] Building jar: /../demo/target/app.jar
[INFO]
[INFO] --- maven-assembly-plugin:3.3.0:single (assemble-app) @ app ---
[INFO] Building jar: /../demo/target/app-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...

完成後會在專案的target目錄生成兩個jar,一個為skinny jar,一個為far jar。名稱後有-jar-with-dependency為fat jar。

上面範例打包後的jar分別為app.jarapp-jar-with-dependency.jar即為可執行jar。

在jar所在目錄使用java -jar <jar-name>.jar執行。<jar-name>為jar的名稱,例如上面的app.jar

C:\>..\demo\target\java -jar app.jar
C:\>..\demo\target\java -jar app-jar-with-dependency.jar

沒有留言:

AdSense