-
- 生成可執行的jar
生成可執行的jar
預設生成的jar包是不能夠直接運作的,以為帶有main方法的類資訊不會添加到manifest中(打開jar檔案中的META-INF/MANIFEST.MF檔案,将無法看到Main-Class一行),借助maven-shade-plugin可以生成可執行的jar檔案。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.pizi.helloworld.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
配置了mainClass為com.pizi.helloworld.HelloWorld,項目在打包的時候,會将該資訊放到MANIFEST中。現在執行maven clean install ,待建構完成之後打開target目錄,可以看到hello-world-1.0-SNAPSHOT.jar和original-hello-world-1.0-SNAPSHOT.jar,前者是帶有Main-Class資訊的可運作的jar,後者是原始jar,現在可以在根項目目錄執行該jar
java -jar hello-world-1.0-SNAPSHOT.jar