1. 程式人生 > >管理工具---Maven系列(一)編譯問題

管理工具---Maven系列(一)編譯問題

問題:Maven打包編譯錯誤工作區間設定編碼格式gbk可以utf-8不可以
錯誤如下:
[INFO] ————————————————————————
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project ins-service: Compilation failure
[ERROR] javac: �Ҳ����ļ�: E:\SVN3\20160918JunKang_Dev\JKDS\05源代�?後端\v1.0\parent\ins-service\src\main\java\com\junk\enums\FileUploadEnum.java
[ERROR] �÷�: javac
[ERROR] -help �����г����ܵ�ѡ��
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1]

http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn -rf :ins-service
解決辦法:
根據出現的亂碼發現應該推測到格式編碼問題,所以驗證下,改工作區間編碼格式為gbk呀呀,竟然好了。再改會utf-8格式呀呀又出錯了,很奇怪,推測問題是兩種:
一種是原來的gbk現在utf-8但是很明顯問題不對路,但是還是提供一種解決方式將gbk程式碼改成utf-8程式碼
程式碼如下:

package org.xc.binny;

import java.io.File;
import java.util.Collection;

import org.apache.commons.io.FileUtils;

public class GBK2UTF8App {
    /**
     * 將制定目錄下的所有Java原始檔的編碼格式從GBK修改為UTF-8
     */
    public static void main(String[] args) throws Exception {
        //GBK編碼格式原始碼路徑
        String srcDirPath = "C:\\
Users\\Wolf\\Desktop\\src"; // //轉為UTF-8編碼格式原始碼路徑E:\SVN3\20160918JunKang_Dev\JKDS\05原始碼\後\v1.0\parent\ins-service\src\main\java\com\junk String utf8DirPath = "C:\\Users\\Wolf\\Desktop\\src"; // String srcDirPath = "E:\\SVN3\\20160918JunKang_Dev\\JKDS\\05原始碼\\後端\\v1.0\\parent\\ins-common\\src\\main\\java\\com\\common"; // //轉為UTF-8編碼格式原始碼路徑E:\SVN3\20160918JunKang_Dev\JKDS\05原始碼\後\v1.0\parent\ins-service\src\main\java\com\junk // String utf8DirPath = "E:\\SVN3\\20160918JunKang_Dev\\JKDS\\05原始碼\\後端\\v1.0\\parent\\ins-common\\src\\main\\java\\com\\common"; // //獲取所有java檔案 Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[]{"java"}, true); int count=0; for (File javaGbkFile : javaGbkFileCol) { //UTF8格式檔案路徑 String utf8FilePath = utf8DirPath+javaGbkFile.getAbsolutePath().substring(srcDirPath.length()); //使用GBK讀取資料,然後用UTF-8寫入資料 // FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK")); FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK")); count++; System.out.println("執行檔案次數"+count); } } }

jar包:commons-io-1.4.jar
第二種是maven編譯的問題:
蒐羅pom檔案關於編譯的問題原來:
maven中的plugins 和 pluginManagement、dependencies和dependencyManagement。這兩個後者都需要放置在父檔案裡面,前者在子檔案裡。他們區別是:
maven會在當前專案中載入plugins宣告的外掛;

pluginManagement是表示外掛宣告,即你在專案中的pluginManagement下聲明瞭外掛,maven不會載入該外掛,pluginManagement宣告可以被繼承。

pluginManagement的一個使用案例是當有父子專案的時候,父專案中可以利用pluginManagement宣告子專案中需要用到的外掛, 之後,當某個或者某幾個子專案需要載入該外掛的時候,就可以在子專案中plugins節點只配置 groupId 和 artifactId就可以完成插 件的引用。
pluginManagement主要是為了統一管理外掛,確保所有子專案使用的外掛版本保持一致。
哈哈問題找到了那麼改一下吧:

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
......
</plugins>
<pluginManagement>
改成:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
......
</plugins>
<build>

那麼原因到底是什麼呢為啥呢gbk可以通過,utf-8不可以呢
由於系統預設編碼是GBK,因此預設可以gbk通過編譯。