Provided scope not working in eclipse - gradle

Gradle 2.2.1
I am trying to include dependencies into a jar file that I will ship to other users. I want them to provide their own versions of some dependencies and am trying to emulate the provided scope from maven.
I have followed the tutorial here. I am able to successfully build the project from the command line (while still getting classes not found errors in eclipse) until the eclipse integration part. From the post he says to add eclipse.classpath.plusConfigurations += configurations.provided but I am getting Could not find property 'provided' on configuration container
apply plugin: 'java'
apply plugin: 'eclipse'
// this causes the error
//eclipse.classpath.plusConfigurations += configurations.provided
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': '...',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
configurations {
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
//Jackson
provided 'com.fasterxml.jackson.core:jackson-core:2.6.2'
provided 'com.fasterxml.jackson.core:jackson-annotations:2.6.2'
provided 'com.fasterxml.jackson.core:jackson-databind:2.6.2'
}
test {
systemProperties 'property': 'value'
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}

Fixed it.
eclipse.classpath.plusConfigurations += configurations.provided
Should be an array
eclipse.classpath.plusConfigurations += [configurations.provided]

Related

How do I configure KoTest with Gradle in a LibGdx project? Currently can't build because StringSpec is not recognised

I have a standard LibGdx project setup by the LibGdx tool, only targeting desktop. It uses Gradle (Groovy DSL) to manage dependencies and tasks. I've converted the core module to Kotlin, and I'm trying to add a Kotlin test module using Kotest.
I've followed the Kotest instructions for Gradle on their GitHub but the compile is failing because StringSpec isn't reocgnised (Unresolved reference: StringSpec). I think LibGdx's default Gradle setup may be a little outdated or use older syntax/structure, and perhaps it's conflicting with Kotest's instructions intended for newer versions of Gradle?
For now I've removed any test and am just trying to get it to recognise StringSpec and compile. I've not even reached the stage of getting IntelliJ to recognise and run the tests. Here's what I have so far:
core.tests/tests/com/me/game/AcceptanceTests.kt
package com.jansky.myproject
class AcceptanceTests : StringSpec() {
}
core.tests/gradle.build
version '1.0'
sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "tests/" ]
tasks.withType(Test) {
useJUnitPlatform()
}
eclipse.project.name = appName + "-core.tests"
./build.gradle (ie the root buildfile)
buildscript {
ext.kotlinVersion = '1.3.71'
repositories {
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
google()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
allprojects {
apply plugin: "eclipse"
version = '1.0'
ext {
appName = "game"
gdxVersion = '1.9.10'
roboVMVersion = '2.3.8'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.8.0'
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
google()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "kotlin"
dependencies {
implementation project(":core")
api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.ashley:ashley:1.7.3"
compile group: 'io.github.libktx', name: 'ktx-ashley', version: '1.9.10-b4'
}
}
project(":core") {
apply plugin: "kotlin"
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
compile "com.badlogicgames.ashley:ashley:1.7.3"
compile group: 'io.github.libktx', name: 'ktx-ashley', version: '1.9.10-b4'
}
}
project(":core.tests") {
apply plugin: "kotlin"
test {
useJUnitPlatform()
}
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
compile "com.badlogicgames.ashley:ashley:1.7.3"
compile group: 'io.github.libktx', name: 'ktx-ashley', version: '1.9.10-b4'
implementation 'io.kotest:kotest-runner-junit5:4.0.2'
implementation 'io.kotest:kotest-assertions-core:4.0.2'
}
}
settings.gradle
include 'desktop', 'core', 'core.tests'
Gradle-wrapper.properties
#Sat Apr 04 15:53:20 BST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
I don't have much JVM experience, so I'm at a bit of a loss. Hopefully I've missed something that's obvious to someone that knows Gradle better. Any ideas?

Gradle Kotlin Native - You have not specified any compilation arguments. No output has been produced

I'm pretty new to Gradle and Kotlin Native. I want to setup my build.gradle so that I can use Native to compile my code. I'm using the Konan plugin to do this. It worked fine when I hard-coded my class in the konanArtifacts.program.srcFiles. But obviously if I add more classes, I'll have to add the paths to srcFiles and I don't (and shouldn't) need to. So I looked around to find a way to add any classes I add automatically, and I attempted sourceSets. But when I try to run the compileKonanClientMingw task now my gradle console gives me the following:
error: you have not specified any compilation arguments. No output has been produced.
I'm not entirely sure that I'm using sourceSets correctly, but this seems to be the farthest I got. Below is my build.gradle
build.gradle:
buildscript {
ext.kotlin_version = '1.2.41'
ext.konan_version = '0.6'
repositories {
mavenCentral()
maven {
url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies"
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:$konan_version"
}
}
plugins {
id 'java'
}
group 'net.dev909'
version '1.0'
apply plugin: 'kotlin'
apply plugin: 'konan'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
sourceSets.main.allSource.srcDir '/src/main/kotlin/'
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
konanArtifacts {
program('client-' + version) {
srcFiles sourceSets.main.runtimeClasspath
}
}
Source sets are a part of the Java world and the Java plugin, so the Kotlin/Native plugin avoids using them to be independent on Java. But you still able to compile a bunch of classes, just specify the srcDir parameter:
konanArtifacts {
program('client-' + version) {
srcDir 'src/main/kotlin/'
}
}
Also note that src/main/kotlin/ is a default source path. So if you use only this path, you don't need to specify it explicitly. You may just write:
konanArtifacts {
program('client-' + version)
}

One Gradle Thymeleaf project reloads on resource change, the other relaunches

EDIT: I've narrowed down the behavior a bit, not sure if there's a way to accomplish what I need.
Turns out, my app has the Thymeleaf templates one level down in the source tree:
src/main/resources/tools-server/templates
And I set this in my tools-server.yml file that gets explicitly loaded at application launch. Removing that specification from my configuration, and moving the templates directory up one level to
src/main/resources/templates
Allows spring-boot-devtools to simply reload the template without restarting the app. I think I’ll file a bug with the project, unless there’s a way around it.
I'm still getting the hang of Spring Boot, so bear with me. I've created two projects over the last few months, each starting from different examples found online.
With respect to reloading Thymeleaf templates, the first project does it neatly when they change, issuing two log messages when a template changes, and nothing more. The other does a complete stop and restart of the application, which causes problems because it re-creates the temporary security password, among other things (it also takes longer).
The two gradle.build files are nearly identical, with slightly different dependencies. I'm not sure if those are the differences causing the different behavior.
The working one:
buildscript
{
ext
{
springBootVersion = "1.4.3.RELEASE"
}
repositories
{
mavenCentral()
}
dependencies
{
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath group: "com.layer", name: "gradle-git-repo-plugin", version: "2.0.2"
}
}
apply plugin: "git-repo"
apply plugin: "java"
apply plugin: "maven"
apply plugin: "spring-boot"
jar
{
baseName = "HOA"
version = "0.0.1-SNAPSHOT"
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories
{
mavenCentral()
maven { url "https://maven.atlassian.com/3rdparty/" }
maven { url "https://mvnrepository.com/artifact/" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies
{
compile group: "org.mindrot", name: "jbcrypt", version: "0.4-atlassian-1"
compile group: "org.eclipse.persistence", name: "javax.persistence", version: "2.1.1"
compile group: "org.springframework.data", name: "spring-data-jpa", version: "1.10.4.RELEASE"
compile group: "org.springframework.hateoas", name: "spring-hateoas", version: "0.21.0.RELEASE"
compile group: "com.h2database", name: "h2", version: "1.4.192"
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-groovy-templates")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-web")
// Automated Testing
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.springframework.restdocs:spring-restdocs-mockmvc")
}
dependencyManagement
{
imports
{
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR1"
}
}
compileJava
{
options.compilerArgs << "-Xlint:all" << "-Xdiags:verbose"
}
bootRepackage
{
mainClass = "com.latencyzero.hoa.Application"
}
bootRun
{
addResources = true
}
The messy one:
buildscript
{
ext
{
springBootVersion = '1.4.3.RELEASE'
}
repositories
{
mavenCentral()
}
dependencies
{
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
archivesBaseName = 'toolcrib'
compileJava
{
options.compilerArgs << "-Xlint:all" << "-Xdiags:verbose"
}
jar
{
manifest
{
attributes 'Implementation-Title': 'ToolCrib',
'Implementation-Version': version
}
}
repositories
{
mavenCentral()
}
dependencyManagement
{
imports
{
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR3'
}
}
dependencies
{
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile('org.springframework.boot:spring-boot-devtools')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
runtime('org.postgresql:postgresql')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
springBoot
{
mainClass = "com.latencyzero.toolcrib.services.tools.ToolsServer"
}
bootRun
{
addResources = true
}
Thanks for any insight!
Have you had a look to the documentation
By default changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public or /templates will not trigger a restart but will trigger a live reload.
And
If you want to customize these exclusions you can use the spring.devtools.restart.exclude
There is also spring.devtools.restart.additional-exclude to add more excludes and keep the defaults. In your case, you should add the following to your configuration:
spring.devtools.restart.additional-exclude=classpath:/tools-server/templates/

Adding a 'provided' configuration to a kotlin/java library using gradle

I have a java library to which I've added a 'provided' configuration using gradle.
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'checkstyle'
apply from: file('../gradle/gradle-mvn-push.gradle')
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
repositories {
mavenCentral()
}
group = GROUP
version = VERSION_NAME
configurations {
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
javadoc.classpath += configurations.provided
idea {
module {
scopes.PROVIDED.plus += [configurations.provided]
}
}
checkstyle {
configFile = new File(rootDir, 'checkstyle.xml')
toolVersion = '6.7'
}
dependencies {
provided 'com.google.android:android:2.3.1'
compile 'com.squareup:javawriter:2.5.0'
compile 'com.workday:metajava:1.0'
}
Everything works as expected with this setup.
Now I want to start using kotlin in my library, so I modified the build script to look like this (sans pluses)
+buildscript {
+ repositories {
+ mavenCentral()
+ }
+ dependencies {
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.12.613"
+ }
+}
apply plugin: 'java'
apply plugin: 'idea'
+apply plugin: 'kotlin'
apply plugin: 'checkstyle'
apply from: file('../gradle/gradle-mvn-push.gradle')
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
repositories {
mavenCentral()
}
group = GROUP
version = VERSION_NAME
configurations {
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
javadoc.classpath += configurations.provided
idea {
module {
scopes.PROVIDED.plus += [configurations.provided]
}
}
checkstyle {
configFile = new File(rootDir, 'checkstyle.xml')
toolVersion = '6.7'
}
dependencies {
provided 'com.google.android:android:2.3.1'
compile 'com.squareup:javawriter:2.5.0'
compile 'com.workday:metajava:1.0'
+ compile "org.jetbrains.kotlin:kotlin-stdlib:0.12.613"
}
Without even adding any kotlin files (and even if I do), the library no longer compiles saying that it can't find the classes in the provided library (android in my case).
What do I need to change to get it so that the provided dependencies are recognized again?
That's how I do it in a Gradle plugin:
private void addProvidedConfiguration() {
final Configuration provided =
addConfiguration(project.configurations, "provided")
final Javadoc javadoc =
project.tasks.getByName(JavaPlugin.JAVADOC_TASK_NAME) as Javadoc
javadoc.classpath = javadoc.classpath.plus(provided)
}
private static Configuration addConfiguration(
final ConfigurationContainer configurations, final String name) {
final Configuration compile =
configurations.getByName(JavaPlugin.COMPILE_CONFIGURATION_NAME)
final Configuration configuration = configurations.create(name)
compile.extendsFrom(configuration)
configuration.visible = false
configuration.transitive = false
configuration.allDependencies.all { final dependency ->
configurations.default.exclude(group: dependency.group, module: dependency.name)
}
return configuration
}
You can check the source code on github.
This gradle-propdeps plugin for Gradle adds provided and optional to Gradle, and also handles its integration into IntelliJ IDEA and Eclipse. It is from the Spring team and it has a lot of usage.
You should apply the kotlin plugin after propdeps.
I have not tested this with Android libraries, but it is a much safer bet than trying to roll your own. There are little special cases to handle.
An alternative plugin, also used heavily is the one from Netflix team who has many plugins available for different useful additions to Gradle. This plugin adds the same provided and optional.
This is a good reminder that the old issue Provide a 'provided' configuration for Gradle is still outstanding for 6 years, and feel free to provide your opinion there as to the lack of this feature.

QueryDSL, spring-boot & Gradle

I was hoping to bring querydsl into my spring-boot project via gradle. Despite finding a couple of examples online, none of them actually work for me because of issues with dependencies (I think). According to the QueryDSL support forum, gradle is not supported yet. But I was wondering with all the gradle & spring-boot being created if someone has managed to make it work yet?
Here is my build.gradle:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
}
}
repositories {
mavenCentral()
maven { url: "http://repo.spring.io/libs-snapshot" }
// maven { url: "http://repo.spring.io/milestone" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.0.0.RC5")
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.0.RC5")
compile("org.springframework:spring-orm:4.0.0.RC1")
compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
compile("com.h2database:h2:1.3.172")
compile("joda-time:joda-time:2.3")
compile("org.thymeleaf:thymeleaf-spring4")
compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
compile('org.codehaus.groovy:groovy-all:2.2.1')
compile('org.jadira.usertype:usertype.jodatime:2.0.1')
// this line fails
querydslapt "com.mysema.querydsl:querydsl-apt:3.3.2"
testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
}
testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
testCompile("junit:junit")
}
jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
sourceSets {
main {
generated {
java {
srcDirs = ['src/main/generated']
}
}
java {
srcDirs = []
}
groovy {
srcDirs = ['src/main/groovy', 'src/main/java']
}
resources {
srcDirs = ['src/main/resources']
}
output.resourcesDir = "build/classes/main"
}
test {
java {
srcDirs = []
}
groovy {
srcDirs = ['src/test/groovy', 'src/test/java']
}
resources {
srcDirs = ['src/test/resources']
}
output.resourcesDir = "build/classes/test"
}
}
configurations {
// not really sure what this is, I see it in examples but not in documentation
querydslapt
}
task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
source = sourceSets.main.java
classpath = configurations.compile + configurations.querydslapt
options.compilerArgs = [
"-proc:only",
"-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"
]
destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}
compileJava {
dependsOn generateQueryDSL
source generateQueryDSL.destinationDir
}
compileGeneratedJava {
dependsOn generateQueryDSL
options.warnings = false
classpath += sourceSets.main.runtimeClasspath
}
clean {
delete sourceSets.generated.java.srcDirs
}
idea {
module {
sourceDirs += file('src/main/generated')
}
}
But gradle fails with:
Could not find method querydslapt() for arguments [com.mysema.querydsl:querydsl-apt:3.3.2]
I have tried changing the querydsl-apt version to earlier ones but I get the same error.
Working configuration for Spring Boot 1.3.5 and supported QueryDSL, tested with gradle 2.14.
ext {
queryDslVersion = '3.6.3'
javaGeneratedSources = file("$buildDir/generated-sources/java")
}
compileJava {
doFirst {
javaGeneratedSources.mkdirs()
}
options.compilerArgs += [
'-parameters', '-s', javaGeneratedSources
]
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile "com.mysema.querydsl:querydsl-jpa:$queryDslVersion"
compileOnly "com.mysema.querydsl:querydsl-apt:$queryDslVersion:jpa"
}
Complete project source code: spring-boot-querydsl
You probably need to do at least 2 things:
Declare the "querydslapt" configuration before you use it
Add querydsl-jpa (or whatever flavours you need) to your "compile" configuration.
Then you will have the classpath set up, but the apt bit will not do anything without some more configuration (as you found no doubt from the querydsl support forum). The apt but is used to generate some code that you then need to compile and use in your application code (the "Q*" classes corresponding to your domain objects). You could drive that from a build task in gradle I imagine (it only has to run once for every change in the domain objects).

Resources