gradle Error: "You must set the property 'aspectjVersion' before applying the aspectj plugin" when using kotlin DSL - gradle

I want to configure aspectJ plugin in my gradle project which is using kotlin DSL.
Below is my build.gradle.kts file.
val aspectjVersion = "1.9.3"
plugins {
java
id("aspectj.gradle") version "0.1.6"
}
group = "java-agents-demo"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
dependencies {
implementation("org.aspectj","aspectjrt", aspectjVersion)
implementation("org.aspectj","aspectjweaver", aspectjVersion)
testCompile("junit", "junit", "4.12")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
When I run compileJava project I get error "You must set the property 'aspectjVersion' before applying the aspectj plugin".
I set aspectjVersion in my build file but I don't know what is the error in it.
Can anyone help me setup the aspectJ plugin for my project in correct way?

You can also define aspectjVersion=1.9.5 in your gradle.properties file as this is loaded prior to the plugins. This works for Gradle 7 compatibility, too.

So val aspectjVersion = "1.9.3" defines a local variable, but the plugin is looking for a project property, note that this also means the plugin can't be applied straight away as the plugins block is evaluated before the rest of the build.gradle.kts (see limitations of plugin DSL), try this instead:
val aspectjVersion by extra("1.9.3")
plugins {
java
id("aspectj.gradle") version "0.1.6" apply false
}
apply(plugin = "aspectj.gradle")
See Gradle docs on extra properties for details.

Related

gradle does not fetch dependencies after upgrade from 5.1.1 to 6.4.1

I have several services that uses gradle 5.1.1 with java 8.
As we want to upgrade to Java 13, we first need to upgrade to gradle 6after doing so, some dependencies are not fetched.
Those dependencies are listed with compile() under a dependency which is our jar library and still built with gradle 5.1.1
our libraries are stored in a S3 bucket and we use shadowjar to generate the end jar.
so, for example:
I have project A which I want to upgrdae.
Project A has project B as a dependency (compile)
Project B has google guava as a dependency (also with compile)
Now, project A, that under gradle 5.1.1 had fetched guava with no problems, alerting me that it is missing guava after upgrading to gradle 6.
I use local computer installed gradle (not wrapper).
Here are the important build.gradle parts:
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
ext.ver = [
'springboot': '2.2.0.RELEASE',
'slf4j' : '1.7.12'
]
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${ver.springboot}"
classpath 'io.spring.gradle:dependency-management-plugin:1.0.7.BUILD-SNAPSHOT'
classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
classpath 'com.amazonaws:aws-java-sdk-core:1.11.5'
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'maven-publish'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
compileJava {
sourceCompatibility = JavaVersion.VERSION_1_8
}
configurations {
compile.exclude module: 'spring-boot-starter-logging'
testCompile.exclude module: 'spring-boot-starter-logging'
runtime.exclude module: 'spring-boot-starter-logging'
compile.exclude group: 'ch.qos.logback'
}
configurations.all {
resolutionStrategy.cacheDynamicVersionsFor 10, 'seconds'
resolutionStrategy.cacheChangingModulesFor 10, 'seconds'
}
dependencyManagement {
applyMavenExclusions = false
}
repositories {
mavenLocal()
maven {
url "s3://bucket"
credentials(AwsCredentials) {
accessKey = awsCredentials.AWSAccessKeyId
secretKey = awsCredentials.AWSSecretKey
}
metadataSources {
artifact()
}
}
mavenCentral()
}
dependencies {
compile("com.test:projectB:1.0.0")
...
}
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer
shadowJar {
classifier = ''
baseName = 'project-A'
manifest {
attributes 'Main-Class': 'com.test.projectA.Starter'
}
mergeServiceFiles()
append 'META-INF/spring.handlers'
append 'META-INF/spring.schemas'
append 'META-INF/spring.tooling'
transform(PropertiesFileTransformer) {
paths = ['META-INF/spring.factories']
mergeStrategy = "append"
}
}
Could this be because project B was not built with new gradle?
unfortunately, I cannot create a real reproducer as those libraries are real code of the company I work at.
Thanks and Regards,
Ido
The metadataSources declaration of the s3 bucket Maven repository is most likely the root cause why transitive dependencies of projectB are not resolved. The documentation is quite a bit vague here, but I suspect artifact() looks for the actual jar file only and not for the POM file, hence transitive dependency resolution is not performed. You should be able to see this behavior when running the build with switches --info and --refresh-dependencies.
Thankfully, this is quite easy to fix. Add mavenPom() and Gradle will try to resolve the POM first and with that, dependency resolution should be back to normal.
And while you're at it, you might want to read the upgrading from Gradle 5 guide and get rid of the compile configuration in favor of implementation. You should be able to see a warning similar to this when running the build with --warning-mode all:
The compile configuration has been deprecated for dependency declaration. This will fail with an error in Gradle 7.0. Please use the implementation or api configuration instead. Consult the upgrading guide for further information: https://docs.gradle.org/6.4.1/userguide/upgrading_version_5.html#dependencies_should_no_longer_be_declared_using_the_compile_and_runtime_configurations

How to download all spring boot dependencies using gradle

I have searched extensively for the answer to this question. I have extensive knowledge of Maven, but am pretty new to Gradle. I am using Intellij 2019.2 Ultimate. I have my build.gradle set up like so:
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.6.RELEASE'
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation 'org.springframework.boot:spring-boot-dependencies:2.2.6.RELEASE'
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.compilerArgs << '-Xlint:unchecked'
options.deprecation = true
}
compileJava {
options.incremental = true
options.fork = true
options.failOnError = false
}
compileTestJava {
options.incremental = true
options.fork = true
options.failOnError = false
}
springBoot {
mainClassName = "com.app.StartMain"
}
bootWar {
manifest {
attributes 'Start-Class': 'com.app.StartMain'
}
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
I would have thought from this that Gradle would download ALL dependencies in the spring-boot-dependencies-2.2.6.pom file and place in my external dependencies tree in Intellij. But this is not happening. There are a TON of dependencies in that file and I just thought that is how it would behave. Am I missing something in my build file?
Thanks in advance!
The spring-boot-dependencies module is a BOM (bill-of-materials) that contains a curated list of module and library versions that are compatible with Spring Boot. But it only advises on which version to use and doesn't actually make your project depend on them. If you open the POM file for it (here is the one for Spring Boot 2.2.6), you will see that they are all in a <dependencyManagement> block. If they were declared as actual dependencies, they would be in a <dependencies> block directly under the project root. This behaviour is the same for both Maven and Gradle.
I don't think there is an "all" library where you get all those dependencies in your project. But it would also be bad practice as you are unlikely to need them all. Instead, you should use the Spring Boot Starters that makes sense for you.
And even though the current documentation in Spring Boot tells you differently, I would suggest you don't use the io.spring.dependency-management plugin as it configures dependencies in a way that is not standard in Gradle, which can lead to confusion. It was created at a time where Gradle didn't support importing BOM files natively, but it does now. Here is a way to declare dependencies without it:
dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:2.2.6-RELEASE") // Import the BOM
implementation "org.springframework.boot"spring-boot-starter" // Use a Spring Boot starter (and not that there is no version defined)
}

Gradle Kotlin DSL multi project build with Java Modules

I'm creating a new project (using IntelliJ IDEA) that will be using:
Gradle as the build system
Kotlin DSL for build scripts
Java 9 modules for "organisation"
Kotlin as the primary language
I'm having problems setting up Gradle to properly build my project. Most examples I've found are for Groovy and not Kotlin DSL, and most only cover some of the features I want, but not all.
Right now I have two modules, core and lib, where the core module requires the lib module. My gradle build scripts are:
build.gradle.kts
plugins {
base
kotlin("jvm") version "1.3.41" apply false
}
subprojects {
afterEvaluate {
tasks.withType<JavaCompile> {
inputs.property("moduleName", extra["moduleName"])
options.compilerArgs.addAll(arrayOf("--module-path", classpath.asPath))
classpath = files()
}
}
repositories {
mavenCentral()
jcenter()
}
}
core/build.gradle.kts
extra.set("moduleName", "myproject.core")
plugins {
kotlin("jvm")
}
dependencies {
compile(kotlin("stdlib"))
compile(project(":networking"))
}
lib/build.gradle.kts
extra.set("moduleName", "myproject.lib")
plugins {
kotlin("jvm")
}
dependencies {
compile(kotlin("stdlib"))
}
Doing this, configuration fails with:
A problem occurred configuring project ':core'.
Cannot get property 'moduleName' on extra properties extension as it does not exist
If I remove the inputs.property() line the configuration succeeds, but the core compilation fails (lib compiles successfully) with :
Task :core:compileKotlin
e: Module myproject.lib cannot be found in the module graph
I assume the issue is is my root build.gradle.kts, but I cannot figure out how to make it work. Googling around, Kotlin DSL for Gradle is somewhat new and not as widely used, and documentation is pretty scarce.
Any advice would be appreciated!
Naturally after posting the question I found the solution. There exists a Gradle plugin that does exactly what's needed in this situation, with a KotlinDSL example: https://github.com/java9-modularity/gradle-modules-plugin/tree/master/test-project-kotlin
Using the plugin, all I needed to do is change the root build.gradle.kts file:
plugins {
base
kotlin("jvm") version "1.3.41" apply false
id("org.javamodularity.moduleplugin") version "1.5.0" apply false
}
subprojects {
apply(plugin = "org.javamodularity.moduleplugin")
repositories {
mavenCentral()
jcenter()
}
}
Note: Make sure that your module-info.java file is in the java src folder, and not in the kotlin src folder, otherwise the plugin will not detect the module.

How to configure a plugin to depend on a specific version of gradle?

I am writing a set of Gradle plugins, but I want to control the specific versions of groovy and gradle that are used.
I don't want the plugins to depend on whatever versions of Gradle/Groovy are installed, like the following would do:
dependencies {
compile localGroovy()
compile gradleApi()
}
Another reason I don't want to use the local method - when you use a proper dependency specification, Gradle then knows about the source code for those libs and the IDE plugins can hookup the source automatically.
Below are the relevant sections of my build script:
allprojects { Project iProject ->
apply plugin: 'idea'
apply plugin: 'maven'
repositories {
jcenter()
}
}
subprojects { Project iProject ->
apply plugin: 'groovy'
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.2'
}
}
project(':eclipsei2g') {
group = 'eclipsei2g'
version = '0.0.1-SNAPSHOT'
dependencies {
compile 'org.gradle:gradle-core:2.0'
}
}
project(':g2idea13') {
group = 'g2idea13'
version = '0.0.1-SNAPSHOT'
dependencies {
compile 'org.gradle:gradle-core:2.0'
compile 'org.gradle-plugins:gradle-ide:2.0'
}
}
When I run this I get an error resolving the gradle-ide dependency:
Could not resolve all dependencies for configuration ':g2idea13:compile'.
> Could not find org.gradle:gradle-ide:2.0.
Searched in the following locations:
http://jcenter.bintray.com/org/gradle/gradle-ide/2.0/gradle-ide-2.0.pom
http://jcenter.bintray.com/org/gradle/gradle-ide/2.0/gradle-ide-2.0.jar
Required by:
g2idea13:g2idea13:0.0.1-SNAPSHOT
There doesn't seem to be anything on the jcenter repository since 0.9 for the plugins stuff.
I also tried 'org.gradle:gradle-ide:2.0'.
Is this even how I should be doing this? Is there another way to specify a specific gradle version? Am I just using the wrong repository? I couldn't even get gradle-core to resolve on mavenCentral(). Is there an official Gradle repository somewhere that I should be using?
gradleApi() is the way to go. There isn't currently a public list of dependencies for Gradle plugins.

Cannot access Guava in Android Studio

I am new to Android Studio.
I'm trying to add Guava to a module but somehow the reference is not resolved. This is my build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.1'
}
}
repositories {
mavenCentral();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.5'
compile 'com.google.appengine:appengine-endpoints:1.9.5'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.5'
compile 'javax.servlet:servlet-api:2.5'
compile 'com.googlecode.objectify:objectify:5.0.2'
compile 'com.google.guava:guava:17.0'
}
appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
endpoints {
getClientLibsOnBuild = true
getDiscoveryDocsOnBuild = true
}
}
As I can see in some other posts, the main mistake that was made is the missing reference to mavenCentral() at root level. This seems to be correct here, but the following import does not work:
import com.google.guava;
"Cannot resolve symbol 'guava'."
I have re-synced the IDE, which did not help. I have also tried to refresh dependencies:
gradlew --refresh-dependencies
I have then added an older version number to see if gradle recognizes the newer version, and it does show me in gradle.build that a newer version exists.
Further, all other references resolve fine.
Anybody has an idea what's missing?
Guava functions and data structures live under the com.google.common.* package (not com.google.guava, as the maven URL would suggest).
See documentation here: http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/index.html

Resources