How to create a Dropwizard project with gradle? - java

I am on a Linux machine with Idea IntelliJ and I would like to create a Dropwizard project with gradle. With maven archetypes this job would be very easy.
mvn archetype:generate
-DarchetypeGroupId=io.dropwizard.archetypes
-DarchetypeArtifactId=java-simple
-DarchetypeVersion=[REPLACE ME WITH A VALID DROPWIZARD VERSION]
For gradle I am struggling to get going. As I checked out other post I was wondering if what I did was correct:
// Needed Instead of the Shade plug in
plugins {
id 'com.github.johnrengelman.shadow' version '2.0.1'
}
version '1.0-SNAPSHOT'
group 'org.example.sampleName'
description 'Sample Dropwizard REST '
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
compile 'io.dropwizard:dropwizard-core:1.2.0'
testCompile 'junit:junit:4.12'
}
mainClassName = 'org.example.sampleName.SampleApplication'
run {
args = ['server', 'config.yml']
}
shadowJar {
mergeServiceFiles()
exclude 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.SF'
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
Then I just run gradle shadowJar and then run java -jar build/SampleApplication.jar server config.yml. Is this correct? And is the line testCompile 'junit:junit:4.12' enough for the unit test?

You could use this Yeoman generator:
https://www.npmjs.com/package/generator-dropwizard-gradle
If you have npm installed.
The line testCompile 'junit:junit:4.12' is sufficient, yes.
You can also search on github for examples, like this one:
https://github.com/kishaningithub/dropwizard-gradle

Related

How to export gradle project with google api dependencies in eclipse

this is my build.gradle
plugins {
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
id 'java'
id 'application'
}
mainClassName = 'myproject.Main'
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:31.1-jre'
implementation 'com.google.api-client:google-api-client:1.18.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.34.1'
implementation 'com.google.apis:google-api-services-sheets:v4-rev20220927-2.0.0'
}
jar {
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF', 'META-INF/LICENSE', 'META-INF/LICENSE.txt', 'META-INF/DEPENDENCIES', 'META-INF/NOTICE', 'META-INF/NOTICE.txt'
manifest {
attributes 'Main-Class':'myproject.Main'
}
from{
configurations.runtimeClasspath.collect{ it.isDirectory()? it : zipTree(it) }
}
}
after i export my project using eclipse, it got problem when i run it. It seems that it has problem with the dependencies.
i want to export it into runnable jar file that can get data from google spreadsheets

Gradle multi-module. Check context of first module with EventListener from another module

I have two modules, first runs Spring boot Application and second it is EventListener which loads files from resources when context starts. All this modules works well separately but I wanna to include event listener module to my first module (Spring boot module) to get all files from resource of my first module when it runs context.
My main module with setting.gradle:
allprojects {
buildDir = file("${rootDir}/build")
group = 'com.example'
version = "0.1.1"
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
}
setting.gradle
rootProject.name = 'test-application'
include 'bootApplication'
include 'eventListener'
project(":eventListener").projectDir = file("C:/examples/eventListener")
My bootApplication.gradle:
plugins {
id 'org.springframework.boot' version '2.2.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'com.example.bootApplication'
version = "0.1.1"
sourceCompatibility = '11'
targetCompatibility = '11'
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
bootJar {
baseName("bootApplication")
}
jar {
enabled = true
}
dependencies {
compile project(":eventListnere")
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.springfox:springfox-swagger2:+'
implementation 'io.springfox:springfox-swagger-ui:+'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
And my eventListener:
plugins {
id 'org.springframework.boot' version '2.2.1.RELEASE'`enter code here`
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'com.example.eventlistener'
version '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
targetCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
ext {
spring_boot_version = '2.2.1.RELEASE'
}
implementation "org.springframework.boot:spring-boot-starter:$spring_boot_version"
compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
testImplementation "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar.enabled = true
When I run my bootApplication main class it creates a eventlistener-.jar file in root build directory. But eventlistener module doesn't check the resource folder, I guess it doesn't see a bootApplication context. Maybe it should be collect to one jar file? It looks like I missed something in gradle build files.
I will just prefix this by saying I don't know if the stuff below is the actual cause of your problems. But you should probably change a few things related to the jar configuration no matter what.
The Spring Boot Gradle plugin is used to create a fat jar out of the project. By default it disables the normal jar task.
You are re-enabling the normal jar task through jar.enabled = true, which is fine. But you also need to give it another name as one will otherwise override the other. For instance, for your eventListener project, you could do this:
// eventListener/build.gradle
bootJar {
classifier = 'boot'
}
However, if the eventListener is not actually a stand-alone executable, there is no need to create a boot jar from it. So unless you are using the plugin for other things, I would remove it from the eventListener completely:
// eventListener/build.gradle
plugins {
// id 'org.springframework.boot' version '2.2.1.RELEASE'`enter code here` <-- Remove this
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
You can still use the Spring Boot starters in the project, you just don't need the plugin for repackaging the jar.
The same thing applies to your bootApplication project: you are both trying to create a fat executable jar at the same time as a normal jar. One will override the other. In this case, you probably don't need the normal jar, so you should disable the jar task again:
// eventListener/build.gradle
// jar.enabled = true <-- Remove this
Lastly, replace compile project(":eventListnere") with implementation project(":eventListener") and testCompile with testImplementation to avoid some deprecation warnings. The maven plugin is deprecated as well in favor of maven-publish. You can probably also get rid of mavenLocal() unless you are integrating with local Maven projects that you build yourself with mvn install.
The eventListener, if packaged correctly as a normal jar inside the fat jar of the bootApplication, should be able to access resources in both its own resource folder as well as the one from bootApplication when you run the latter.

Running Android unit tests with Spock framework

I am using:
Android Studio 2.1.3
Gradle 2.14.1 (I tried with 2.14 also)
OpenJDK version "1.8.0_91"
I want to write some Unit tests with Groovy and Spock for sample Android application.
I have already read about RoboSpock.
When I am trying to run simple test:
package a.b.regex
class TestSum extends spock.lang.Specification {
def "test adding some numbers"() {
when:
def a = 5 + 4
then:
a == 9
}
}
When I try to run this test in Android Studio I have an error:
Process finished with exit code 1
Class not found: "a.b.regex.TestSum"Empty test suite.
Configurations that I used:
1)
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
apply plugin: 'groovyx.grooid.groovy-android'
// ...
dependencies {
testCompile 'org.robospock:robospock:1.0.0'
}
2)
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
}
}
apply plugin: 'groovyx.android'
dependencies {
testCompile "org.codehaus.groovy:groovy-all:2.4.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile 'org.codehaus.groovy:groovy:2.4.6:grooid'
}
From the console no tests are run at all.
With testing Java applications I have no problem.
Here is the project code where I want to use Spock: GitHub repository
Thankfully to Pieces I found the answer.
You should use the following configuration:
apply plugin: 'groovyx.android'
buildscript {
repositories {
jcenter() // or mavenCentral, etc.
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
}
}
testCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
exclude group: 'junit'
}
1)
This should work great other than you are using an outdated version of the groovy android plugin. The current version is 1.0.0. The error you are seeing is that you included your tests in androidTest source folder, when they should be included in the test source folder.
2)
You do not want groovy-all, and want to exclude that from the spock transitive dependencies as well.
This would look similar to
dependencies {
testCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
exclude group: 'junit'
}
}
Same as the problem with #1 you probably have the source under androidTest folder instead of the test folder.
The androidTest folder is for test that will run on the device, and the test folder if for tests that will run on your machines JVM.
If you reached here trying to configure for gradle 6.6 this will help you:
I stepped multiple times into this while trying to configure Spock in android having gradle 6.6, there has been multiple changes in gradle, so they made this plugin deprecated 'groovyx.android': https://github.com/groovy/groovy-android-gradle-plugin
Deprecated: This plugin has been deprecated in favor of Kotlin which has the full support of JetBrains and Google. The changes that go into each Android Plugin Version make it really hard for this plugin to keep up. As of Gradle 6.0 this plugin does not work.
Found out that gradle have a separate plugin for groovy support:
https://plugins.gradle.org/plugin/org.codehaus.groovy.android
This is the configuration for gradle 6.6 using groovy DSL for gradle
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "gradle.plugin.org.codehaus.groovy:groovy-android-gradle-plugin:3.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
And then in the app configuration:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "org.codehaus.groovy.android"
def groovyVersion = "3.0.5"
def spockVersion = "2.0-M3-groovy-3.0"
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
testImplementation 'junit:junit:4.13'
testImplementation("org.codehaus.groovy:groovy:${groovyVersion}")
testImplementation("org.codehaus.groovy:groovy-all:${groovyVersion}")
testImplementation("org.spockframework:spock-core:${spockVersion}")
testImplementation("org.spockframework:spock-spring:${spockVersion}")
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

Gradle not able to detect osdetector plugin

I am using OpenSSL for my Java GRPC project which uses gradle.
I read the documetation which mentions that we should make security settings as mentioned in this link.
I have included the osdetector plugin in the build.gradle file.
But when I build the project, gradle is not able to resolve the osdetector plugin and throwing error
> Failed to apply plugin [id 'com.google.protobuf']
> Plugin with id 'osdetector' not found.
My gradle file is as follows:
def neo4jVersion = "2.3.3"
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'idea'
apply plugin: 'com.google.osdetector'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.3'
classpath 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
}
}
protobuf {
protoc {
// The version of protoc must match protobuf-java. If you don't depend on
// protobuf-java directly, you will be transitively depending on the
// protobuf-java version that grpc depends on.
artifact = "com.google.protobuf:protoc:3.0.0-beta-2"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:0.12.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
generatedFilesBaseDir = "$projectDir/src/generated"
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'io.grpc:grpc-all:0.12.0'
compile "org.neo4j:neo4j:${neo4jVersion}"
compile "org.neo4j:neo4j-ogm-core:2.0.1"
compile "org.neo4j:neo4j-ogm-http-driver:2.0.1"
compile 'com.google.apis:google-api-services-storage:v1-rev71-1.22.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.3'
compile 'io.netty:netty-tcnative-boringssl-static:1.1.33.Fork14:' + osdetector.classifier
}
The compile dependency alone is getting resolved however.
I think I am missing something basic here. Please let me know the solution.
Updated
protobuf-gradle-plugin is not compatible with osdetector-gradle-plugin-1.4.0, since that version changed the name of the plugin. Swapping to version osdetector 1.2.1 should fix the problem.

How to control H2 driver version in Grails 3 Gradle project?

How to control H2 driver version in Grails/Gradle project?
Having problems with running Grails 3 app with H2 I found this answer: Grails accessing H2 TCP server hangs stating it may be caused by driver version difference.
My IDE reports Grails app uses 1.3.176 version of H2, while my server has 1.4.190. So, I would like to upgrade app's H2, but can't find where it is defined. I sought all project files and found no version definition.
UPDATE
My current build.gradle:
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
classpath "org.grails.plugins:hibernate:4.3.10.5"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
}
version "0.1"
group "multipleparentsgrails"
apply plugin: "spring-boot"
apply plugin: "war"
apply plugin: "asset-pipeline"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
assets {
minifyJs = true
minifyCss = true
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails.plugins:hibernate"
compile "org.grails.plugins:cache"
compile "org.hibernate:hibernate-ehcache"
compile "org.grails.plugins:scaffolding"
runtime "org.grails.plugins:asset-pipeline"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
// Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.)
testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'
console "org.grails:grails-console"
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
You should be able to specify it like any other dependency
runtime "com.h2database:h2:1.4.190"
There are many dependencies which may use H2. grails, hibernate, others.
I would go into your project. Let's say it's $HOME/projects/myproj.
1) Do a dependency report. Pipe it into grep so you don't have to wade through a 1,000 line report, and see what versions of H2 are being used.
cd $HOME/projects/myproj
./gradlew dependencies | grep 'H2'
2) Find the highest version number, and then explicitly include this in your build.gradle to force every dependency to use the most current version:
dependencies {
// all the other dependencies
runtime "com.h2database:h2:1.4.190" // where 1.4.190 is the most
// current version. as i
// type this it is 1.4.191
// according to maven central
}

Categories