pom.xml (โมเดลวัตถุโครงการ) คืออะไร? ตัวอย่าง pom.xml ให้คุณใช้ใน Dynamic Web Project (Java / J2EE)
เผยแพร่แล้ว: 2021-06-25
ไฟล์ pom.xml คืออะไร?
POM หมายถึง Project Object Model
เป็นหน่วยพื้นฐานของงานใน Maven ไฟล์ pom.xml มีการกำหนดค่าทั้งหมด การพึ่งพา การรวมปลั๊กอินและคำแนะนำในการตั้งค่าโครงการ
นี่เป็นวิธีที่ดีที่สุดในการเพิ่มการพึ่งพาไลบรารีลงในโปรเจ็กต์ของคุณ การคัดลอกไฟล์ .jar ลงในโฟลเดอร์ทรัพยากรของโปรเจ็กต์เป็นวิธีการแบบเก่า
มาทำความเข้าใจไฟล์ pom.xml อย่างละเอียดกัน
ไฟล์ pom.xml เริ่มต้นที่ง่ายที่สุด
1 2 3 4 5 6 7 8 9 10 |
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com . crunchify . tutorials < / groupId > < artifactId > crunchify - tutorials < / artifactId > < version > 1.0.1 - SNAPSHOT < / version > < / project > |
มาดูองค์ประกอบ pom.xml กัน:
องค์ประกอบ | คำอธิบาย |
---|---|
โครงการ | อิลิเมนต์เป็นรูทของตัวอธิบาย ตารางต่อไปนี้แสดงรายการองค์ประกอบย่อยที่เป็นไปได้ทั้งหมด |
รุ่นรุ่น | ประกาศเวอร์ชันของตัวอธิบายโปรเจ็กต์ POM นี้สอดคล้องกับเวอร์ชันใด ตั้งเป็นเวอร์ชัน 4.0.0 |
groupId | ตัวระบุที่ไม่ซ้ำกันในระดับสากลสำหรับโปรเจ็กต์ เป็นเรื่องปกติที่จะใช้ชื่อแพ็คเกจที่มีคุณสมบัติครบถ้วนเพื่อแยกความแตกต่างจากโปรเจ็กต์อื่นที่มีชื่อคล้ายกัน (เช่น org.apache.maven) |
รหัสสิ่งประดิษฐ์ | ตัวระบุสำหรับอาร์ติแฟกต์นี้ที่ไม่ซ้ำกันภายในกลุ่มที่กำหนดโดย ID กลุ่ม สิ่งประดิษฐ์คือสิ่งที่สร้างขึ้นหรือใช้โดยโครงการ ตัวอย่างของสิ่งประดิษฐ์ที่ผลิตโดย Maven สำหรับโปรเจ็กต์ ได้แก่ JAR, การแจกแจงแบบซอร์สและไบนารี และ WAR |
รุ่น | อาร์ติแฟกต์เวอร์ชันปัจจุบันที่สร้างโดยโปรเจ็กต์นี้ |
ไฟล์ pom.xml พร้อมองค์ประกอบเพิ่มเติม
ไฟล์ pom.xml พร้อมองค์ประกอบเพิ่มเติม:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com . crunchify . tutorials < / groupId > < artifactId > crunchify - tutorials < / artifactId > < version > 1.0.1 - SNAPSHOT < / version > < packaging > jar < / packaging > < name > Crunchify ' s Java Tutorials < / name > < url > https : //crunchify.com</url> < build > < sourceDirectory > src < / sourceDirectory > < finalName > crunchify - tutorial < / finalName > < plugins > < plugin > < groupId > org . apache . maven . plugins < / groupId > < artifactId > maven - compiler - plugin < / artifactId > < version > 3.5.1 < / version > < configuration > < source > 15 < / source > < target > 15 < / target > < / configuration > < / plugin > < / plugins > < / build > < dependencies > < dependency > < groupId > org . glassfish < / groupId > < artifactId > javax . json < / artifactId > < version > 1.0.4 < / version > < / dependency > < dependency > < groupId > com . github . wnameless < / groupId > < artifactId > json - flattener < / artifactId > < version > 0.2.2 < / version > < / dependency > < dependency > < groupId > commons - io < / groupId > < artifactId > commons - io < / artifactId > < version > 2.10.0 < / version > < / dependency > < dependency > < groupId > org . apache . commons < / groupId > < artifactId > commons - lang3 < / artifactId > < version > 3.10 < / version > < / dependency > < / dependencies > < / project > |
องค์ประกอบ | คำอธิบาย |
---|---|
บรรจุภัณฑ์ | ประเภทของสิ่งประดิษฐ์ที่โปรเจ็กต์นี้ผลิตขึ้น เช่น jar war ear pom ปลั๊กอินสามารถสร้างบรรจุภัณฑ์ของตนเองได้ และด้วยเหตุนี้จึงมีประเภทบรรจุภัณฑ์ของตัวเอง ดังนั้นรายการนี้จึงไม่มีประเภทที่เป็นไปได้ทั้งหมด |
ชื่อ | ชื่อเต็มของโครงการ |
url | URL ไปยังหน้าแรกของโครงการ |
การพึ่งพา | องค์ประกอบนี้อธิบายการพึ่งพาทั้งหมดที่เกี่ยวข้องกับโครงการ การพึ่งพาเหล่านี้ใช้เพื่อสร้าง classpath สำหรับโครงการของคุณระหว่างกระบวนการสร้าง จะถูกดาวน์โหลดโดยอัตโนมัติจากที่เก็บที่กำหนดไว้ในโปรเจ็กต์นี้ |
การพึ่งพา | กลุ่มโปรเจ็กต์ที่สร้างการพึ่งพา เช่น org.apache.maven |
สร้าง | ข้อมูลที่จำเป็นในการสร้างโครงการ |
sourceDirectory | อิลิเมนต์นี้ระบุไดเร็กทอรีที่มีแหล่งที่มาของโปรเจ็กต์ ระบบบิลด์ที่สร้างขึ้นจะรวบรวมซอร์สในไดเร็กทอรีนี้เมื่อสร้างโปรเจ็กต์ เส้นทางที่กำหนดจะสัมพันธ์กับตัวอธิบายโปรเจ็กต์ |
สุดท้ายชื่อ | ชื่อไฟล์ (ไม่รวมส่วนขยาย และไม่มีข้อมูลพาธ) ที่อาร์ติแฟกต์ที่สร้างขึ้นจะถูกเรียก ค่าเริ่มต้นคือ ${artifactId}-${version} |
ปลั๊กอิน | รายการปลั๊กอินที่จะใช้ |
เสียบเข้าไป | ID กลุ่มของปลั๊กอินในที่เก็บ |
ขอบเขต | ขอบเขตของการพึ่งพา – คอมไพล์ รันไทม์ ทดสอบ ระบบ และจัดเตรียม ใช้ในการคำนวณ classpaths ต่างๆ ที่ใช้ในการคอมไพล์ การทดสอบ และอื่นๆ นอกจากนี้ยังช่วยในการกำหนดสิ่งประดิษฐ์ที่จะรวมในการแจกจ่ายโครงการนี้ |
เราได้เผยแพร่บทช่วยสอน Java มากกว่า 700 รายการบน Crunchify และเราใช้ไฟล์ pom.xml ตั้งแต่เริ่มต้น ซึ่งรวมถึงการอ้างอิง Spring MVC
ในช่วงไม่กี่ปีที่ผ่านมาเนื่องจากการเพิ่มการพึ่งพาไลบรารีมากขึ้นเรื่อย ๆ ไฟล์ Java tutorial pom.xml ของ Crunchify จึงใหญ่ขึ้น
นี่คือไฟล์ pom.xml ปัจจุบันของเราสำหรับการอ้างอิงของคุณ หากคุณคัดลอกและเพิ่มลงในโครงการของคุณ นั่นก็เหมาะกับคุณเช่นกัน
ไฟล์ pom.xml ที่เราใช้สำหรับเครื่องมือ Java ของ Crunchify
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com . crunchify . tutorials < / groupId > < artifactId > crunchify - tutorials < / artifactId > < version > 1.0.1 - SNAPSHOT < / version > < packaging > jar < / packaging > < name > Crunchify 's Java Tutorials</name> <url>https://crunchify.com</url> <build> <sourceDirectory>src</sourceDirectory> <finalName>crunchify-tutorial</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>15</source> <target>15</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.3</version> <configuration> <descriptors> <descriptor>src/assembly/crunchify.xml</descriptor> </descriptors> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!--This plugin' s configuration is used to store Eclipse m2e settings only . It has no influence on the Maven build itself . -- > < plugin > < groupId > org . eclipse . m2e < / groupId > < artifactId > lifecycle - mapping < / artifactId > < version > 1.0.0 < / version > < configuration > < lifecycleMappingMetadata > < pluginExecutions > < pluginExecution > < pluginExecutionFilter > < groupId > org . apache . maven . plugins < / groupId > < artifactId > maven - compiler - plugin < / artifactId > < versionRange > [ 3.1 , ) < / versionRange > < goals > < goal > compile < / goal > < / goals > < / pluginExecutionFilter > < action > < ignore / > < / action > < / pluginExecution > < pluginExecution > < pluginExecutionFilter > < groupId / > < artifactId / > < versionRange > [ 3.1 , ) < / versionRange > < goals > < goal / > < / goals > < / pluginExecutionFilter > < action > < ignore / > < / action > < / pluginExecution > < / pluginExecutions > < / lifecycleMappingMetadata > < / configuration > < / plugin > < / plugins > < / pluginManagement > < / build > < dependencies > < dependency > < groupId > org . glassfish < / groupId > < artifactId > javax . json < / artifactId > < version > 1.1.4 < / version > < / dependency > < dependency > < groupId > com . github . wnameless . json < / groupId > < artifactId > json - flattener < / artifactId > < version > 0.12.0 < / version > < / dependency > < dependency > < groupId > commons - io < / groupId > < artifactId > commons - io < / artifactId > < version > 2.10.0 < / version > < / dependency > < dependency > < groupId > com . google . code . gson < / groupId > < artifactId > gson < / artifactId > < version > 2.8.7 < / version > < / dependency > < dependency > < groupId > net . jodah < / groupId > < artifactId > expiringmap < / artifactId > < version > 0.5.9 < / version > < / dependency > < dependency > < groupId > org . apache . httpcomponents < / groupId > < artifactId > httpclient < / artifactId > < version > 4.5.13 < / version > < / dependency > < dependency > < groupId > org . json < / groupId > < artifactId > json < / artifactId > < version > 20210307 < / version > < / dependency > < dependency > < groupId > net . spy < / groupId > < artifactId > spymemcached < / artifactId > < version > 2.12.3 < / version > < / dependency > < dependency > < groupId > com . whalin < / groupId > < artifactId > Memcached - Java - Client < / artifactId > < version > 3.0.2 < / version > < / dependency > < dependency > < groupId > com . googlecode . xmemcached < / groupId > < artifactId > xmemcached < / artifactId > < version > 2.4.7 < / version > < / dependency > < dependency > < groupId > com . paypal . sdk < / groupId > < artifactId > rest - api - sdk < / artifactId > < version > LATEST < / version > < / dependency > < dependency > < groupId > org . apache . commons < / groupId > < artifactId > commons - dbcp2 < / artifactId > < version > 2.8.0 < / version > < / dependency > < dependency > < groupId > commons - dbcp < / groupId > < artifactId > commons - dbcp < / artifactId > < version > 20030825.184428 < / version > < / dependency > < dependency > < groupId > javax . ws . rs < / groupId > < artifactId > javax . ws . rs - api < / artifactId > < version > 2.1.1 < / version > < / dependency > < dependency > < groupId > org . hamcrest < / groupId > < artifactId > hamcrest - all < / artifactId > < version > 1.3 < / version > < / dependency > < dependency > < groupId > org . apache . logging . log4j < / groupId > < artifactId > log4j - core < / artifactId > < version > 2.16.0 < / version > < / dependency > < dependency > < groupId > com . google . guava < / groupId > < artifactId > guava < / artifactId > < version > 30.1.1 - jre < / version > < / dependency > < dependency > < groupId > com . googlecode . json - simple < / groupId > < artifactId > json - simple < / artifactId > < version > 1.1.1 < / version > < / dependency > < dependency > < groupId > commons - net < / groupId > < artifactId > commons - net < / artifactId > < version > 3.8.0 < / version > < / dependency > < dependency > < groupId > org . ow2 . asm < / groupId > < artifactId > asm < / artifactId > < version > 9.1 < / version > < / dependency > < dependency > < groupId > axis < / groupId > < artifactId > axis < / artifactId > < version > 1.4 < / version > < / dependency > < dependency > < groupId > commons - beanutils < / groupId > < artifactId > commons - beanutils < / artifactId > < version > 1.9.4 < / version > < / dependency > < dependency > < groupId > org . apache . commons < / groupId > < artifactId > commons - collections4 < / artifactId > < version > 4.4 < / version > < / dependency > < dependency > < groupId > org . apache . commons < / groupId > < artifactId > commons - configuration2 < / artifactId > < version > 2.7 < / version > < / dependency > < dependency > < groupId > commons - discovery < / groupId > < artifactId > commons - discovery < / artifactId > < version > 0.5 < / version > < / dependency > < dependency > < groupId > org . apache . commons < / groupId > < artifactId > commons - lang3 < / artifactId > < version > 3.12.0 < / version > < / dependency > < dependency > < groupId > commons - logging < / groupId > < artifactId > commons - logging < / artifactId > < version > 1.2 < / version > < / dependency > < dependency > < groupId > commons - logging < / groupId > < artifactId > commons - logging - api < / artifactId > < version > 1.1 < / version > < / dependency > < dependency > < groupId > javax . mail < / groupId > < artifactId > javax . mail - api < / artifactId > < version > 1.6.2 < / version > < / dependency > < dependency > < groupId > javax . mail < / groupId > < artifactId > mail < / artifactId > < version > 1.4.7 < / version > < / dependency > < dependency > < groupId > javax . xml < / groupId > < artifactId > jaxrpc - api < / artifactId > < version > 1.1 < / version > < / dependency > < dependency > < groupId > javax . servlet < / groupId > < artifactId > javax . servlet - api < / artifactId > < version > 4.0.1 < / version > < scope > provided < / scope > < / dependency > < dependency > < groupId > org . apache . axis < / groupId > < artifactId > axis - saaj < / artifactId > < version > 1.4 < / version > < / dependency > < dependency > < groupId > wsdl4j < / groupId > < artifactId > wsdl4j < / artifactId > < version > 1.6.3 < / version > < / dependency > < dependency > < groupId > com . google . zxing < / groupId > < artifactId > core < / artifactId > < version > 3.4.1 < / version > < / dependency > < dependency > < groupId > org . apache . commons < / groupId > < artifactId > commons - compress < / artifactId > < version > 1.20 < / version > < / dependency > < dependency > < groupId > mysql < / groupId > < artifactId > mysql - connector - java < / artifactId > < version > 8.0.25 < / version > < / dependency > < dependency > < groupId > org . junit . jupiter < / groupId > < artifactId > junit - jupiter - api < / artifactId > < version > 5.8.0 - M1 < / version > < scope > test < / scope > < / dependency > < dependency > < groupId > ch . qos . logback < / groupId > < artifactId > logback - classic < / artifactId > < version > 1.3.0 - alpha5 < / version > < / dependency > < dependency > < groupId > commons - validator < / groupId > < artifactId > commons - validator < / artifactId > < version > 1.7 < / version > < / dependency > < dependency > < groupId > org . springframework < / groupId > < artifactId > spring - context < / artifactId > < version > 5.3.8 < / version > < / dependency > < dependency > < groupId > org . springframework < / groupId > < artifactId > spring - context - support < / artifactId > < version > 5.3.8 < / version > < / dependency > < dependency > < groupId > org . slf4j < / groupId > < artifactId > slf4j - api < / artifactId > < version > 1.7.31 < / version > < / dependency > < dependency > < groupId > org . slf4j < / groupId > < artifactId > jul - to - slf4j < / artifactId > < version > 1.7.31 < / version > < / dependency > < dependency > < groupId > org . slf4j < / groupId > < artifactId > jcl - over - slf4j < / artifactId > < version > 1.7.31 < / version > < / dependency > < dependency > < groupId > org . slf4j < / groupId > < artifactId > log4j - over - slf4j < / artifactId > < version > 1.7.31 < / version > < / dependency > < / dependencies > < / project > |
ฉันหวังว่าสิ่งนี้จะชี้แจงว่าไฟล์ pom.xml คืออะไรและทำไมเราจึงใช้ในโครงการ J2EE ของ Java

โปรดแจ้งให้เราทราบหากคุณมีคำถามเพิ่มเติม
อัปเดต Log4j เป็นเวอร์ชันล่าสุด
CVE-2021-44228: คุณสมบัติ Apache Log4j2 <=2.14.1 JNDI ที่ใช้ในการกำหนดค่า ข้อความบันทึก และพารามิเตอร์ไม่ได้ป้องกัน LDAP ที่ควบคุมโดยผู้โจมตีและปลายทางอื่นที่เกี่ยวข้องกับ JNDI
จาก log4j 2.16.0 ลักษณะการทำงานนี้ถูกปิดใช้งานโดยค่าเริ่มต้น