Bad gradle code from LibGdx book? - java

I am using Learning LibGDX Game Development and I am on trying to follow the Gradle way. In chapter 4 at the bottom of page 144 it says add:
compile "com.badlogic.gdx:gdx-tools:$gdxVersion"
underneath the section with this code:
project(":desktop")
The build.gradle that I inherited from the book's website is posted below. I added the code in the section with "-desktop" but I got an error from Eclipse.
Here is my error:
Could not find method compile() for arguments [com.badlogic.gdx:gdx-
tools:1.6.4] on org.gradle.plugins.ide.eclipse.model.EclipseProject_Decorated#54d80175.
I am brand spanking new to Gradle, does anybody know what I should do if I want to add the gdx-tools.jar via Gradle?
Build.gradle for desktop project:
apply plugin: "java"
sourceCompatibility = 1.6
sourceSets.main.java.srcDirs = [ "src/" ]
project.ext.mainClassName = "com.packetpub.libgdx.canyonbunny.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../android/assets");
task run(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
}
task dist(type: Jar) {
from files(sourceSets.main.output.classesDir)
from files(sourceSets.main.output.resourcesDir)
from {configurations.compile.collect {zipTree(it)}}
from files(project.assetsDir);
manifest {
attributes 'Main-Class': project.mainClassName
}
}
dist.dependsOn classes
eclipse {
project {
name = appName + "-desktop"
linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/android/assets'
compile "com.badlogic.gdx:gdx-tools:$gdxVersion"
}
}
task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
doLast {
def classpath = new XmlParser().parse(file(".classpath"))
new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]);
def writer = new FileWriter(file(".classpath"))
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.setPreserveWhitespace(true)
printer.print(classpath)
}
}

You are using the old domain, which is wrong:
compile "com.badlogic.gdx:gdx-tools:$gdxVersion"
Please, replace it for this new:
compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
Ensure you put in the correct place:
Tools Gradle
Core Dependency: Dont put me in core!
Desktop Dependency: compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
Android Dependency: Not compatible!
iOS Dependency: Not compatible!
HTML Dependency: Not compatible!

It should be under dependencies branch. But there is no gdxVersion defined in this gradle file also.
I assume you can use a workaround as defining your variable and using under dependencies by adding this code to the end of the gradle file you shared.
dependencies {
def gdxVersion = '1.6.4'
compile "com.badlogic.gdx:gdx-tools:$gdxVersion"
//you can add another libraries as well
}
But I recommend you to manage libGDX dependencies in the project's gradle file for the consistency of module's libGDX versions. By default, libGDX setup creates build.gradle file in the project's root directory. And gdxVersion variable and libGDX dependencies are defined and managed in this file. Like as:
//defines common variables and settings that all projects (modules like desktop, ios, android) use
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = 'YOUR_PROJECT_NAME'
gdxVersion = '1.7.0'
roboVMVersion = '1.8.0'
gwtVersion = '2.6.0'
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
//and here desktop module specific settings
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
}
}
With this usage, you can see that all module's libGDX dependencies can be handled by the same way in the desktop module's depencendies. And you can update your libGDX version with one change to the gdxVersion above.

Related

Gradle dependency downloads but packages aren't available in project

I published a small library of mine to a free maven hosting service, and am using that package in another project. I've done this before without issue but something isn't working this time.
Gradle finds the file just fine, and downloads it I assume. I know this because any change to the URL, package name, or version, and Gradle throws the "can't find the dep in any of these places" error.
However, any import of the packages in this JAR error, saying it can't find the package. IntelliJ, when I refresh gradle deps, also doesn't show my library in the "External Libraries" section.
Here's my gradle.build for the project I'm using the library in:
apply plugin: 'java'
repositories {
maven { url = 'https://repo.repsy.io/mvn/viveleroi/tileowner' }
}
group = project.property("group")
version = project.property("version")
dependencies {
compileOnly 'network.darkhelmet.tileowner:tileowner:1.0.0'
}
compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
options.forkOptions.executable = 'javac'
}
The test class:
import network.darkhelmet.tileowner.TileOwner;
public class Demo {}
Gradle says it's on the classpath:
compileClasspath - Compile classpath for source set 'main'.
\--- network.darkhelmet.tileowner:tileowner:1.0.0
compileOnly - Compile only dependencies for source set 'main'. (n)
\--- network.darkhelmet.tileowner:tileowner:1.0.0 (n)
I've downloaded the published jar of my library from that repository, unzipped it, and have confirmed all files are in there as expected.
Here's the build.gradle of my library project:
import org.apache.tools.ant.filters.ReplaceTokens
buildscript {
dependencies {
classpath group: 'com.github.rodionmoiseev.gradle.plugins', name: 'idea-utils', version: '0.2'
}
}
plugins {
id "com.github.johnrengelman.shadow" version "7.0.0"
id "xyz.jpenilla.run-paper" version "1.0.6"
id 'maven-publish'
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'idea-utils'
apply plugin: 'checkstyle'
processResources {
filter ReplaceTokens, tokens: [
"apiversion": project.property("apiversion"),
"version" : project.property("version")
]
}
repositories {
mavenLocal()
mavenCentral()
maven { url = "https://repo.aikar.co/content/groups/aikar/" }
maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
}
group = project.property("group")
version = project.property("version")
targetCompatibility = sourceCompatibility = JavaVersion.VERSION_17
dependencies {
compileOnly 'org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT'
implementation 'co.aikar:acf-bukkit:0.5.0-SNAPSHOT'
}
compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
options.forkOptions.executable = 'javac'
}
jar {
actions = []
dependsOn = []
dependsOn('shadowJar')
}
shadowJar {
relocate 'co.aikar.commands', 'network.darkhelmet.tileowner.acf'
relocate 'co.aikar.locales', 'network.darkhelmet.tileowner.locales'
}
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
repositories {
maven {
name = 'repsy'
url = 'https://repo.repsy.io/mvn/viveleroi/tileowner'
credentials(PasswordCredentials)
}
}
}

How to make Gradle put Java dependecies in different destination directories?

I have a project that generates a client and a server jar file. The client uses an old version of WorldWind (1.5.1) in a set of "do not touch it code", meaning I can't upgrade to a newer WorldWind. This WorldWind, in turn, depends on old versions of jogl (1.1.1a), gluegen (1.0b06), etc... The client and server both use jackson (2.6.3 is latest we have in our Artifactory) to pass JSON messages to each other, with the server using some additional jackson libraries to support another 3rd party library it needs. There is some sort of conflict between the old jogl and the jackson that makes it so that if all these libraries are together in one classpath (lib folder) they stomp on each other and running the client at startup it claims it cannot find jogl.
I had solved this in the past by generating a lib_worldwind folder that would contain only the worldwind jar, a lib_jackson folder that would contain only the server-specific jackson jars, and a lib folder that would contain everything else. I did this via ant. Then I'd tell the client its classpath was lib and lib_worldwind, and I'd tell the server it is lib and lib_jackson.
We recently have converted everything to Gradle, which in the long run will let me actually try and update some of my dependencies to never versions easier and then see what breaks (and I can then fix piecemeal). Nonetheless, it seems Gradle puts everything into one lib folder.
So my question is, how do I replicate the old behavior where stuff ends up in 3 different folders (while makeing sure Gradle also tells Eclipse where it ended up putting the stuff correctly so it can build and run)? I've been trying for a week now (I thought maybe Configurations gets me there but it either does not or I'm not doing it right).
My Gradle scripts:
root level build.gradle:
def standalone_distribution = new File(rootProject.projectDir, "standalone-distribution")
def jars_dir = new File(rootProject.projectDir, "jars")
def lib_dir = new File(rootProject.projectDir, "jars/lib")
def lib_client = new File(rootProject.projectDir, "jars/lib_worldwind")
def lib_server = new File(rootProject.projectDir, "jars/lib_jackson")
ant.importBuild 'build.xml'
buildscript {
repositories {
maven { url 'http://mylocalartifactory'}
}
dependencies {
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6'
}
}
//irrelevant sonarqube settings
allprojects {
version = 'x'
}
subprojects {
repositories {
maven { url 'http://mylocalartifactory'}
flatDir { //I have to do this since I don't have some of these old libraries in my local artifactory but instead are sitting in a lib folder of specific sub projects
dirs "${projectDir}/lib"
}
}
apply plugin: 'maven'
apply plugin: 'java'
apply plugin: 'eclipse'
sourceSets {
main.java.srcDirs = ['src']
main.resources.srcDirs = ['conf']
test.java.srcDirs = ['test']
test.resources.srcDirs = ['testConf']
}
compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << "-Xilint:all"
}
javadoc.options.encoding = 'UTF-8'
configurations.all {
exclude group: 'javax.media', module: 'jai-core'
exclude group: 'com.sun.media', module: 'jai_imageio'
exclude group: 'com.sun.jdmk', module: 'jmxtools'
exclude group: 'com.sun.jmx', module: 'jmxri'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
jar { archiveName = project.name + ".jar" }
group = 'client'
group = 'server'
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.compilerArgs << '-Xlint:-deprecation'
options.compilerArgs << '-Xlint:-unchecked'
options.compilerArgs << '-Xlint:-rawtypes'
options.compilerArgs << '-Xlint:-varargs'
options.compilerArgs << '-Xling:-dep-ann'
}
//unit test stuff
task copyJar (type: Copy) {
from jar
into "$jars_dir"
}
task copyDeps (type: Copy) {
from configurations.runtime
into "$lib_dir"
}
task copyJarsandDeps (type: Copy, dependsOn: [copyJar, copyDeps])
}
The ant script build.xml sets up folder structure for deployment to a folder that would house this, in other words
standalone-distribution
- the primary project.name.jar jars that are my actual project + the scripts that start them
- lib
- lib_worldwind
- lib_jackson
- other folders like Resources, Docs, etc..
Then individual projects have gradle that looks like:
client build.gradle:
jar {
from('src') {
include 'resourcefolder'
}
manifest {
attributes 'Implementation-Title': 'Client', 'Implementation-Version': version
}
}
dependencies {
compile project (':some-common-code-project')
compile name: 'jogl-1.1.1a'
compile name: 'worldwind-1.5.1'
//a handful more like this name: because they are old and I don't have them in localartifactory
compile 'org.apache.commons:commons-configurations:1.10'
//a handful more that I do have
}
Then, similarly in server build.gradle:
jar {
manifest {
attributes 'Implementation-Title': 'Server', 'Implementation-Version': version
}
}
dependencies {
compile project (':some-common-code-project')
compile 'thirdpartylibrary' //not used in client, thirdpartylibrary depends on jackson-core, jackson-annotations, and jackso-databind 2.8.7 which are not compatible with WorldWind so I want them in a different directory
//a handful more that I do have in local library
}
Any idea on how to do this "split"?
edit: just inspected that old worldwind-1.5.1.jar and it contains a set of jackson packages in there! So that's probably part of the problem.
edit2: got a (previously uninvolved) coworker to look at this and he suggested trying to exclude jackson from the client build.gradle and that solved it!

Can't compile Java class when calling Clojure code

Adding clojure to an already existing gradle java project
I have a Java project that I want to start trying to add some Clojure to, but I'm hitting a few issues. I'm using the IntelliJ IDEA with the Cursive for Clojure IntelliJ plugin.
I am also using the gradle-clojure plugin for Clojure. My Java classes recognise and can call my Clojure code, but my Java code will no longer compile because at compile time it can no longer see my Clojure code.
Do I need to add an extra step in my build.gradle? Do I need to compile my Clojure separate and manually before trying to compile Java?
Any help would be greatly appreciated.
Environment
gradle-clojure v0.3.1 gradle v4.4.1Java v1.8 Intellij IDE on MacOS High Sierra
Stacktrace <> Task :compileJava FAILED java:8: error: cannot find symbol import com.example.clojure;
You can use Netflix's Clojure Wrapper, nebula.clojure.
Here is an example of a Gradle Build script for a project that uses Java and Clojure:
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "com.netflix.nebula:nebula-clojure-plugin:4.4.0"
}
}
plugins {
id "nebula.clojure" version "4.4.0"
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.demo'
def artifactName = 'demo-service'
version = latestRepoTag()
static def latestRepoTag() {
def cmd = "git describe --abbrev=0"
def proc = cmd.execute();
return proc.text.trim();
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext['spring-restdocs.version'] = '1.2.1.RELEASE'
ext {
springCloudVersion = 'Dalston.SR4'
}
compileJava {
classpath = project.files(
project.compileClojure.outputs,
classpath
)
}
compileClojure {
jvmOptions {
jvmArgs '-Djava.awt.headless=true'
}
}
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
jar {
baseName = "${artifactName}"
version = latestRepoTag()
}
clojure.aotCompile = true
configurations{
dev
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Dalston.SR3'
}
}
dependencies {
compile 'org.clojure:clojure:1.6.0'
compile('com.google.guava:guava:19.0')
compile("commons-io:commons-io:2.5")
compile "org.apache.pdfbox:pdfbox:2.0.0-RC3"
compile("org.apache.commons:commons-lang3:3.0")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.cloud:spring-cloud-starter-eureka")
compileOnly("org.projectlombok:lombok:1.16.6")
runtime('com.h2database:h2:1.4.190')
runtime("com.ingres.jdbc:iijdbc:10.0-4.0.5")
runtime('org.apache.commons:commons-dbcp2:2.1.1')
runtime('org.postgresql:postgresql:9.4.1209')
dev("org.springframework.boot:spring-boot-devtools")
testCompile('com.jayway.jsonpath:json-path')
testCompile('com.jayway.jsonpath:json-path-assert:2.2.0')
testCompile('com.google.code.gson:gson:2.8.1')
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.springframework.restdocs:spring-restdocs-mockmvc:1.2.1.RELEASE")
}
task wrapper(type: Wrapper) {
gradleVersion = '4.1'
}
bootRun {
classpath = sourceSets.main.runtimeClasspath + configurations.dev
systemProperties = System.properties
jvmArgs = ["-client", "-Dsun.net.inetaddr.ttl=60", "-Djava.security.egd=file:/dev/./urandom"]
environment = [
'spring_profiles_active': 'beta,fast_discovery'
]
}
Here is a link to the plugin on Gradle's plugin documentation site:

Gradle task to compile Java and Groovy sources

We are starting a new Project with gradle (all of my previous projects are on Maven) and this is my first experience on using gradle, below is my build.gradle file and am trying to compile the java and groovy sources using the task compile
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
springVersion = '4.3.7.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
task compile(type: GroovyCompile) {
//source = fileTree(dir: 'src', include: '**/*.java')
sourceSets {
main {
java { srcDirs = [] } // no source dirs for the java compiler
groovy { srcDir "src" } // compile everything in src/ with groovy
}
}
destinationDir = file('build/classes/main')
classpath = files('build/classes/main')
}
dependencies {
compile "org.codehaus.groovy:groovy-all:2.4.10"
compile('org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}')
compile('org.springframework.boot:spring-boot-actuator-docs:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-groovy-templates:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-jersey:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-security:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-web:${springBootVersion}')
compile('org.springframework:spring-webmvc:${springVersion}')
compile "com.microsoft:sqljdbc4:4.0"
testCompile('org.springframework.boot:spring-boot-starter-test:${springBootVersion}')
}
And when I run the gradle compile command am seeing :compile NO-SOURCE and no compiled classes in build\classes\main
can someone please help me with gradle task to compile both java and groovy sources?
The Gradle docs of the Groovy plugin describe the default layout like follows. If it is an option to stick to that, there is no need for a custom compile task.
src/main/java Production Java source
src/main/resources Production resources
src/main/groovy Production Groovy sources. May also contain Java sources for joint compilation.
src/test/java Test Java source
src/test/resources Test resources
src/test/groovy Test Groovy sources. May also contain Java sources for joint compilation.
```

Gdx pay desktop no class defined error

I am using libgdx pay for Android and iOS, and was until recently able to test my application in desktop mode.
Since upgrading to IntelliJ 2016.1, I now receive the following error only when trying to run the desktop application; both iOS and Android continue to compile and run successfully.
The error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NoClassDefFoundError: com/badlogic/gdx/pay/PurchaseObserver
My build.gradle file for the desktop and core modules looks as below;
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = '####'
gdxVersion = '1.9.2'
roboVMVersion = '1.8.0'
box2DLightsVersion = '1.4'
ashleyVersion = '1.4.0'
aiVersion = '1.5.0'
gdxpayVersion = '0.6.0'
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "de.tomgrill.gdxtwitter:gdx-twitter-core:0.1.0-SNAPSHOT"
compile "com.badlogicgames.gdxpay:gdx-pay:$gdxpayVersion"
compile "com.badlogicgames.gdxpay:gdx-pay-client:$gdxpayVersion"
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
compile "de.tomgrill.gdxtwitter:gdx-twitter-desktop:0.1.0-SNAPSHOT"
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
}
}
Furthermore, within IntelliJ it reports no error in being able to import the PurchaseObserver, it only occurs when I try to run the application.
Any help would be appreciated.
This is a known bug of IntelliJ itself, which is summarised here http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=21148&hilit=intellij
The bug IntelliJ issue can be found here
https://youtrack.jetbrains.com/issue/IDEA-147788
The bug only occurs when there is also an Android run configuration.
Until it is fixed a workaround is to use gradle via the command line, as below;
gradle ProjectName:desktop run
This can also be implemented as a run configuration in IntelliJ.

Categories