ChienI Kao
筆記網站
JHTNT
筆記網站
# Maven download
https://maven.apache.org/download.cgi
# 環境變數設定
![](https://i.imgur.com/AWZuYAJ.png =400x)
# 確認有安裝好 Maven
mvn -v
mvn -version
# Create Project
Cmd
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
powershell
珍惜生命,學習 Java 請少用 PowerShell 當成你主要的 Shell 環境
mvn archetype:generate '-DgroupId=com.mycompany.app' -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart '-DarchetypeVersion=1.4' -DinteractiveMode=false
Git Bash
mvn archetype:generate \
-DgroupId=com.mycompany.app -DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 -DinteractiveMode=false
\ 換行只有 bash
以上皆無法
mvn archetype:generate
並選取以下功能
# Add exec-maven-plugin
<build>
<pluginManagement>
<plugins>
<!-- 其他 plugins ... -->
<!--===== 加入這段 =====-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.mycompany.app.App</mainClass>
</configuration>
</plugin>
<!--===== 加入這段 =====-->
<!-- 其他 plugins ... -->
</plugins>
</pluginManagement>
</build>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jar.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution><!--設定Goal的執行方式-->
<goals>
<goal>java</goal> <!--要設定的goal-->
</goals>
</execution>
</executions>
<configuration>
<mainClass>jar.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
# 清除 target
mvn clean
# 產生執行檔
Compile: 通常只下這行就可以執行了
mvn compile
Building: 會完整的建構整個專案,並把專案打包成.jar 檔,供別人下載執行。但會產出較多檔案,且耗費較多時間
mvn package
# 執行
Execute
mvn exec:java