Add compiler argument to build Maven project

English 简体中文 繁体中文 ภาษาไทย Tiếng Việt
Summary

Maven projects often require passing specific arguments to the Java compiler, such as defining source and target releases or handling options like -XDignore.symbol.file for Java 8's modularized source code. To integrate these arguments into a Maven build, developers must configure the maven-compiler-plugin within their project's pom.xml. For plugin versions 3.1 and later, compiler arguments are specified using the block containing multiple elements. Earlier versions (prior to 3.1) utilize a single tag, which has the limitation of only processing the last argument if multiple are provided. Therefore, it is strongly recommended to use maven-compiler-plugin version 3.1 or newer for reliable and flexible compiler argument management.

Maven คือซอฟต์แวร์โปรเจ็กต์ที่ใช้เพื่อจัดการการสร้าง การรายงาน และเอกสารของโปรเจ็กต์จากข้อมูลส่วนกลาง ตอนนี้มีการใช้กันอย่างแพร่หลายในการสร้างและปรับใช้โปรเจ็กต์ ซึ่งสามารถช่วยดูแลการพึ่งพาของโปรเจ็กต์ได้โดยอัตโนมัติ โดยมีไฟล์กำหนดค่าโปรเจ็กต์ส่วนกลางชื่อ pom.xml ซึ่งคุณสามารถกำหนดค่าโปรเจ็กต์ที่คุณต้องการสร้างได้ที่นี่

ในโพสต์นี้ เราจะแสดงวิธีเพิ่มอาร์กิวเมนต์คอมไพเลอร์เมื่อใช้ javac เพื่อคอมไพล์ซอร์สโค้ด Java บางครั้งเราจำเป็นต้องส่งอาร์กิวเมนต์คอมไพเลอร์เมื่อเราคอมไพล์ซอร์สโค้ด ตัวอย่างเช่น เราอาจต้องการระบุ -source และ -target release ของโค้ด โดยเฉพาะอย่างยิ่งใน Java 8 ล่าสุด ซอร์สโค้ดได้รับการแยกส่วนเป็นโมดูลแล้ว เมื่อ javac กำลังคอมไพล์โค้ด จะไม่ได้ลิงก์กับ rt.jar โดยค่าเริ่มต้นใน Java 8 ล่าสุด แต่จะใช้ไฟล์สัญลักษณ์พิเศษ lib/ct.sym ที่มีสแต็บคลาสแทน ตัวเลือก -XDignore.symbol.file ใช้เพื่อละเว้นไฟล์สัญลักษณ์เพื่อให้ลิงก์กับ rt.jar

ในการเพิ่มอาร์กิวเมนต์คอมไพเลอร์ คุณต้องเพิ่มปลั๊กอินคอมไพเลอร์ลงในไฟล์ pom.xml ของโปรเจ็กต์ของคุณ นี่คือตัวอย่างการกำหนดค่าปลั๊กอิน:

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.3</version>
			<configuration>
				<source>1.8</source> 
				<target>1.8</target> 
				<fork>true</fork>
				<compilerArgs>
					<arg>-XDignore.symbol.file</arg>
				</compilerArgs>
			</configuration>
		</plugin>
	</plugins>
</build>

ในการกำหนดค่านี้ เวอร์ชันของปลั๊กอินมีความสำคัญมาก ก่อนหน้า maven-compiler-plugin 3.1 อาร์กิวเมนต์คอมไพเลอร์ควรเป็น <compilerArgument>-XDignore.symbol.file</compilerArgument> แทน และมีข้อจำกัด หากคุณใส่ <compilerArgument>...</compilerArgument>s หลายรายการ จะมีเพียงรายการสุดท้ายเท่านั้นที่จะถูกเลือก และรายการอื่นๆ ทั้งหมดจะถูกละเว้น

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.0</version>
			<configuration>
				<source>1.8</source> 
				<target>1.8</target> 
				<fork>true</fork>
				<compilerArgument>-XDignore.symbol.file</compilerArgument>
			</configuration>
		</plugin>
	</plugins>
</build>

ดังนั้น เราขอแนะนำอย่างยิ่งให้คุณใช้ปลั๊กอินตั้งแต่ 3.1 ขึ้นไป ในบล็อก <configuration> คุณสามารถระบุ source และ target release ของโค้ดได้ และยังมี รายการตัวเลือก javac ที่สามารถระบุได้

คุณสามารถรัน maven ด้วยตัวเลือกบรรทัดคำสั่ง -X เพื่อแสดงข้อมูลการดีบัก ซึ่งคุณจะเห็นอาร์กิวเมนต์คอมไพเลอร์ที่คุณระบุ ตัวอย่างเช่น:

MAVEN COMPILER ARGUMENT COMPILER OPTION JAVA 8

  RELATED

  COMMENT

1
Rohit
Apr 12, 2015 at 11:01 pm

Good tutorial Keep it up.