1. 创建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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>archetype.groupId</groupId> <artifactId>simple-archetype</artifactId> <version>1.0</version> <packaging>jar</packaging> </project>
这个pom文件中定义的元素标签与mvn archetype:generate 可选参数相对应。
| pom.xml中元素标签 | mvn archetype:generate 可选参数 |
| archifactId | archetypeArtifactId |
| groupId | archetypeGroupId |
| version | archetypeVersion |
2. 创建archetype.xml
<archetype xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd">
<id>simple-archetype</id>
<sources>
<source>src/main/java/App.java</source>
</sources>
<testSources>
<source>src/test/java/AppTest.java</source>
</testSources>
</archetype>
archetype.xml 是一个文件组织架构描述文件。
- <sources> = src/main/java
- <resources> = src/main/resources
- <testSources> = src/test/java
- <testResources> = src/test/resources
- <siteResources> = src/site
3. 创建原型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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>jar</packaging>
<name>A custom project</name>
<url>http://www.myorganization.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这个pom文件中定义的元素标签与mvn archetype:generate 可选参数相对应。这个pom定义了要生成的文件架构的archefact,group id 和 version架构。
| pom.xml中元素标签 | mvn archetype:generate 可选参数 |
| archifactId | archifactId |
| groupId | groupId |
| version | version |
4. 创建App.java 和 AppTest.java.
App.java
package com.mycompany.app;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
AppTest.java
package com.mycompany.app;
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 );
}
}
5. 创建如下文件结构
6. 打开1中pom.xml所在目录。 执行mvn install 进行安装.

7. 创建一个新目录。执行以下命令
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DinteractiveMode=false -DarchetypeArtifactId=simple-archetype -DarchetypeGroupId=archetype.groupId -DarchetypeVersion=1.0 -DarchetypeCatalog=local
就可以查看到生成的文件骨架了。

本文详细介绍了如何使用Maven创建项目,包括创建pom.xml、archetype.xml、原型pom.xml、App.java和AppTest.java,以及如何利用mvnarchetype:generate命令生成所需的项目结构。
1448

被折叠的 条评论
为什么被折叠?



