跟Maven一样流行的Gradle,构建Java项目实战
Posted 秋9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跟Maven一样流行的Gradle,构建Java项目实战相关的知识,希望对你有一定的参考价值。
目录
1.Gradle是什么?
Gradle是一个基于JVM的构建工具,是一款通用灵活的构建工具,支持maven, Ivy仓库,支持传递性依赖管理,而不需要远程仓库或者是pom.xml和ivy.xml配置文件,基于Groovy,build脚本使用Groovy编写。
1.1Gradle功能
Ø gradle对多工程的构建支持很出色,工程依赖是gradle的第一功能。
Ø gradle支持局部构建。
Ø 支持多方式依赖管理:包括从maven远程仓库、nexus私服、ivy仓库以及本地文件系统的jars或者dirs
Ø gradle是第一个构建集成工具,与ant、maven、ivy有良好的相容相关性。
Ø 轻松迁移:gradle适用于任何结构的工程,你可以在同一个开发平台平行构建原工程和gradle工程。通常要求写相关测试,以保证开发的插件的相似性,这种迁移可以减少破坏性,尽可能的可靠。这也是重构的最佳实践。
Ø gradle的整体设计是以作为一种语言为导向的,而非成为一个严格死板的框架。
Ø 免费开源
1.2gradle提供了什么
1. 一种可切换的,像maven一样的基于约定的构建框架,却又从不锁住你(约定优于配置)
2. 强大的支持多工程的构建
3. 强大的依赖管理(基于Apache Ivy),提供最大的便利去构建你的工程
4. 全力支持已有的Maven或者Ivy仓库基础建设
5. 支持传递性依赖管理,在不需要远程仓库和pom.xml和ivy配置文件的前提下
6. 基于groovy脚本构建,其build脚本使用groovy语言编写
7. 具有广泛的领域模型支持你的构建
2.Gradle与Maven区别
Gradle和Maven都是项目自动构建工具,编译源代码只是整个过程的一个方面,更重要的是,你要把你的软件发布到生产环境中来产生商业价值,所以,你要运行测试,构建分布、分析代码质量、甚至为不同目标环境提供不同版本,然后部署。
虽然两者都是项目工具,但是maven现在已经是行业标准,Gradle是后起之秀,很多人对他的了解都是从android studio中得到的,Gradle抛弃了Maven的基于XML的繁琐配置,众所周知XML的阅读体验比较差,对于机器来说虽然容易识别,但毕竟是由人去维护的。取而代之的是Gradle采用了领域特定语言Groovy的配置,大大简化了构建代码的行数,比如在Maven中你要引入一个依赖:
<properties>
<kaptcha.version>2.3</kaptcha.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.kaptcha</groupId>
<artifactId>kaptcha</artifactId>
<version>${kaptcha.version}</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
然后我将其转换成Gradle脚本,结果是惊人的:
dependencies {
compile('org.springframework:spring-core:2.5.6')
compile('org.springframework:spring-beans:2.5.6')
compile('org.springframework:spring-context:2.5.6')
compile('com.google.code.kaptcha:kaptcha:2.3:jdk15')
testCompile('junit:junit:4.7')
}
注意,配置从原来的28行缩减至7行!
Gradle两大特点:
一是简洁,基于Groovy的紧凑脚本实在让人爱不释手,在表述意图方面也没有什么不清晰的地方二是灵活,各种在Maven中难以下手的事情,在Gradle就是小菜一碟,比如修改现有的构建生命周期,几行配置就完成了。
3.项目实战
3.1.项目介绍
基于Java,使用Gradle构建一个抓取百度地图中的数据客户端,功能如下:
3.2.build.gradle配置
group 'com.example' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'; compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'; compile group: 'org.apache.poi', name: 'poi', version: '3.9'; compile group: 'org.apache.poi', name: 'poi-excelant', version: '3.9'; compile group: 'org.apache.poi', name: 'poi-scratchpad', version: '3.9'; compile group: 'junit', name: 'junit', version: '4.12'; testCompile group: 'junit', name: 'junit', version: '4.12' }
3.3.核心代码
GrabBaiduData.java内容为:
public class GrabBaiduData extends Thread implements ActionListener, WindowListener {
String[] provinces = {"北京", "上海", "天津", "重庆", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "四川", "贵州", "云南", "西藏", "陕西", "甘肃", "宁夏", "青海", "新疆", "香港", "澳门", "台湾"};
String[][] citys = {
{"东城区", "西城区", "崇文区", "宣武区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区", "昌平区", "大兴区", "怀柔区", "平谷区", "密云县", "延庆县"},
{"黄浦区", "卢湾区", "徐汇区", "长宁区", "静安区", "普陀区", "虹口区", "杨浦区", "闵行区", "宝山区", "嘉定区", "浦东新区", "金山区", "松江区", "青浦区", "南汇区", "奉贤区", "崇明县"},
{"和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区", "东丽区", "西青区", "津南区", "北辰区", "武清区", "宝坻区", "宁河县", "静海县", "蓟县"},
{"万州区", "涪陵区", "渝中区", "大渡口区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "北碚区", "万盛区", "双桥区", "渝北区", "巴南区", "黔江区", "长寿区", "綦江县", "潼南县", "铜梁县", "大足县", "荣昌县", "璧山县", "梁平县", "城口县", "丰都县", "垫江县", "武隆县", "忠县", "开县", "云阳县", "奉节县", "巫山县", "巫溪县", "石柱土家族自治县", "秀山土家族苗族自治县", "酉阳土家族苗族自治县", "彭水苗族土家族自治县", "江津市", "合川市", "永川市", "南川市"},
{"石家庄市", "张家口市", "承德市", "秦皇岛市", "唐山市", "廊坊市", "保定市", "衡水市", "沧州市", "邢台市", "邯郸市"},
{"太原市", "朔州市", "大同市", "阳泉市", "长治市", "晋城市", "忻州市", "晋中市", "临汾市", "吕梁市", "运城市"},
{"呼和浩特市", "包头市", "乌海市", "赤峰市", "通辽市", "呼伦贝尔市", "鄂尔多斯市", "乌兰察布市", "巴彦淖尔市", "兴安盟", "锡林郭勒盟", "阿拉善盟"},
{"沈阳市", "朝阳市", "阜新市", "铁岭市", "抚顺市", "本溪市", "辽阳市", "鞍山市", "丹东市", "大连市", "营口市", "盘锦市", "锦州市", "葫芦岛市"},
{"长春市", "白城市", "松原市", "吉林市", "四平市", "辽源市", "通化市", "白山市", "延边州"},
{"哈尔滨市", "齐齐哈尔市", "七台河市", "黑河市", "大庆市", "鹤岗市", "伊春市", "佳木斯市", "双鸭山市", "鸡西市", "牡丹江市", "绥化市", "大兴安岭地区"},
{"南京市", "徐州市", "连云港市", "宿迁市", "淮安市", "盐城市", "扬州市", "泰州市", "南通市", "镇江市", "常州市", "无锡市", "苏州市"},
{"杭州市", "湖州市", "嘉兴市", "舟山市", "宁波市", "绍兴市", "衢州市", "金华市", "台州市", "温州市", "丽水市"},
{"合肥市", "宿州市", "淮北市", "亳州市", "阜阳市", "蚌埠市", "淮南市", "滁州市", "马鞍山市", "芜湖市", "铜陵市", "安庆市", "黄山市", "六安市", "巢湖市", "池州市", "宣城市"},
{"福州市", "南平市", "莆田市", "三明市", "泉州市", "厦门市", "漳州市", "龙岩市", "宁德市"},
{"南昌市", "九江市", "景德镇市", "鹰潭市", "新余市", "萍乡市", "赣州市", "上饶市", "抚州市", "宜春市", "吉安市"},
{"济南市", "青岛市", "聊城市", "德州市", "东营市", "淄博市", "潍坊市", "烟台市", "威海市", "日照市", "临沂市", "枣庄市", "济宁市", "泰安市", "莱芜市", "滨州市", "菏泽市"},
{"郑州市", "开封市", "三门峡市", "洛阳市", "焦作市", "新乡市", "鹤壁市", "安阳市", "濮阳市", "商丘市", "许昌市", "漯河市", "平顶山市", "南阳市", "信阳市", "周口市", "驻马店市", "济源市"},
{"武汉市", "十堰市", "襄樊市", "荆门市", "孝感市", "黄冈市", "鄂州市", "黄石市", "咸宁市", "荆州市", "宜昌市", "随州市", "省直辖县级行政单位", "恩施州"},
{"长沙市", "张家界市", "常德市", "益阳市", "岳阳市", "株洲市", "湘潭市", "衡阳市", "郴州市", "永州市", "邵阳市", "怀化市", "娄底市", "湘西州"},
{"广州市", "深圳市", "清远市", "韶关市", "河源市", "梅州市", "潮州市", "汕头市", "揭阳市", "汕尾市", "惠州市", "东莞市", "珠海市", "中山市", "江门市", "佛山市", "肇庆市", "云浮市", "阳江市", "茂名市", "湛江市"},
{"南宁市", "桂林市", "柳州市", "梧州市", "贵港市", "玉林市", "钦州市", "北海市", "防城港市", "崇左市", "百色市", "河池市", "来宾市", "贺州市"},
{"海口市", "三亚市", "省直辖县级行政单位"},
{"成都市", "广元市", "绵阳市", "德阳市", "南充市", "广安市", "遂宁市", "内江市", "乐山市", "自贡市", "泸州市", "宜宾市", "攀枝花市", "巴中市", "达州市", "资阳市", "眉山市", "雅安市", "阿坝州", "甘孜州", "凉山州"},
{"贵阳市", "六盘水市", "遵义市", "安顺市", "毕节地区", "铜仁地区", "黔东南州", "黔南州", "黔西南州"},
{"昆明市", "曲靖市", "玉溪市", "保山市", "昭通市", "丽江市", "思茅市", "临沧市", "德宏州", "怒江州", "迪庆州", "大理州", "楚雄州", "红河州", "文山州", "西双版纳州"},
{"拉萨市", "那曲地区", "昌都地区", "林芝地区", "山南地区", "日喀则地区", "阿里地区"},
{"西安市", "延安市", "铜川市", "渭南市", "咸阳市", "宝鸡市", "汉中市", "榆林市", "安康市", "商洛市"},
{"兰州市", "嘉峪关市", "白银市", "天水市", "武威市", "酒泉市", "张掖市", "庆阳市", "平凉市", "定西市", "陇南市", "临夏州", "甘南州"},
{"西宁市", "海东地区", "海北州", "海南州", "黄南州", "果洛州", "玉树州", "海西州"},
{"银川市", "石嘴山市", "吴忠市", "固原市", "中卫市"},
{"乌鲁木齐市", "克拉玛依市", "自治区直辖县级行政单位", "喀什地区", "阿克苏地区", "和田地区", "吐鲁番地区", "哈密地区", "克孜勒苏柯州", "博尔塔拉州", "昌吉州", "巴音郭楞州", "伊犁州", "塔城地区", "阿勒泰地区"},
{"香港"},
{"澳门"},
{"台北市", "高雄市", "台中市", "花莲市", "基隆市", "嘉义市", "金门市", "连江市", "苗栗市", "南投市", "澎湖市", "屏东市", "台东市", "台南市", "桃园市", "新竹市", "宜兰市", "云林市", "彰化市"}};
private JTextField textField1;
private JComboBox comboBox1;
private JCheckBox provinceCheckBox;
private JCheckBox infoCheckBox;
private JCheckBox mobileCheckBox;
private JButton exportButton;
private JPanel jpaneltitle;
private JCheckBox searchkeyCheckBox;
private JLabel selectprovince;
private JLabel exportfield;
private JLabel searchcontent;
public void run() {
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
double Width = screensize.getWidth();
double Height = screensize.getHeight();
JFrame frame = new JFrame("百度地图数据抓取工具");
// GrabBaiduData data=new GrabBaiduData();
//设置大小
frame.setSize(500, 300);
//窗体位置
frame.setLocation((int) ((Width - 500) / 2), (int) ((Height - 300) / 2));
frame.setContentPane(jpaneltitle);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
for (String province : provinces) {
comboBox1.addItem(province);
}
exportButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
//System.out.println("//"+actionEvent.getSource());
if (actionEvent.getSource() == exportButton) {
exportButton.setText("导出中……");
exportButton.setEnabled(false);
try {
String keyValue = textField1.getText();
if (keyValue == null || "".equals(keyValue.trim())) {
JOptionPane.showMessageDialog(null, "查询内容不能为空!");
exportButton.setText("导出");
exportButton.setEnabled(true);
return;
}
String privince = (String) comboBox1.getSelectedItem();
Integer privinceIndex = (Integer) comboBox1.getSelectedIndex();
boolean priviceData = provinceCheckBox.isSelected();
boolean infoData = infoCheckBox.isSelected();
boolean mobileData = mobileCheckBox.isSelected();
boolean nameData = searchkeyCheckBox.isSelected();
// System.out.println(new Gson().toJson(citys[privinceIndex]));
// System.out.println(keyValue+"//"+privince+"//"+priviceData+"//"+infoData+"//"+mobileData);
BaiduMapAPI baiduMapAPI = new BaiduMapAPI(priviceData, infoData, mobileData, nameData);
baiduMapAPI.getArea(keyValue, citys[privinceIndex]);
} catch (IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "导出完成!文件目录:" + BaiduMapAPI.saveFile);
exportButton.setText("导出");
exportButton.setEnabled(true);
}
}
@Override
public void windowOpened(WindowEvent windowEvent) {
}
@Override
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
@Override
public void windowClosed(WindowEvent windowEvent) {
System.exit(0);
}
@Override
public void windowIconified(WindowEvent windowEvent) {
}
@Override
public void windowDeiconified(WindowEvent windowEvent) {
}
@Override
public void windowActivated(WindowEvent windowEvent) {
}
@Override
public void windowDeactivated(WindowEvent windowEvent) {
}
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
jpaneltitle = new JPanel();
jpaneltitle.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(5, 6, new Insets(30, 30, 30, 30), -1, -1));
jpaneltitle.setToolTipText("百度地图数据抓取工具");
jpaneltitle.setBorder(BorderFactory.createTitledBorder(""));
final com.intellij.uiDesigner.core.Spacer spacer1 = new com.intellij.uiDesigner.core.Spacer();
jpaneltitle.add(spacer1, new com.intellij.uiDesigner.core.GridConstraints(2, 0, 3, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_VERTICAL, 1, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
searchcontent = new JLabel();
searchcontent.setText("查询内容:");
jpaneltitle.add(searchcontent, new com.intellij.uiDesigner.core.GridConstraints(1, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
comboBox1 = new JComboBox();
final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
comboBox1.setModel(defaultComboBoxModel1);
jpaneltitle.add(comboBox1, new com.intellij.uiDesigner.core.GridConstraints(2, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_NORTHWEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(52, 28), null, 0, false));
provinceCheckBox = new JCheckBox();
provinceCheckBox.setSelected(true);
provinceCheckBox.setText("省市");
jpaneltitle.add(provinceCheckBox, new com.intellij.uiDesigner.core.GridConstraints(3, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
infoCheckBox = new JCheckBox();
infoCheckBox.setSelected(true);
infoCheckBox.setText("详细地址");
jpaneltitle.add(infoCheckBox, new com.intellij.uiDesigner.core.GridConstraints(3, 3, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
mobileCheckBox = new JCheckBox();
mobileCheckBox.setSelected(true);
mobileCheckBox.setText("联系方式");
jpaneltitle.add(mobileCheckBox, new com.intellij.uiDesigner.core.GridConstraints(3, 4, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
exportButton = new JButton();
exportButton.setText("导出");
jpaneltitle.add(exportButton, new com.intellij.uiDesigner.core.GridConstraints(4, 4, 1, 2, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
selectprovince = new JLabel();
selectprovince.setText("选择省份:");
jpaneltitle.add(selectprovince, new com.intellij.uiDesigner.core.GridConstraints(2, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
exportfield = new JLabel();
exportfield.setText("导出字段:");
jpaneltitle.add(exportfield, new com.intellij.uiDesigner.core.GridConstraints(3, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
textField1 = new JTextField();
jpaneltitle.add(textField1, new com.intellij.uiDesigner.core.GridConstraints(1, 2, 1, 4, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
searchkeyCheckBox = new JCheckBox();
searchkeyCheckBox.setSelected(true);
searchkeyCheckBox.setText("查询名称");
jpaneltitle.add(searchkeyCheckBox, new com.intellij.uiDesigner.core.GridConstraints(3, 5, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return jpaneltitle;
}
}
以上是关于跟Maven一样流行的Gradle,构建Java项目实战的主要内容,如果未能解决你的问题,请参考以下文章