我在Java桌面应用程序中使用gradle。
IDE是NetBeans,我想从我的Java类生成一些自动化的JSON。
我实现了3个类(只是POJO),并尝试通过gradle导入“com.github.reinert”,名称:“jjschema”,版本:“1.11”。
jjschema将打印Java对象的JSON等效项。
我的gradle版本是4.4和Java C:\ Program Files \ Java \ jdk1.8.0_171(通过检查路径和发出cmd命令gradle-version进行确认)
C:\ X \ Documents \ NetBeansProjects \ xMessage> gradle-version
Gradle 4.4
构建时间: 2017-12-06 09:05:06 UTC 修订版: cf7821a6f79f8e2a598df21780e3ff7ce8db2b82
Groovy: 2.4.12 Ant: Apache Ant(TM) 版本 1.9.9,编译于 2017 年 2 月 2 日 JVM: 1.8.0_171 (Oracle Corporation 25.171-b11) 操作系统: Windows 10 10.0 amd64
然而,gradle 没有解析类和下载依赖项。
这是我的 gradle 文件:
apply plugin: 'java'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
ext.mainClass = ''
}
repositories {
mavenCentral()
// You may define additional repositories, or even remove "mavenCentral()".
// Read more about repositories here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}
dependencies {
// TODO: Add dependencies here ...
// You can read more about how to add dependency here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
//I also tried compile group: 'com.github.reinert', name: 'jjschema', version: '1.11'
implementation group: 'com.github.reinert', name: 'jjschema', version: '1.11'
}
这是我的POJO,按照MobileDevice的顺序:
package x_mqtt_message;
import java.util.ArrayList;
import java.util.List;
public class MobileDevice {
//most likely a phone number tied to the device if available or other id
private String softwareId;
//device's hardwareId, is import if the user changes the phone or swaps SIM cards, we can still identify the hardware for tracking/diagnostics/debugging
private String hardwareId;
//services running on the device
private List<xService> services = new ArrayList<xService>();
public String getSoftwareId() {
return softwareId;
}
public void setSoftwareId(String softwareId) {
this.softwareId = softwareId;
}
public String getHardwareId() {
return hardwareId;
}
public void setHardwareId(String hardwareId) {
this.hardwareId = hardwareId;
}
public List<xService> getServices() {
return services;
}
public void setServices(List<xService> services) {
this.services = services;
}
类 xSensor:
package x_mqtt_message;
import com.github.reinert.jjschema.Attributes;
@Attributes(title="xSensor", description="An external sensor such as ECG,temp,Accelerometer,Pulse-oximete,Barometric Pressure,etc...")
public class xSensor {
//unique id of the sensor
@Attributes(required=true, description="unique id of the sensor")
private String id;
//type such as ECG,body_temperature,accelerometer,pulse-oximeter,barometric_pressure
@Attributes(required=true, description="type such as ECG,body_temperature,accelerometer,pulse-oximeter,barometric_pressure")
private String type;
@Attributes(minItems=0,uniqueItems=false)
private Object [] data;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Object[] getData() {
return data;
}
public void setData(Object[] data) {
this.data = data;
}
}
类 xService:
package x_mqtt_message;
import com.github.reinert.jjschema.Attributes;
import java.util.HashSet;
@Attributes(title="xService", description="A background process that send & receives data via several protocol")
public class xService {
//service examples: mqtt_service, gps_service, compass_service, bluetooth_service, etc...
@Attributes(required=true, description="mqtt_service, gps_service, compass_service, bluetooth_service, etc...")
private String serviceName;
//is the service 100% up & running yes=true no=false
@Attributes(required=true, description="is the service 100% up & running yes=true no=false")
private Boolean serviceStatus= false;
@Attributes(minItems=0,uniqueItems=true)
private HashSet<xSensor> listOfSensors = new HashSet<xSensor>();
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
}
主要入口:
package x_mqtt_message;
public class x_mqtt_message {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// These packages are NOT being found!!!!
JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory();
schemaFactory.setAutoPutDollarSchema(true);
JsonNode productSchema = schemaFactory.createSchema(MobileDevice.class);
System.out.println(productSchema);
}
}
在IDE中构建时出现以下情况:
执行:gradle clean build 参数:[-c, C:\x\Documents\NetBeansProjects\NgfrMessage\settings.gradle]
:clean C:\ x \ Documents \ NetBeansProjects \ NgfrMessage \ src \ main \ java \ ngfr_mqtt_message \ Ngfr_mqtt_message.java:17:错误:找不到符号 JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory(); ^ 符号:类JsonSchemaFactory 位置:类Ngfr_mqtt_message C:\ x \ Documents \ NetBeansProjects \ NgfrMessage \ src \ main \ java \ ngfr_mqtt_message \ Ngfr_mqtt_message.java:17:错误:找不到符号 JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory(); ^ 符号:类JsonSchemaV4Factory 位置:类Ngfr_mqtt_message C:\ x \ Documents \ NetBeansProjects \ NgfrMessage \ src \ main \ java \ ngfr_mqtt_message \ Ngfr_mqtt_message.java:19:错误:找不到符号 JsonNode productSchema = schemaFactory.createSchema(MobileDevice.class); ^ 符号:类JsonNode 位置:类Ngfr_mqtt_message 3个错误 :compileJava失败
失败:由于异常而构建失败。
出现了什么问题: 任务“:compileJava”的执行失败。
编译失败,请查看编译器错误输出获取详细信息。
尝试: 使用--stacktrace选项运行以获取堆栈跟踪。 使用--info或--debug选项运行以获取更多日志输出。 使用--scan运行以获得完整的见解。
通过cmd> gradle build --debug发出命令时 相关片段:
17:33:42.834 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] 完成构建操作 'Task :compileJava'
17:33:42.834 [null] [org.gradle.internal.progress.DefaultBuildOperationExecutor]
17:33:42.834 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] 构建操作 'Task :compileJava' 已完成
17:33:42.835 [INFO] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] :compileJava (线程[Task worker for ':',5,main]) 已完成。耗时0.267秒。
17:33:42.835 [DEBUG] [org.gradle.internal.work.DefaultWorkerLeaseService] Worker lease root.1.2 已完成(使用中的工作线程数为1)
17:33:42.835 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] 任务工作线程:':‘已释放根锁定1.2上的锁定
17:33:42.835 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] 任务工作线程:':‘已释放锁定:
17:33:42.836 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] 任务工作线程[线程[Task worker for ':' Thread 6,5,main]]已完成,繁忙时间:0.0秒,空闲时间:0.277秒
17:33:42.836 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] 任务工作线程[线程[Task worker for ':' Thread 7,5,main]]已完成,繁忙时间:0.0秒,空闲时间:0.278秒
17:33:42.837 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] 任务工作线程[线程[Daemon worker Thread 10,5,main]]已完成,繁忙时间:0.0秒,空闲时间:0.278秒
17:33:42.836 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] 任务工作线程[线程[Task worker for ':' Thread 5,5,main]]已完成,繁忙时间:0.0秒,空闲时间:0.277秒
17:33:42.837 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] 任务工作线程[线程[Task worker for ':',5,main]]已完成,繁忙时间:0.267秒,空闲时间:0.012秒
17:33:42.837 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] 任务工作线程[线程[Task worker for ':' Thread 2,5,main]]已完成,繁忙时间:0.0秒,空闲时间:0.278秒
17:33:42.836 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] 任务工作线程[线程[Task worker for ':' Thread 3,5,main]]已完成,繁忙时间:0.0秒,空闲时间:0.278秒
17:33:42.836 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] 任务工作线程[线程[Task worker for ':' Thread 4,5,main]]已完成,繁忙时间:0.0秒,空闲时间:0.278秒
17:33:42.839 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] 完成构建操作 '运行任务'
17:33:42.844 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
17:33:42.844 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 失败:构建失败并出现异常。
17:33:42.845 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
17:33:42.845 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * 出了什么问题:
17:33:42.845 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 执行任务':compileJava'失败。
17:33:42.845 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] >
谢谢。