[toc] ## Apache Maven (簡單使用介紹) (官網: https://maven.apache.org/) Maven是一個專案管理/構件工具 官方解釋: Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM). ### How to install (Windows): 前往 https://maven.apache.org/download.cgi 下載 Binary archive, 並unarchive. 設定Environment Variable: User variables: MAVEN_HOME, M2_HOME(value設為bin資料夾的path) System variables: Path (新增bin資料夾的path) ### 專案架構: ``` /Project /src ->程式碼 /main /java ->java程式碼 /resources ->資源,會被打包進jar, 如圖片/config檔等等 /test ->測試目錄 /target ->構建結果 pom.xml -> information ``` ### 專案識別: groupId:artifactId:version ### pom.xml #### XML格式 ```xml <!-- 註解 --> <attribute>value</attribute> <!--屬性存取--> <attr2>${attribute}</attr2> ``` pom.xml為此專案的設定檔 #### 依賴 一個專案A可以依賴另一個專案B 執行專案A的時候可以包含專案B的程式 但是A.jar並不會打包B.jar 需要用一些插件來達成, 像是"Maven Shade Plugin" 如果有用到依賴,依賴專案的.jar會存放在"local maven repo"裡 ```C:\\Users\user\.m2\repository\``` ```~/.m2/repository/``` 如果沒有, Maven會試圖從 repo1.maven.org/maven2/ 下載。 pom.xml的常見內容: ```xml= <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> <!-- the information of this project --> <groupId>groupId</groupId> <artifactId>artifactId</artifactId> <version>1.0-SNAPSHOT</version> <!-- project properties --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> </properties> <!-- dependencies --> <dependencies> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.11.0</version> </dependency> </dependencies> <!-- build settings --> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- your build plugins 常用的像是maven compile/maven shade --> </plugins> </build> </project> ``` ### 指令(mvn) 常用: ``` mvn -version //check the maven version mvn compile //compile the source mvn clean //remove target directory mvn install //install the project to local maven repo ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up