Skip to content

Commit 54209e4

Browse files
committed
antTask
1 parent c6f980e commit 54209e4

2 files changed

Lines changed: 230 additions & 0 deletions

File tree

build.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<project name ="ANt_TASK" default="run" basedir=".">
3+
<path id="lib_classpath">
4+
<fileset dir="${basedir}/libs">
5+
<include name="*.jar"/>
6+
</fileset>
7+
</path>
8+
<target name="clean">
9+
<delete dir="${basedir}/build"/>
10+
</target>
11+
<target name="compile" depends ="clean">
12+
<mkdir dir ="${basedir}/build/classes"/>
13+
<javac srcdir ="${basedir}/src" destdir ="${basedir}/build/classes">
14+
<classpath refid="lib_classpath"/>
15+
</javac>
16+
<copy todir="${basedir}/build/classes">
17+
<fileset dir="${basedir}/resources">
18+
<include name="**/*.properties"/>
19+
<include name="**/*.xml"/>
20+
</fileset>
21+
</copy>
22+
</target>
23+
<!-- 修改 arg 构建自己的应用 -->
24+
<target name="run" depends ="compile">
25+
<java classname ="cn.jpush.api.ant.AntTask" >
26+
<arg value="de_key"/>
27+
<arg value="dev_secret"/>
28+
<arg value="app_name"/>
29+
<arg value="android_package"/>
30+
<arg value="group_name"/>
31+
<classpath>
32+
<pathelement path="${basedir}/build/classes/"/>
33+
</classpath>
34+
<classpath refid="lib_classpath"/>
35+
36+
</java>
37+
</target>
38+
</project>

src/cn/jpush/api/ant/AntTask.java

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package cn.jpush.api.ant;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import cn.jpush.api.common.NativeHttpClient;
7+
import cn.jpush.api.common.ResponseWrapper;
8+
import cn.jpush.api.examples.PushExample;
9+
10+
import com.google.gson.Gson;
11+
import com.google.gson.JsonObject;
12+
13+
/**
14+
* the class convenient client for use in the ant script.
15+
*
16+
* @author caiyihuang
17+
*
18+
*/
19+
public class AntTask {
20+
private static final String ANT_HOST_NAME = "https://admin.jpush.cn";
21+
private static final String ANT_RECEIVE_PATH = "/v1/app";
22+
private AntTaskEntity entity;
23+
private NativeHttpClient _httpClient = new NativeHttpClient();;
24+
protected static final Logger LOG = LoggerFactory
25+
.getLogger(PushExample.class);
26+
27+
public AntTask(String dev_key, String dev_secret, String app_name,
28+
String android_package, String group_name) {
29+
entity = new AntTaskEntity(dev_key,dev_secret,app_name,android_package,group_name);
30+
31+
}
32+
33+
public void getAntRespone() {
34+
String url = ANT_HOST_NAME + ANT_RECEIVE_PATH;
35+
Gson gson = new Gson();
36+
String json = gson.toJson(entity);
37+
ResponseWrapper respone = _httpClient.sendPost(url, json, "");
38+
String content = respone.responseContent;
39+
/* System.out.println(content);
40+
System.out.println(json);*/
41+
AntResponseEntity responseEntity = gson.fromJson(content,
42+
AntResponseEntity.class);
43+
Error error = responseEntity.getError();
44+
Success success=responseEntity.getSuccess();
45+
if (error != null) {
46+
LOG.error("create failed "+error.getCode() + " " + error.getMessage());
47+
}if(success!=null){
48+
LOG.info("create success " + "your appkey is" +success.getApp_key()+"your android package is"+success.getAndroid_package());
49+
}
50+
51+
}
52+
/**
53+
* entry of ant task
54+
* @param args[0]- args[4] are dev_key,dev_secret,app_name,android_package,group_name
55+
*/
56+
public static void main(String[] args) {
57+
AntTask ant = new AntTask(args[0], args[1], args[2], args[3],args[4]);
58+
ant.getAntRespone();
59+
}
60+
61+
}
62+
63+
class AntResponseEntity {
64+
private Error error;
65+
private Success success;
66+
67+
public Success getSuccess() {
68+
return success;
69+
}
70+
71+
public void setSuccess(Success success) {
72+
this.success = success;
73+
}
74+
75+
public Error getError() {
76+
return error;
77+
}
78+
79+
public void setError(Error error) {
80+
this.error = error;
81+
}
82+
83+
}
84+
85+
class Success {
86+
private String app_key;
87+
private String android_package;
88+
private String is_new_created;
89+
90+
public String getApp_key() {
91+
return app_key;
92+
}
93+
94+
public void setApp_key(String app_key) {
95+
this.app_key = app_key;
96+
}
97+
98+
public String getAndroid_package() {
99+
return android_package;
100+
}
101+
102+
public void setAndroid_package(String android_package) {
103+
this.android_package = android_package;
104+
}
105+
106+
public String getIs_new_created() {
107+
return is_new_created;
108+
}
109+
110+
public void setIs_new_created(String is_new_created) {
111+
this.is_new_created = is_new_created;
112+
}
113+
114+
}
115+
116+
class Error {
117+
private String code;
118+
private String message;
119+
120+
public String getCode() {
121+
return code;
122+
}
123+
124+
public void setCode(String code) {
125+
this.code = code;
126+
}
127+
128+
public String getMessage() {
129+
return message;
130+
}
131+
132+
public void setMessage(String message) {
133+
this.message = message;
134+
}
135+
}
136+
137+
class AntTaskEntity {
138+
private String dev_key;
139+
private String dev_secret;
140+
private String app_name;
141+
private String android_package;
142+
private String group_name;
143+
144+
public AntTaskEntity(String dev_key, String dev_secret, String app_name,
145+
String android_package, String group_name) {
146+
this.dev_key=dev_key;
147+
this.dev_secret=dev_secret;
148+
this.app_name=app_name;
149+
this.android_package=android_package;
150+
this.group_name=group_name;
151+
}
152+
153+
public String getDev_key() {
154+
return dev_key;
155+
}
156+
157+
public String getDev_secret() {
158+
return dev_secret;
159+
}
160+
161+
public String getApp_name() {
162+
return app_name;
163+
}
164+
165+
public String getGroup_name() {
166+
return group_name;
167+
}
168+
169+
public void setDev_key(String dev_key) {
170+
this.dev_key = dev_key;
171+
}
172+
173+
public void setDev_secret(String dev_secret) {
174+
this.dev_secret = dev_secret;
175+
}
176+
177+
public void setApp_name(String app_name) {
178+
this.app_name = app_name;
179+
}
180+
181+
public void setGroup_name(String group_name) {
182+
this.group_name = group_name;
183+
}
184+
185+
public String getAndroid_package() {
186+
return android_package;
187+
}
188+
189+
public void setAndroid_package(String android_package) {
190+
this.android_package = android_package;
191+
}
192+
}

0 commit comments

Comments
 (0)