[size=x-large][color=red]本文出自此处:[url=http://www.naxsu.com/maven_-yi-lai/]http://www.naxsu.com/maven_-yi-lai/[/url]
更多资源请访问:[url=http://www.naxsu.com]http://www.naxsu.com[/url][/color][/size]
依赖包的查询
1、所有的依赖都是通过坐标来进行存储的(GAV-->groupId、artifactId、version)
2、有一些网上的仓库提供了坐标的查询(http://mvnrepository.com)
3、通过<dependencies>设置依赖
maven是如何搜索依赖的?首先会在本地仓库查询如果本地仓库没有,就去中央仓库查询
依赖的传递性
1、依赖是会被传递
A-->C B-->A ==> B-->C(这种依赖是基于compile这个范围进行传递)
对于依赖的传递而言,主要是针对compile作用域传递
传递的冲突问题
1、a-->b1.0 c-->b1.1 d-->a和c,这个时候在d的pom中,哪一个依赖先写就使用先写依赖的版本
如果d--><d>a</d><d>c</d>-->依赖版本是b1.0
2、a-->b1.0 c-->b1.1 d-->a和c-->b1.0 f-->d,c,如果路径的长短不一致就选择最小路径
f-->b1.1
3、如果希望精确的控制依赖包,可以使用依赖的排除功能进行控制
依赖的范围
1、test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖
2、compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去
3、provided依赖:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突
4、runtime在运行的时候依赖,在编译的时候不依赖
默认的依赖范围是compile
小项目
项目背景:一个用户管理项目,分为三个模块:core模块、log模块、dao模块和service模块。dao模块依懒于core模块和log模块,core模块又依懒于hibernate等,最后dao也会依懒于hibernate等,这就是领情的依懒性。依懒的范围在其中也得到体现。在log模块中用到的log4j是1.2.9,在core模块用到的log4j是1.2.16,所以,在service模块中充分解决了传递的冲突问题。
core模块
user实体类
package com.naxsu.vo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private int id;
private String username;
private String nickname;
private String email;
private String password;
@Id
@GeneratedValue
public int getId() {
return id;
}
// set get method ...
}
Hibernate工具类
package com.naxsu.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
/**
* SessionFactory是单例
*/
private static SessionFactory factory = null;
static {
factory = new Configuration().configure().buildSessionFactory();
}
public static Session openSession() {
return factory.openSession();
}
}
测试类TestUser
package com.naxsu.test;
import org.hibernate.Session;
import org.junit.Assert;
import org.junit.Test;
import com.naxsu.util.HibernateUtil;
import com.naxsu.vo.User;
public class TestUser {
@Test
public void testAdd() {
Session session = HibernateUtil.openSession();
session.beginTransaction();
// User u = new User();
// u.setUsername("admin");
// u.setPassword("admin");
// u.setNickname("超级管理员");
// u.setEmail("admin@admin.com");
// session.save(u);
//
// Assert.assertTrue(u.getId()>0);
session.getTransaction().commit();
}
@Test
public void testLoad() {
Session session = HibernateUtil.openSession();
session.beginTransaction();
User u = (User)session.load(User.class, 1);
Assert.assertEquals(1, u.getId());
session.getTransaction().commit();
}
}
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.naxsu.user</groupId>
<artifactId>user-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>user-core</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>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
</dependencies>
</project>
log模块
Log实体类
package com.naxsu.user.log;
import java.util.Date;
public class Log {
public static void log(String str) {
System.out.println(new Date()+"---"+str);
}
}
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.naxsu.user</groupId>
<artifactId>user-log</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>user-log</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>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
dao模块
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.naxsu.user</groupId>
<artifactId>user-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>user-dao</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>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.naxsu.user</groupId>
<artifactId>user-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
service模块
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.naxsu.user</groupId>
<artifactId>user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>user-service</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>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.naxsu.user</groupId>
<artifactId>user-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.naxsu.user</groupId>
<artifactId>user-log</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
转载请注明出处:[url=http://www.naxsu.com/maven_-yi-lai/]http://www.naxsu.com/maven_-yi-lai/[/url]
更多资源请访问:[url=http://www.naxsu.com]http://www.naxsu.com[/url][/color][/size]
依赖包的查询
1、所有的依赖都是通过坐标来进行存储的(GAV-->groupId、artifactId、version)
2、有一些网上的仓库提供了坐标的查询(http://mvnrepository.com)
3、通过<dependencies>设置依赖
maven是如何搜索依赖的?首先会在本地仓库查询如果本地仓库没有,就去中央仓库查询
依赖的传递性
1、依赖是会被传递
A-->C B-->A ==> B-->C(这种依赖是基于compile这个范围进行传递)
对于依赖的传递而言,主要是针对compile作用域传递
传递的冲突问题
1、a-->b1.0 c-->b1.1 d-->a和c,这个时候在d的pom中,哪一个依赖先写就使用先写依赖的版本
如果d--><d>a</d><d>c</d>-->依赖版本是b1.0
2、a-->b1.0 c-->b1.1 d-->a和c-->b1.0 f-->d,c,如果路径的长短不一致就选择最小路径
f-->b1.1
3、如果希望精确的控制依赖包,可以使用依赖的排除功能进行控制
依赖的范围
1、test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖
2、compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去
3、provided依赖:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突
4、runtime在运行的时候依赖,在编译的时候不依赖
默认的依赖范围是compile
小项目
项目背景:一个用户管理项目,分为三个模块:core模块、log模块、dao模块和service模块。dao模块依懒于core模块和log模块,core模块又依懒于hibernate等,最后dao也会依懒于hibernate等,这就是领情的依懒性。依懒的范围在其中也得到体现。在log模块中用到的log4j是1.2.9,在core模块用到的log4j是1.2.16,所以,在service模块中充分解决了传递的冲突问题。
core模块
user实体类
package com.naxsu.vo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private int id;
private String username;
private String nickname;
private String email;
private String password;
@Id
@GeneratedValue
public int getId() {
return id;
}
// set get method ...
}
Hibernate工具类
package com.naxsu.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
/**
* SessionFactory是单例
*/
private static SessionFactory factory = null;
static {
factory = new Configuration().configure().buildSessionFactory();
}
public static Session openSession() {
return factory.openSession();
}
}
测试类TestUser
package com.naxsu.test;
import org.hibernate.Session;
import org.junit.Assert;
import org.junit.Test;
import com.naxsu.util.HibernateUtil;
import com.naxsu.vo.User;
public class TestUser {
@Test
public void testAdd() {
Session session = HibernateUtil.openSession();
session.beginTransaction();
// User u = new User();
// u.setUsername("admin");
// u.setPassword("admin");
// u.setNickname("超级管理员");
// u.setEmail("admin@admin.com");
// session.save(u);
//
// Assert.assertTrue(u.getId()>0);
session.getTransaction().commit();
}
@Test
public void testLoad() {
Session session = HibernateUtil.openSession();
session.beginTransaction();
User u = (User)session.load(User.class, 1);
Assert.assertEquals(1, u.getId());
session.getTransaction().commit();
}
}
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.naxsu.user</groupId>
<artifactId>user-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>user-core</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>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
</dependencies>
</project>
log模块
Log实体类
package com.naxsu.user.log;
import java.util.Date;
public class Log {
public static void log(String str) {
System.out.println(new Date()+"---"+str);
}
}
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.naxsu.user</groupId>
<artifactId>user-log</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>user-log</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>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
dao模块
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.naxsu.user</groupId>
<artifactId>user-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>user-dao</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>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.naxsu.user</groupId>
<artifactId>user-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
service模块
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.naxsu.user</groupId>
<artifactId>user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>user-service</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>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.naxsu.user</groupId>
<artifactId>user-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.naxsu.user</groupId>
<artifactId>user-log</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
转载请注明出处:[url=http://www.naxsu.com/maven_-yi-lai/]http://www.naxsu.com/maven_-yi-lai/[/url]