Maven 常用技巧總結

雪中亮發表於2018-01-17

「部落格搬家」 原地址: 簡書 原發表時間: 2017-04-06

1. 設定 Java JDK 的版本為「JDK 1.8」

可以修改 pom.xml 新增如下語句實現使用 Java 8 語言特性,共有兩種方式,可任選其一:

1.1 新增 property

<project>
  [...]
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  [...]
</project>
複製程式碼

1.2 直接配置外掛

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>
複製程式碼

2. Maven 構建生成可執行的 Jar

2.1 簡單構建可執行的 Jar

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          [...]
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        [...]
      </plugin>
      [...]
</project>
複製程式碼

此時可使用如下命令構建可執行 Jar

mvn assembly:single
複製程式碼

2.2 將 Assembly 的 single 目標繫結到專案的構建生命週期中

可在 pom.xml 檔案中新增如下內容:

<project>    
  [...]              
  <build>        
    [...]                            
    <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.0.0</version>
          <configuration>
            <!-- append assembly id in release file name -->
            <appendAssemblyId>true</appendAssemblyId>
            <!--構建可執行的 Jar-->
            <archive>
              <manifest>
                <mainClass>cc.bitky.fx.Main</mainClass>
              </manifest>
            </archive>
            <!--使用「Jar整合依賴」的描述符-->
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <!-- this is used for inheritance merges -->
              <phase>package</phase>
              <!-- bind to the packaging phase -->
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    [...]  
  </build>
</project>
複製程式碼

之後,為了生成專案的 Jar 檔案,可以簡單地執行如下生命週期階段命令:

mvn package
複製程式碼

構建完畢後,可以看到已經生成了類似於以下名字的檔案:

target / sample-1.0-SNAPSHOT-jar-with-dependencies.jar

target / sample-1.0.jar
複製程式碼

appendAssemblyId: 控制是否在已生成的檔案的檔名中包含「ssembly id」。

execution: 用於將 maven-assembly-plugin 整合至標準的 Maven 構建生命週期中,此時執行 maven package 時就會執行指定的操作,從而實現自定義打包。

2.3 Assembly 外掛的 Maven Goals

  • assembly:assembly: 「已棄用」會自動執行 package 生命週期。
  • assembly:single: 僅會裝配成 jar-with-dependencies,我們不想讓 package 階段執行兩次 package,所以配置為這個。

3. 可執行 Jar 檔案執行時出現亂碼

使用 Maven 生成的可執行 Jar 檔案,在 cmd 中執行時出現亂碼,專案編碼統一為 UTF-8,日誌使用 SLF4J 框架。

3.1 方法 1:windows 預設使用 GBK 編碼,執行 Jar 時動態指定編碼

執行 java -jar 命令時新增引數

-Dfile.encoding=UTF-8 
複製程式碼

格式如下:

java -jar -Dfile.encoding=UTF-8 simpler.jar 
複製程式碼

或新增環境變數:

JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF-8
複製程式碼

3.2 使用臨時的活動內碼表執行

在控制檯執行命令:

chcp 65001
複製程式碼

可將當前 cmd 的編碼臨時變為 UTF-8,執行 chcp 命令可顯示當前 cmd 的編碼。

4. Maven 中讀取資原始檔

在 Maven 專案的根目錄下,有如下必要的目錄結構:

  • src
    • main
      • resources「存放一些資原始檔」
      • java
    • test
      • resources「存放一些資原始檔」
      • java
  • pom.xml

在專案的 compile 時期,所有的資原始檔和 .class 檔案均被複制到 target/classes/ 目錄中,獲取資原始檔,可參考如下語句:

1. this.getClass().getResource(""//得到的是當前類 class 檔案所在的目錄 URL。

2. this.getClass().getResource("/"3. this.getClass().getClassLoader().getResource(""4. ClassLoader.getSystemResource(""5. Thread.currentThread().getContextClassLoader().getResource(""//得到的是當前 ClassPath 的絕對 URI 路徑。
複製程式碼

所以,如若想要獲取 resources 資料夾中的資原始檔 ky.xml,可使用如下語句之一:

getClass().getClassLoader().getResource("ky.xml")
getClass().getResource("/ky.xml")
複製程式碼

5. 參考資料

  1. Setting the -source and -target of the Java Compiler
  2. 關於 Apache Maven 您不知道的 5 件事
  3. Maven 打包可執行 Jar 的方法
  4. [關於建立可執行的 Jar 檔案](http://tonglin.iteye.com/blog/556449)
  5. 初學 Maven - 使用 Assembly Plugin 實現自定義打包
  6. JavaFX and maven: NullPointerException: Location is required
  7. Java 獲取檔案的路徑
  8. Maven 專案打包成 Jar 後執行日誌亂碼
  9. Console 輸出 UTF-8

相關文章