Maven相对传统导入第三方jar包的方式,有着巨大的优势。但Maven也存在一些问题,比如依赖过多的时候,会导致pom.xml文件过长,阅读起来十分不友好。另外Maven插件的开发也过也繁琐。Gradle的出现,解决了这些问题。
下载Gradle
下载完后,解压即可
配置环境变量
测试是否环境配置成功,出现下面内容即配置成功!
配置Gradle,在init.d文件下新建init.gradle文件,并在该文件里配置如下信息
allprojects{
repositories{
maven { url 'file:e:\\apache-maven-3.8.4\\repository'}
mavenLocal()
maven{ name "Alibaba" ; url "https://maven.aliyun.com/repository/public"}
mavenCentral()
}
}
现在在IDEA中配置Gradle
点击Apply,然后OK即可。即在IDEA配置成功。
现在我们使用IDEA来创建Gradle工程,这以Java操作Excel为列。
Person类,代码如下
public class Person {
private String name;//名称
private String city;//城市
private String cellPhone;//手机号码
public Person(){
}
public Person(String name, String city, String cellPhone) {
this.name = name;
this.city = city;
this.cellPhone = cellPhone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", city='" + city + '\'' +
", cellPhone='" + cellPhone + '\'' +
'}';
}
}
ExcelWriter类,代码如下
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelWriter {
public static void main(String[] args) throws IOException {
List<Person> list = new ArrayList<>();
list.add(new Person("张三","成都","12345678910"));
list.add(new Person("李四","北京","6666666666"));
list.add(new Person("王麻子","上海","8888888888"));
ExcelWriter excelWriter = new ExcelWriter();
excelWriter.fillExcel(list,new File("persion.xlsx"));
}
/**
* 填充Excel
* @param data 用户数据
* @param destFile 目标文件
*/
public void fillExcel(List<Person> data, File destFile) throws IOException {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
XSSFSheet sheet = xssfWorkbook.createSheet("用户");
XSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("名称");
row.createCell(1).setCellValue("城市");
row.createCell(2).setCellValue("手机号码");
for(int i=0;i<data.size();i++){
Person person = data.get(i);
System.out.println(person);
XSSFRow r = sheet.createRow(i+1);
r.createCell(0).setCellValue(person.getName());
r.createCell(1).setCellValue(person.getCity());
r.createCell(2).setCellValue(person.getCellPhone());
}
FileOutputStream fileOutputStream = new FileOutputStream(destFile);
xssfWorkbook.write(fileOutputStream);
fileOutputStream.close();
xssfWorkbook.close();
}
}
Gradle添加依赖与Maven方式不同
testCompile是7.0之前的写法,gradle版本不是我们自己配置的。我们现在进行配置。
运行项目,效果如下(乱码)
接下来我们使用gradle来把工程打包成jar包。
首先,添加application插件
然后增加一个jar包选项
jar {
//当有重复依赖,排除重复依赖
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
//指定清单文件编码
manifestContentCharset 'utf-8'
//清单文件内容
manifest{
attributes 'Main-Class': 'www.excel.test.ExcelWriter' //运行主类
}
//指定哪些依赖包要被打包到jar包里面
from {
configurations.runtimeClasspath.collect{
it.isDirectory() ? it : zipTree(it)
}
}
}