Maven建立Java專案

五柳-先生發表於2015-07-21

Maven使用原型外掛來建立專案。要建立一個簡單的Java應用程式,我們使用maven-archetype-quickstart外掛。在下面的例子中,我們將建立一個基於Maven的Java應用程式專案在C:\MVN資料夾。

讓我們開啟命令控制檯,進入到C:\MVN目錄並執行以下命令mvn命令。

C:\MVN>mvn archetype:generate
-DgroupId=com.companyname.bank 
-DartifactId=consumerBanking 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false

Maven會開始處理,並建立完整的Java應用程式專案結構。

INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] -------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] -------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Batch mode
[INFO] -------------------------------------------------------------------
[INFO] Using following parameters for creating project 
 from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.companyname.bank
[INFO] Parameter: packageName, Value: com.companyname.bank
[INFO] Parameter: package, Value: com.companyname.bank
[INFO] Parameter: artifactId, Value: consumerBanking
[INFO] Parameter: basedir, Value: C:\MVN
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\MVN\consumerBanking
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: 14 seconds
[INFO] Finished at: Tue Jul 10 15:38:58 IST 2012
[INFO] Final Memory: 21M/124M
[INFO] ------------------------------------------------------------------

現在去到C:/ MVN目錄。將看到建立了一個Java應用程式專案命名consumerBanking(如artifactId規定)。 Maven使用標準的目錄結構如下圖所示:

Java application project structure

用上面的例子中,我們可以瞭解到以下關鍵概念

資料夾結構 描述
consumerBanking contains src folder and pom.xml
src/main/java contains java code files under the package structure (com/companyName/bank).
src/main/test contains test java code files under the package structure (com/companyName/bank).
src/main/resources it contains images/properties files (In above example, we need to create this structure manually).

Maven還建立了一個示例Java原始檔和Java測試檔案。開啟C:\MVN\consumerBanking\src\main\java\com\companyname\bank資料夾,會看到App.java。

package com.companyname.bank;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

開啟 C:\MVN\consumerBanking\src\test\java\com\companyname\bank 資料夾, 你會看到 AppTest.java.

package com.companyname.bank;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest extends TestCase 
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

開發人員把他們的檔案在提到上述表格和Maven處理所有構建相關的複雜性。

轉載: http://www.yiibai.com/maven/maven_creating_project.html

相關文章