我正在尝试使用 Dropwizard 和 Gradle 作为构建系统创建一个简单的微服务。没有数据库,只有 REST 端点要公开。
所以我有一个控制器:
@Path("/domainurl/")
@Produces(MediaType.APPLICATION_JSON)
public class SimpleController {
@GET
public Example resourceExample() {
return new Example("something");
}
}
我的应用程序主类:
public class Application extends Application<MyConfiguration> {
@Override
public void run(MyConfiguration configuration, Environment environment) throws Exception {
final SimpleCOntroller controller = new SimpleController();
environment.jersey().register(controller);
}
public static void main(String[] args) throws Exception {
new Application().run(args);
}
}
示例是一个带有一个字符串属性的简单值对象,此时 MyConfiguration 是一个空类。
和 build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.0'
}
}
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
mainClassName = "com.example.Application"
//
dependencies
//
run {
args 'server', './src/config/microservice.yml'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.1'
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
但是在构建之后,当我输入:
java -jar MyApp.jar
我仍然得到:
Error: Could not find or load main class com.example.Application
有任何想法吗?