1.新建hello_world主目录;
2.在主目录下新建pom.xml文件,输入如下内容;
<?xml version="1.0" encoding="UTF-8"?> <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-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.iteye.xujava.hello_world</groupId> <artifactId>hello-world</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven Hello World Project</name> </project>
3.在主目录下新建目录src/main/java,在此目录下新建包路径com.iteye.xujava.hello_world。在该目录下新建HelloWorld.java,内容如下:
package com.iteye.xujava.hello_world; public class HelloWorld { public String sayHello(){ return "Hello Maven"; } public static void main(String[] args){ System.out.println(new HelloWorld().sayHello()); } }
4.在命令提示行中进入主目录,输入命令mvn clean compile进行编辑。clean为清理、compile为编译。
5.在pom.xml中追加junit的依赖,POM文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <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-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.iteye.xujava.hello_world</groupId> <artifactId>hello-world</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven Hello World Project</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies> </project>
6.在主目录下添加src/test/java目录,在该目录下添加包路径com.iteye.xujava.hello_world,在该目录下添加HelloWorldTest.java,内容如下:
package com.iteye.xujava.hello_world; import static org.junit.Assert.assertEquals; import org.junit.Test; public class HelloWorldTest { @Test public void testSayHello(){ HelloWorld helloWorld=new HelloWorld(); String result=helloWorld.sayHello(); assertEquals("Hello Maven",result); } }
7.在命令提示行中输入mvn clean test进行测试
8.在命令提示行中输入mvn clean package打包
9.如果想运行Jar包,在 pom.xml中添加插入,POM内容如下:
<?xml version="1.0" encoding="UTF-8"?> <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-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.iteye.xujava.hello_world</groupId> <artifactId>hello-world</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven Hello World Project</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.iteye.xujava.hello_world.HelloWorld</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
9.在命令提示行输入mvn clean package打包
10.在命令提示行输入java -jar target\hello-world-1.0.0-SNAPSHOT.jar
输出:Hello Maven
11.在命令提示行输入mvn clean install安装到Maven库
12.在每个命令后面加上-X,Debug模式
13.可以直接运行mvn archetype:generate生成项目框架
下面是运行这个命令的执行过程:
E:\mavenspace>mvn archetype:generate ...此处内容自动生成,太多,省略。。。 Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co ntains): 280:(此处直接回车) Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 1: 1.0-alpha-1 2: 1.0-alpha-2 3: 1.0-alpha-3 4: 1.0-alpha-4 5: 1.0 6: 1.1 Choose a number: 6:(此处直接回车) Define value for property 'groupId': : com.iteye.xujava(此处输入groupId) Define value for property 'artifactId': : helloworld(此处输入artifactId) Define value for property 'version': 1.0-SNAPSHOT: :(此处直接回车或输入自己的版本号) Define value for property 'package': com.iteye.xujava: : com.iteye.xujava.helloworld(此处直接回国或输入自己的包) Confirm properties configuration: groupId: com.iteye.xujava artifactId: helloworld version: 1.0-SNAPSHOT package: com.iteye.xujava.helloworld Y: : Y(此处输入Y) [INFO] ------------------------------------------------------------------------- --- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1 [INFO] ------------------------------------------------------------------------- --- [INFO] Parameter: groupId, Value: com.iteye.xujava [INFO] Parameter: packageName, Value: com.iteye.xujava.helloworld [INFO] Parameter: package, Value: com.iteye.xujava.helloworld [INFO] Parameter: artifactId, Value: helloworld [INFO] Parameter: basedir, Value: E:\mavenspace [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: E:\mavenspace\helloworld [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1:06.388s [INFO] Finished at: Sat Jun 15 16:28:34 CST 2013 [INFO] Final Memory: 10M/110M [INFO] ------------------------------------------------------------------------ E:\mavenspace>
完成后,项目框架及POM文件已经生成,自动生成的POM文件如下。
<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>com.iteye.xujava</groupId> <artifactId>helloworld</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>helloworld</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>