6.Eclipse下创建Maven项目

 

Eclipse下创建Maven项目

1. 创建项目

创建一个简单项目:

2. pom.xml

加入相关依赖

加入常量配置

排除相关依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.emp</groupId>
<artifactId>maven_java_helloworld_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven_01</name>

<!-- constant -->
<properties>
<spring.version>5.2.12.RELEASE</spring.version>
</properties>

<!-- denpendency -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
<!-- exclusion -->
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

</project>

3. scope的说明

compile:编译依赖范围,在三个classpath都有效。默认的

test:测试依赖范围,在编译代码和运行代码是无效。

provided:以提供的依赖范围,在编译和测试的时候有效,在运行的时候无效。例如servlet-api,因为容器已经提供,在运行的时候是不需要的。

runtime:运行时依赖范围,仅在测试和运行的时候有效。例如jdbc只有在测试和运行的时候才有效。

system:系统依赖范围,与provided范围一致,但是依赖是通过系统变量来指定依赖,不利于移植。

4. 导入本地jar包

(1) 创建一个普通Java项目Java Project

创建一个User类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.bean;

public class User {

private Integer id;
private String name;
private Integer age;

public User() {
super();
}

public User(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}


public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}

}

导出为MyUser.jar

(2) 创建一个Maven Project

创建一个Book类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.bean;

public class User {

private Integer id;
private String name;
private Integer age;

public User() {
super();
}

public User(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}


public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}

}

导出为MyBook.jar

(3) 在另外的maven项目

在pom.xml中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.emp</groupId>
<artifactId>maven_java_helloworld_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven_01</name>

<!-- denpendency -->
<dependencies>
<dependency>
<groupId>com.my.dependent</groupId>
<artifactId>mybean</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>E:\MyUser.jar</systemPath>

</dependency>

<dependency>
<groupId>com.my.dependent</groupId>
<artifactId>myvo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>E:\MyBook.jar</systemPath>
</dependency>

</dependencies>

</project>

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.test;

import com.bean.User;
import com.vo.Book;

public class Test {

public static void main(String[] args) {
User user = new User();
user.setId(1);
user.setName("Tim");
user.setAge(10);

System.out.println(user.toString());

Book book = new Book();
book.setTitle("bookName");
book.setPrice(30);
book.setPage(200);
book.setCategory("science");

System.out.println(book.toString());

}
}

output:

1
2
User [id=1, name=Tim, age=10]
Book [title=bookName, price=30, page=200, category=science]