Gradle dependency on project for all configurations - configuration

I'm looking into using Gradle instead of Ant/Ivy. I'm trying to create dependencies between my projects for all configurations, so that for example, project1.compile depends on project2.compile, project1.runtime depends on project2.runtime, etc.
In Ivy, I did this with the following XML:
project1/ivy.xml
<dependency conf="*->#" org="myorg" name="project2" rev="latest.integration" />
In Gradle, here's what I have tried:
project1/build.gradle
configurations.each { config ->
config.dependencies.add project(path: ':project2', configuration: config.name)
}
But it complains that the project function doesn't exist:
> Could not find method project() for arguments [{path=:project2, configuration=archives}] on project ':project1'.
Any ideas how to do this?

configurations.all { config ->
project.dependencies.add(config.name,
project.dependencies.project(
path: ':project2', configuration: config.name))
}

To anyone looking for a working answer to the same question in 2023:
configurations.all {
withDependencies {
add(
project.dependencies.module("org.example:example-artifact:0.0.1") // external dependency
)
add(
project.dependencies.platform("org.example:example-bom:0.0.1") // platform BOM
)
add(
project.dependencies.project(":project2") // project submodule
)
}
}

Related

Jaxb Plugin Issue with gradle7 and jdk11

I am trying to run build locally using gradle 7.4 and jdk 11 with below setting in build.gradle.
apply plugin: 'com.github.jacobono.jaxb'
dependencies {
.....
jaxb "com.sun.xml.bind:jaxb-xjc:3.0.2"
jaxb "com.sun.xml.bind:jaxb-impl:3.0.2"
}
jaxb {
xsdDir='src/main/resources/xsd' // Define the folder for location of the schema file
xjc {
generatePackage 'com.demo.bizid.entities' // Set the package name for generated source files
destinationDir='build/jaxb-generated'
}
}
sourceSets.main.java.srcDirs 'build/jaxb-generated'
tasks.withType(JavaCompile) {
dependsOn xjc
}
But getting build issue with below error
Some problems were found with the configuration of task ':xsd-dependency-tree' (type 'JaxbDependencyTree').
- In plugin 'com.github.jacobono.jaxb' type 'org.gradle.jacobo.plugins.task.JaxbDependencyTree' property 'dependencyTreeFactory' is missing an input or output annotation.
Reason: A property without annotation isn't considered during up-to-date checking.

LibGDX html:superDev task giving me an error

So I've been trying to run the libGDX command "gradlew html:superDev" for my project but it always gives me and error part way through the process:
> Task :html:beforeRun FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':html:beforeRun'.
> Could not resolve all files for configuration ':html:grettyRunnerJetty94'.
> Could not find org.gretty:gretty-runner-jetty94:3.0.2.
Searched in the following locations:
- file:/C:/Users/Joachim/.m2/repository/org/gretty/gretty-runner-jetty94/3.0.2/gretty-runner-jetty94-3.0.2.pom
- https://repo.maven.apache.org/maven2/org/gretty/gretty-runner-jetty94/3.0.2/gretty-runner-jetty94-3.0.2.pom
- https://dl.google.com/dl/android/maven2/org/gretty/gretty-runner-jetty94/3.0.2/gretty-runner-jetty94-3.0.2.pom
- https://oss.sonatype.org/content/repositories/snapshots/org/gretty/gretty-runner-jetty94/3.0.2/gretty-runner-jetty94-3.0.2.pom
- https://oss.sonatype.org/content/repositories/releases/org/gretty/gretty-runner-jetty94/3.0.2/gretty-runner-jetty94-3.0.2.pom
Required by:
project :html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings
This is what the build.gradle file looks like (the one located in the project folder):
buildscript {
repositories {
gradlePluginPortal()
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
google()
}
dependencies {
classpath 'org.wisepersist:gwt-gradle-plugin:1.0.13'
classpath 'org.gretty:gretty:3.0.3'
}
}
allprojects {
apply plugin: "eclipse"
version = '1.0'
ext {
appName = "ludum-dare-48"
gdxVersion = '1.9.14'
roboVMVersion = '2.3.12'
box2DLightsVersion = '1.5'
ashleyVersion = '1.7.3'
aiVersion = '1.8.2'
gdxControllersVersion = '2.1.0'
}
repositories {
gradlePluginPortal()
mavenLocal()
mavenCentral()
google()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "java-library"
dependencies {
implementation project(":core")
api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
}
}
project(":html") {
apply plugin: "java-library"
apply plugin: "gwt"
apply plugin: "war"
apply plugin: "org.gretty"
dependencies {
implementation project(":core")
api "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
api "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
api "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
api "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion:sources"
}
}
project(":core") {
apply plugin: "java-library"
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
}
}
Any help in fixing this problem would be very much appreciated. Thanks!
In the maven reposotories it seems that there is no such version 3.0.2 of the project you are looking for. Maybe it got deleted and replaced by version 3.0.3.
So a solution would be to check your build.gradle file (probably the one in the html project) and replace the string org.gretty:gretty-runner-jetty94:3.0.2 by either org.gretty:gretty-runner-jetty94:3.0.3 or org.gretty:gretty-runner-jetty94:2.3.0.
You should try version 3.0.3 first, since 2.3.0 might be lacking some features.
Ok so I went to my main gradle.build file and in the repositories {} block for both the buildscript {} block and the allprojects {} block I put gradlePluginPortal() and now the superDev command works fine.

How to use com.google.appengine.labs.repackaged.org.json in Android Studio

I have working code which uses JSON with package shown below.
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
Code runs in app engine, built under eclipse. And now I'm trying to migrate it to Android Studio. But I can't find out how to refer correct library with it, so I can't compile. My current build.gradle file is shown below.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.18'
}
}
repositories {
jcenter();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
war.dependsOn appengineEnhance
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.18'
compile 'com.google.appengine:appengine-endpoints:1.9.18'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.18'
compile 'com.google.appengine.orm:datanucleus-appengine:2.1.2'
compile 'javax.servlet:servlet-api:2.5'
compile 'javax.jdo:jdo2-api:2.3-eb'
compile 'org.datanucleus:datanucleus-api-jdo:3.1.3'
}
appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
enhancer {
version = "v2"
api="jdo"
enhanceOnBuild = true
}
}
There are other libraries I need to add but I want to fix this issue first.
I found a solution. Adding next line to build.gradle made successful compile.
compile 'com.google.appengine:appengine-api-labs:1.7.4'
Now I can go forward to run/debug server side code.

Error while building fat jar for deploying my jar to remote Cluster

I have set up a single node cluster and trying to deploy my sample topology to it .
I believe to deploy my topology to cluster I have to submit the jar with all dependencies to the Cluster .
For that I created a sample project and added a simple topology to it .
While generating the fat jar using gradle I am seeing this error
gradle fatjar gives below error
Could not expand ZIP '/Users/agarg/.gradle/caches/modules-2/files-
2.1/org.apache.storm/storm-
core/0.9.5/d2bf27db853347dcf66990b4514db20a7897303e/storm-core-0.9.5.jar'.
Could not copy zip entry /Users/agarg/.gradle/caches/modules-2/files-
2.1/org.apache.storm/storm-
core/0.9.5/d2bf27db853347dcf66990b4514db20a7897303e/storm-core-
0.9.5.jar!META-INF/license/LICENSE.base64.txt to '/Users/agarg/Documents/notificationRepo/sample/build/tmp/expandedArchives/storm-
core-0.9.5.jar_366us3312tpl54tci2fld83fij/META-INF/license/LICENSE.base64.txt'.
Here is my build.gradle file fo reference :
dependencies {
compile group: 'clj-stacktrace' , name: 'clj-stacktrace',version: cljStackTrace
compile group: 'org.apache.storm' , name: 'storm-core',version: stormVersion
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'storm.topology.ExclamationTopology'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
buildscript {
repositories {
mavenCentral()
maven { url 'http://repo.spring.io/snapshot' }
maven { url 'http://repo.spring.io/milestone' }
maven { url "https://clojars.org/repo/" }
maven { url "http://repo.maven.apache.org/maven2" }
}
}
Can nyone help me what is going wrong here ???
Thanks In Advance
I am not familiar with gradle, however, you do not need to include "storm-core" or transitive dependencies in your fat jar. It is sufficient if your far jar contains Spout and Bolt classes (and maybe some 3rd party libraries you are using that are not part of Storm)

Gradle custom task only applied to certain subprojects?

I have created a custom herpderp Gradle task:
task herpderp(type: HerpDerpTask)
class HerpDerpTask extends DefaultTask {
#TaskAction
def herpderp() {
println "Herp derp!"
}
}
With this, I can add this task to other Gradle builds an use it inside build invocations for other projects:
gradle clean build herpderp
Now, I have the following multi-project setup:
myapp/
myapp-client/
build.gradle
src/** (omitted for brevity)
myapp-shared/
build.gradle
src/** (omitted for brevity)
myapp-server
build.gradle
src/** (omitted for brevity)
build.gradle
settings.gradle
Where myapp/build.gradle is:
subprojects {
apply plugin: 'groovy'
sourceCompatibility = '1.7'
targetCompatibility = '1.7'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile (
'org.codehaus.groovy:groovy-all:2.3.7'
)
}
}
And where myapp/settings.gradle is:
include ':myapp-shared'
include ':myapp-client'
include ':myapp-server'
I would like to be able to navigate to the parent myapp directory, and run gradler clean build herpderp and have the herpderp task only run on the myapp-client and myapp-shared projects (not the server project).
So it sounds like I need either another custom task or some type of closure/method inside myapp/build.gradle that:
Runs clean build; then
Drops (cd) into myapp-client and runs herpderp; and then
Drop into myapp-shared and runs herpderp.
What do I need to add to any of my files in order to get herpderp invoked from the parent build command, but only executed in the client and shared subprojects?
The following piece of code can do the trick (should be placed in myapp/build.gradle):
allprojects.findAll { it.name in ['myapp-client', 'myapp-shared'] }. each { p ->
configure(p) {
task herpderp(type: HerpDerpTask)
}
}
class HerpDerpTask extends DefaultTask {
#TaskAction
def herpderp() {
println "Herp derp from ${project.name}!"
}
}
As indicated in the Gradle documentation, you can filter subprojects and configure them this way:
configure(subprojects.findAll { it.name.endsWith("server") }) {
apply plugin: 'com.google.cloud.tools.jib'
jib {
from {
image = 'openjdk:alpine'
}
}
...
}