就沒有簡短的途徑,一定要把 jar 引用到的所有第三方函式庫,一併全打包進 jar 裡
在 Maven 中可以透過設定幾個 build 參數來達成。
首先先在 pom.xml 裡寫上需要的參數如下:
<project>
<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- Indicate where is project main() function / the code entry. -->
<mainClass>org.example.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
其中 22 行的 org.example.App 是 jar 的進入點。最後再使用 Maven 的 console 執行以下的指令,就可以打包完成了。
mvn package打包出來的檔案,會被放在 /target 資料夾裡。
沒有留言:
張貼留言