maven(1)--环境搭建

来源:互联网 发布:足球球员数据 编辑:程序博客网 时间:2024/05/14 15:35
1.maven环境搭建:

1)到官网下载maven http://maven.apache.org/download.cgi

2)解压

3)配置环境变量:


4)测试在cmd中输入mvn -v 将显示一下信息


2.Hello world(maven)

1)建立目录:D:\mavenDemo\maven_ch01\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>cn.edu.zttc.maven.hello</groupId>  <artifactId>hello-first</artifactId>  <version>1.0-SNAPSHOT</version>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.10</version>      <scope>test</scope>    </dependency>  </dependencies></project>

建立目录:D:\mavenDemo\maven_ch01\src\main\java\cn\edu\zttc\hello

Hello.java:

package cn.edu.zttc.hello;public class Hello{public String sayHello(String name){return "hello:"+name;}public static void main(String[] args){System.out.println("hello world");}}

在cmd中输入 mvn complie:



我的遇到了如上错误 解决放按:


观察目录结构:多了D:\mavenDemo\maven_ch01\target

新建目录:D:\mavenDemo\maven_ch01\src\test\java\cn\edu\zttc\hello

TestHello.java

package cn.edu.zttc.hello;import org.junit.*;import static junit.framework.Assert.*;import cn.edu.zttc.hello.*;public class TestHello{@Testpublic void testHello(){Hello h = new Hello();assertEquals(h.sayHello("zs"),"hello:zs");}}

在命令行中输入 mvn test


测试成功


在 命令行中输入:

mvn clean


观察目录变化 少了target 目录


在cmd中数据 mvn clean package 在执行clean后 还会生成帮我们自动编译




也会自动打jar包:


大家也可以自己试一试mvn clean install


3.建立模块maven_ch02

D:\mavenDemo\maven_ch02\src\main\java\cn\edu\zttc\world

建立World.java

package cn.edu.zttc.world;import cn.edu.zttc.hello.*;public class World{public static void main(String[] args){Hello h = new Hello();h.sayHello("李四");System.out.println(h.hello());}}

在world中添加新的方法 hello

package cn.edu.zttc.hello;public class Hello{public String sayHello(String name){return "hello:"+name;}public static void main(String[] args){System.out.println("hello world");}public String hello(){return "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>cn.edu.zttc.maven.hello</groupId>  <artifactId>hello-second</artifactId>  <version>1.0-SNAPSHOT</version>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.10</version>      <scope>test</scope>    </dependency> <dependency>      <groupId>cn.edu.zttc.maven.hello</groupId>  <artifactId>hello-first</artifactId>  <version>1.0-SNAPSHOT</version>      <scope>compile</scope>    </dependency>  </dependencies></project>

在cmd中重新编译:D:\mavenDemo\maven_ch01>mvn clean package

继续编译:D:\mavenDemo\maven_ch02>mvn clean package


工程maven_ch02 编译成功

0 0
原创粉丝点击