maven的文件有自己的组织方式,例如以下所看到的:
---maven
----src
---main
----java
---test
----java
-----pom.xml
当运行完mvn compile之后会在src的同级文件夹生成target文件夹,里面包括.class文件
以下是俩个样例
example 1:
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-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zttc.itat.maven</groupId>
<artifactId>maven_ch01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
HelloMaven.java
package com.fjj;
public class HelloMaven {
public String sayHello(String name){
return "hello:"+name;
}
}
TestHelloMaven.java
package com.fjj;
import org.junit.*;
import static org.junit.Assert.*;
public class TestHelloMaven {
@Test
public void testSayHello(){
HelloMaven hm = new HelloMaven();
String str = hm.sayHello("maven");
assertEquals(str,"hello:maven");
}
}
运行mvn compile
然后运行mvn install (把生成的jar包放到本地repository中)
Example2
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-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zttc.itat.maven</groupId>
<artifactId>maven_ch02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>zttc.itat.maven</groupId>
<artifactId>maven_ch01</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Hello.java
package com.fjj;
import com.fjj.HelloMaven;
public class Hello {
public String say(String name){
HelloMaven hm = new HelloMaven();
return hm.sayHello("maven");
}
}
运行mvn compile
当中example2使用了example1生成的jar包。
运行mvn compile时,maven插件会依据pom.xml文件到本地repository去找相应的jar包,假设没有,则会去中央repository去下载相应的jar包,mvnrepository的网址为http://www.mvnrepository.com/能够在mvnrepository中找到每一个jar包的坐标。坐标由<groupId><artifactId><version>构成相应<项目><模块><版本号>.
eg:junit