Maven创建项目
Maven翻译成中文是『专家、内行』。Maven是Apache组织中一个颇为成功的开源项目,Maven主要服务于基于Java平台的项目构建、依赖管理和项目信息管理的优秀工具
本文将用maven命令行构建项目,外部依赖包以redis为例
一、安装
$ brew install maven
二、创建项目
用模板构建项目
$ mvn archetype:generate
确定groupId、artifactID、package等信息
Define value for property 'groupId': demo
Define value for property 'artifactId': hello
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' demo: : hi
Confirm properties configuration:
groupId: demo
artifactId: hello
version: 1.0-SNAPSHOT
package: hi
自动创建的工程结构
$ tree
.
└── hello
├── pom.xml
└── src
├── main
│ └── java
│ └── hi
│ └── App.java
└── test
└── java
└── hi
└── AppTest.java
三、配置pom.xml
添加自己的依赖项,以及build的配置参数
<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>demo</groupId>
<artifactId>hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello</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>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 添加自己需要的依赖,这里以jedis为例 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<!-- 打包时自动添加依赖包 -->
<build>
<plugins>
<!-- 这个插件与下面那个都可以使用,如果用这个的话直接在package的时候就会把依赖包加进去 -->
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin> -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
四、编写代码
这里以java访问redis为例
App.java
package hi;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import redis.clients.jedis.Jedis;
public class App {
/**
* 字符串
*/
public static void testString(Jedis jedis) {
jedis.set("key", "value");
System.out.println("key: " + jedis.get("key"));
jedis.set("a", "5");
jedis.incr("a");
System.out.println("a: " + jedis.get("a"));
}
/**
* 列表
*/
public static void testList(Jedis jedis) {
jedis.del("my_list");
jedis.lpush("my_list", "Redis");
jedis.lpush("my_list", "Mongodb");
jedis.rpush("my_list", "MySQL");
Long num = jedis.llen("my_list");
List<String> list = jedis.lrange("my_list", 0, num);
System.out.println("String stored in List:");
for (String w : list) {
System.out.print(w+" ");
}
System.out.println();
}
public static void testHash(Jedis jedis){
String key = "user";
jedis.hset(key, "name", "Jerry");
jedis.hset(key, "city", "New York");
Map<String, String> map = jedis.hgetAll(key);
Set<Entry<String, String>> entries = map.entrySet();
for (Entry<String, String> entry : entries){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
public static void testZset(Jedis jedis){
String key = "zset";
jedis.del(key);
jedis.zadd(key, 2, "member");
jedis.zadd(key, 3.5, "menu");
jedis.zadd(key, 1, "apple");
for (String member : jedis.zrange(key, 0, 5)) {
Double socre = jedis.zscore(key, member);
System.out.println(member + " : " + socre);
}
}
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
/**
* 测试
*/
testString(jedis);
System.out.println("-------------------------");
testList(jedis);
System.out.println("-------------------------");
testHash(jedis);
System.out.println("-------------------------");
testZset(jedis);
System.out.println("-------------------------");
jedis.close();
}
}
五、打包运行
$ mvn assembly:assembly # 会把依赖项打包进jar
$ java -cp target/hello-1.0-SNAPSHOT-jar-with-dependencies.jar hi.App # 运行主类
六、运行结果
key: value
a: 6
-------------------------
String stored in List:
Mongodb Redis MySQL
-------------------------
name : Jerry
city : New York
-------------------------
apple : 1.0
member : 2.0
menu : 3.5
-------------------------
七、最终项目结构
八、小结
Maven强大的包管理机制,可以使我们脱离复杂的开发工具,虽然不及IDE方便,但在linux命令行界面下,显然只有Maven才能胜任项目的构建