创建maven工程
原创
©著作权归作者所有:来自51CTO博客作者hoho_12的原创作品,请联系作者获取转载授权,否则将追究法律责任
maven工程结构
Project
|--src(源码包)
|--main(正常的源码包)
|--java(.java文件的目录)
|--resources(资源文件的目录)
|--test(测试的源码包)
|--java
|--resources
|--target(class文件、报告等信息存储的地方)
|--pom.xml(maven工程的描述文件)
创建maven工程
eclipse mars 4.5.0版本自带maven插件
通过骨架创建
file——》new Maven Project——》maven-archetype-quickstart
MavenFirst.java
package com.wxh.maven;
public class MavenFirst {
public String sayHello(String name){
return "hello "+name;
}
}
TestMavenFirst.java
package com.wxh.maven;
import org.junit.Test;
import junit.framework.Assert;
public class TestMavenFirst {
@Test
public void testSayHello(){
MavenFirst mf=new MavenFirst();
String result=mf.sayHello("maven");
Assert.assertEquals("hello maven", result);
}
}
pom.xml自动配好了
跳过骨架选择
file——》new Maven Project——》(勾选上 create a simple project)
有三种打包方式可供选择:jar,war,pom
MavenSecond.java
package com.wxh.maven;
public class MavenSecond {
public String sayHello(String name){
MavenFirst first=new MavenFirst();
return first.sayHello(name);
}
}
引用了第一个工程中的类,可能不能编译,clean一下工程就行了。
通过配置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.wxh.maven</groupId>
<artifactId>MavenSecond</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.wxh.maven</groupId>
<artifactId>MavenFirst</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
</dependencies>
</project>
TestMavenSecond.java
package com.wxh.maven;
import org.junit.Test;
import junit.framework.Assert;
public class TestMavenSecond {
@Test
public void sayHello(){
MavenSecond second=new MavenSecond();
String result=second.sayHello("wxh");;
Assert.assertEquals("wxh", result);
}
}