清理
编译
测试
打包
运行
部署
hello\pom.xml
POM:Project Object Model,项目对象模型
pom.xml与ant的build.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>com.demo.hello</groupId> <artifactId>hello-world</artifactId> <version>1.0.0-SNAPSHOT</version> <name>hello</name> </project>
说明:
modelVersion:指定当前POM模型的版本,Maven2及Maven3只能是4.0.0
groupId:项目组名称
artifactId:当前Maven项目在组中的唯一的id
version:版本
hello\src\main\java下的Hello.java
package com.demo ; public class Hello { public void sayHi(){ System.out.println("hello world"); } public static void main(String [] args){ new Hello().sayHi(); } }
运行清理及编译命令:
进入hello的文件夹路径,运行
mvn clean compile
运行结果
查看target文件夹的内容
查看本地仓库
hello\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>com.demo.hello</groupId> <artifactId>hello-world</artifactId> <version>1.0.0-SNAPSHOT</version> <name>hello</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project>
dependencies:指明这个项目所需要的依赖包
hello\src\test\java\HelloTest.java
package com.demo; import org.junit.Test; public class HelloTest { @Test public void testHello(){ new Hello().sayHi(); } }
运行测试命令
mvn clean test
查看结果
查看本地仓库
运行命令
mvn clean package
查看结果
重新改造pom.xml,加入插件
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>com.demo.hello</groupId> <artifactId>hello-world</artifactId> <version>1.0.0-SNAPSHOT</version> <name>hello</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <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.demo.Hello</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
重新运行打包命令:
mvn clean package
执行完毕后,进入hello/target下,运行命令
java -jar hello-world-1.0.0-SNAPSHOT.jar
查看结果:
安装到本地仓库,供其它项目依赖
运行命令
mvn clean install
查看本地仓库
项目构建之maven篇:2.HelloWorld项目构建过程,布布扣,bubuko.com
项目构建之maven篇:2.HelloWorld项目构建过程
原文地址:http://blog.csdn.net/wobendiankun/article/details/30123299