$ sudo apt intsall gradle
$ gradle init --type java-library
まぁ、コマンドラインじゃなくても IDE (Eclipse/Netbeans/IntelliJ) に任せればよかろう
$ tree
.
└── JettyExam
├── LICENSE
├── README.md
├── bin
│ ├── main/ ← コンパイル結果
│ └── test/ ← コンパイル結果
├── build / ← ビルド結果 (プロジェクト全体を zip 圧縮したものや、製品の jar など)
├── build.gradle ← ★これが gradle スクリプト
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew ← ★これが、このプロジェクト向けの設定が入った gralde 実行スクリプト。プロジェクト作成時に自動生成される
├── gradlew.bat ← ★その Windows用
├── settings.gradle
└── src
├── main
│ ├── java/ ← ソースコード
│ └── resources/ ← 設定ファイル類
└── test
├── java/ ← テストコード
└── resources / ← テストデータ類
$ gradlew build
$ gradlew test
$ gradlew javadoc
buildscript {
ext.buildConfig = [
'compileSdk': 11,
'targetSdk': 11,
'main': 'com.example.jetty.Main'
]
ext.versions = [
'jetty': '9.4.25.v20191220',
'jersey': '2.30',
'derby': '10.15.1.3',
'derby8': '10.14.2.0'
]
}
plugins {
// check library versions
// $ ./gradlew dependencyUpdates
id "com.github.ben-manes.versions" version "0.27.0"
id "com.github.johnrengelman.shadow" version "4.0.4"
id 'java'
}
jar {
manifest {
attributes "Main-Class" : "${buildConfig.main}"
}
}
group 'com.example'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'jacoco'
// To avoid "java.lang.NoSuchFieldException: $jacocoAccess"
jacoco {
toolVersion = "0.8.4"
}
apply plugin: 'application'
//apply plugin: 'war' //
sourceCompatibility = "${buildConfig.compileSdk}" // -source
targetCompatibility = "${buildConfig.targetSdk}" // -target
mainClassName = "${buildConfig.main}"
compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-Xlint:unchecked'
}
test {
testLogging {
events 'standard_out', 'standard_error'
}
}
repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
// https://mvnrepository.com/artifact/commons-cli/commons-cli
compile 'commons-cli:commons-cli:1.4'
// The Jetty Web Server.
compile "org.eclipse.jetty:jetty-servlet:${versions.jetty}"
compile "org.eclipse.jetty:jetty-webapp:${versions.jetty}"
// JAX-RS (REST API), CDI
compile "org.glassfish.jersey.core:jersey-server:${versions.jersey}"
compile "org.glassfish.jersey.containers:jersey-container-servlet-core:${versions.jersey}"
compile ("org.glassfish.jersey.containers:jersey-container-jetty-http:${versions.jersey}") {
exclude group : 'org.eclipse.jetty', module : 'jetty-util'
}
compile "org.glassfish.jersey.media:jersey-media-json-jackson:${versions.jersey}"
compile "org.glassfish.jersey.inject:jersey-hk2:${versions.jersey}"
// logs
compile 'ch.qos.logback:logback-classic:1.2.3'
// JPA (Database)
compile 'org.eclipse.persistence:eclipselink:2.7.6'
if (buildConfig.targetSdk < 9) {
println "Target SDK is ${buildConfig.targetSdk} => Use Derby ${versions.derby8}"
compile "org.apache.derby:derby:${versions.derby8}"
compile "org.apache.derby:derbytools:${versions.derby8}"
} else {
println "Target SDK is ${buildConfig.targetSdk} => User Derby ${versions.derby}"
compile "org.apache.derby:derby:${versions.derby}"
compile "org.apache.derby:derbytools:${versions.derby}"
compile "org.apache.derby:derbyshared:${versions.derby}"
}
// Lombok (Getter/Setter)
compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
// Lookup classes by it's annotation
compile 'io.github.classgraph:classgraph:4.8.61'
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
// JEP 320: Remove the Java EE and CORBA Modules (from JavaSE11)
// http://openjdk.java.net/jeps/320
// -> Use RI libraries from Maven Central
// java.xml.ws RI
compile 'com.sun.xml.ws:jaxws-ri:2.3.2'
// java.xml.bind RI
compile 'javax.xml.bind:jaxb-api:2.3.1'
compile 'com.sun.xml.bind:jaxb-core:2.3.0.1'
compile 'com.sun.xml.bind:jaxb-impl:2.3.0.1'
// java.activation RI
compile 'com.sun.activation:javax.activation:1.2.0'
// java.xml.ws.annotation RI
compile 'javax.annotation:javax.annotation-api:1.3.2'
// java.transaction RI
compile 'javax.transaction:javax.transaction-api:1.3'
// to fix Javadoc error : "class file for javax.interceptor.InterceptorBinding not found"
// related to JPA transction-api
compileOnly 'javax.interceptor:javax.interceptor-api:1.+'
// Test
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
testCompile group: 'junit', name: 'junit', version: '4.13'
testCompile 'org.jmockit:jmockit:1.12'
}
buildscript { ext.buildConfig = [ 'compileSdk': 11, 'targetSdk': 11 ] ext.versions = [ 'jetty': '9.4.25.v20191220', 'jersey': '2.30', 'derby': '10.15.1.3' ] }
とすると、${buildConfig.compileSdk} や ${versions.jettty} で、値を参照できる
plugins { // check library versions // $ ./gradlew dependencyUpdates id "com.github.ben-manes.versions" version "0.27.0" }
というように build.gradle を書いておき、コマンドラインから
$ ./gradlew dependencyUpdates
を実行すると最新のライブラリが使われているか調べることができる
plugins { id "com.github.johnrengelman.shadow" version "4.0.4" }
で全部盛りJarが作られる
jar { manifest { attributes "Main-Class" : "com.example.jetty.Main" } }
で Jar の MANIFEST に属性を書き込むことができる
mainClassName = 'com.example.jetty.Main'
sourceCompatibility = "${buildConfig.compileSdk}" // -source targetCompatibility = "${buildConfig.targetSdk}" // -target' mainClassName = "${buildConfig.main}" compileJava { options.encoding = 'UTF-8' options.compilerArgs << '-Xlint:unchecked' }
dependencies { // https://mvnrepository.com/artifact/commons-cli/commons-cli compile 'commons-cli:commons-cli:1.4' ... }
compile 'commons-cli:commons-cli:1.4'は、
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'の省略形
compile ("org.glassfish.jersey.containers:jersey-container-jetty-http:${versions.jersey}") { exclude group : 'org.eclipse.jetty', module : 'jetty-util' }
test { testLogging { events 'standard_out', 'standard_error' } }
repositories { google() mavenCentral() jcenter() }
println "Target SDK is ${buildConfig.targetSdk} => Use Derby ${versions.derby8}"
if (buildConfig.targetSdk < 9) { println "Target SDK is ${buildConfig.targetSdk} => Use Derby ${versions.derby8}" compile "org.apache.derby:derby:${versions.derby8}" compile "org.apache.derby:derbytools:${versions.derby8}" } else { println "Target SDK is ${buildConfig.targetSdk} => User Derby ${versions.derby}" compile "org.apache.derby:derby:${versions.derby}" compile "org.apache.derby:derbytools:${versions.derby}" compile "org.apache.derby:derbyshared:${versions.derby}" }
上記のように条件分岐がいるのは、Apache Derby が、Java 8 用と Java 9 or higher 用で異なるため。ライブラリパッケージの構成も異なる。