Maven创建项目

2018-01-09 19:18 更新

Maven教程 - Maven创建项目


在本章中,我们将使用Maven插件来创建一个新项目。创建一个Maven项目后,我们将添加一些Java源代码。

Maven使用 archetype maven-archetype-quickstart 插件插件来创建项目。

C:\\ mvn_test 文件夹中转到并执行以下mvn命令。

C:\mvn_test>mvn archetype:generate -DgroupId=com.java2s.ide -DartifactId=xmlFileEditor -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

这里是输出。

c:\mvn_test>mvn archetype:generate -DgroupId=com.java2s.ide -DartifactId=xmlFileEditor -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/archetype-catalog/2.2/archetype-catalog-2.2.pom
...
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/groovy/groovy/1.8.3/groovy-1.8.3.jar (5394 KB at 1501.4 KB/sec)
[INFO] Generating project in Batch mode
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: c:\mvn_test
[INFO] Parameter: package, Value: com.java2s.ide
[INFO] Parameter: groupId, Value: com.java2s.ide
[INFO] Parameter: artifactId, Value: xmlFileEditor
[INFO] Parameter: packageName, Value: com.java2s.ide
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: c:\mvn_test\xmlFileEditor
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.178 s
[INFO] Finished at: 2014-11-03T15:58:42-08:00
[INFO] Final Memory: 26M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test>

Maven生成的文件夹。

null

它还为我们创建了两个源文件,一个使用main方法另一个是单元测试用例。

package com.java2s.ide;

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

Maven还为生成的应用程序创建了测试用例。

package com.java2s.ide;

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 );
    }
}

生成的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.java2s.ide</groupId>
  <artifactId>xmlFileEditor</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>xmlFileEditor</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>


注意

Maven已经添加了Junit作为测试框架。默认情况下,Maven添加一个源文件 App.java 和在其默认目录结构中的测试文件 AppTest.java

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号