初設專案
Maven 專案命令是 mvn, mvn 主命令參數 archetype:generate 會根據 archetypeArtifactId 這參數, 來決定要初設何種專案.
Maven 小組提供一個專案模版網站, 網址 : http://www.mvnbrowser.com/index.html
初設 maven-archetype-quickstart 專案, 命令如下 :
Maven 專案命令是 mvn, mvn 主命令參數 archetype:generate 會根據 archetypeArtifactId 這參數, 來決定要初設何種專案.
Maven 小組提供一個專案模版網站, 網址 : http://www.mvnbrowser.com/index.html
初設 maven-archetype-quickstart 專案, 命令如下 :
$ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
:Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-5/plexus-interactivity-api-1.0-alpha-5.jar (14 KB at 6.6 KB/sec)
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker/2.0.10/maven-invoker-2.0.10.jar (28 KB at 12.8 KB/sec)
Downloaded: http://repo1.maven.org/maven2/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar (562 KB at 97.0 KB/sec)
[INFO] Generating project in Batch mode
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 7.4 KB/sec)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 1.2 KB/sec)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.mycompany.app
[INFO] Parameter: packageName, Value: com.mycompany.app
[INFO] Parameter: package, Value: com.mycompany.app
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: basedir, Value: /mnt/hda1/apache-maven-3.0.3
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: /mnt/hda1/apache-maven-3.0.3/my-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:35.260s
[INFO] Finished at: Sat Aug 06 09:14:53 UTC 2011
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
上述命令, 會下載 archetypeArtifactId 參數所指 maven-archetype-quickstart 專案, 所需的所有檔案 (*.jar, *.pom) 至指定的儲存目錄, 並且會根據 artifactId 參數, 產生 my-app 目錄
檢視儲存目錄內容, 如下 :
drwxr-xr-x 14 root root 4096 Aug 6 09:14 .
drwxr-xr-x 10 root root 4096 Aug 6 07:43 ..
drwxr-xr-x 3 root root 4096 Aug 6 09:14 classworlds
drwxr-xr-x 3 root root 4096 Aug 6 09:14 commons-cli
drwxr-xr-x 3 root root 4096 Aug 6 09:14 commons-collections
drwxr-xr-x 3 root root 4096 Aug 6 09:14 commons-io
drwxr-xr-x 3 root root 4096 Aug 6 09:14 commons-lang
drwxr-xr-x 3 root root 4096 Aug 6 09:14 dom4j
drwxr-xr-x 3 root root 4096 Aug 6 09:14 jdom
drwxr-xr-x 3 root root 4096 Aug 6 09:14 junit
drwxr-xr-x 3 root root 4096 Aug 6 09:14 net
drwxr-xr-x 4 root root 4096 Aug 6 09:13 org
drwxr-xr-x 3 root root 4096 Aug 6 09:14 oro
drwxr-xr-x 3 root root 4096 Aug 6 09:14 xml-apis
my-app 目錄結構, 如下 :
my-app |-- pom.xml `-- src |-- main | `-- java | `-- com | `-- mycompany | `-- app | `-- App.java `-- test `-- java `-- com `-- mycompany `-- app `-- AppTest.java
在 my-app 目錄中的 pom.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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</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>
</project>
專案建立
$ cd my-app # 一定要執行此命令
$ mvn package
:
:
xus-io-1.0.pom
Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/1.0/plexus-io-1.0.pom (2 KB at 2.1 KB/sec)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.4.1/maven-archiver-2.4.1.jar
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-archiver/1.0/plexus-archiver-1.0.jar
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/1.0/plexus-io-1.0.jar
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.4.1/maven-archiver-2.4.1.jar (20 KB at 16.3 KB/sec)
Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/1.0/plexus-io-1.0.jar (50 KB at 19.8 KB/sec)
Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-archiver/1.0/plexus-archiver-1.0.jar (174 KB at 46.9 KB/sec)
[INFO] Building jar: /mnt/hda1/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:20.944s
[INFO] Finished at: Sat Aug 06 10:20:02 UTC 2011
[INFO] Final Memory: 10M/25M
[INFO] ------------------------------------------------------------------------
執行專案
$ java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Hello World!
參考文章
1. Maven in 5 Minutes
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
No Response to "Apache Maven 專案管理平台 - Java 專案建立與執行"
張貼留言