How to disable gradle plugin by environments - spring-boot

I have two different environments say lower env and production. I have few gradle dependencies which should not get downloaded or used in production. So I want to block those plugins to get downloaded or activated in PROD.
I want the below plugins not to get added in prod, but in development and test environments .
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-sleuth-zipkin-stream
compile group: 'org.springframework.cloud', name: 'spring-cloud-sleuth-zipkin-stream', version: '1.0.0.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-sleuth
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '2.1.1.RELEASE'

There are two recommended ways to disable the Daemon persistently for
an environment: Via environment variables: add the flag
-Dorg.gradle.daemon=false to the GRADLE_OPTS environment variable. Via properties file: add org.gradle.daemon=false to the
«GRADLE_USER_HOME»/gradle.properties file.

Related

gradle default configuration, what is it and how can I define it

I am having a weird error, for which I found a lot of hits in google but most about android studio or library imports and I don't use android studio nor am I trying to build any app/library so I'll try and ask here and see if I understand the background of it.
I have a project in which I have this plugin:
https://gitlab.com/zkovari/gradle-mermaid-plugin/-/blob/master/examples/single-project/build.gradle
This is a plugin that displays my dependencies as a mermaid graph.
Everything was fine until today I updated some dependencies and created a sub-project to do something else.
I'm trying to generate the graph again and somehow it's failing with this error message.
> Task :generateMermaidDependenciesDiagram FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':generateMermaidDependenciesDiagram'.
> Configuration with name 'default' not found.
I can see in the source code of the plugin that it automatically sets some configurations
#Override
public void apply(Project project) {
GenerateMermaidDependenciesDiagram generateDependencyDiagramsTask = project.getTasks()
.create(GENERATE_MERMAID_DEPENDENCIES_DIAGRAM_TASK_NAME, GenerateMermaidDependenciesDiagram.class);
generateDependencyDiagramsTask.setConfiguration("default");
generateDependencyDiagramsTask
.setOutput(project.getBuildDir().toPath().resolve("mermaid/dependencies.mmd").toFile());
generateDependencyDiagramsTask.setDiagramGenerator(new MermaidDiagramGenerator());
}
So my question is a bit high level but what is the "default" configuration?
And in my project I don't really have any configuration block I simply have my dependencies:
dependencies {
implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: awsVersion, transitive: false
...
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: slf4jVersion
}
My structure looks like this:
project:
-- build.gradle
-- settings.gradle
-- sub-project:
--- build.gradle
I think this stopped working when I added my sub-project but the plugin is applied at the root project level.
I do have a dependency between my sub-project and my root project (one of my sub-project tasks depends on the root project)
I'm trying to understand that this might be very similar but I'm not fully getting it.
Gradle: What Is The Default Configuration and How Do I Change It
I do apply the java plugin on my main project so I expect the configuration.default should exist.
Even if I think I shouldn't do it I tried to re-define a default configuration extending implementation but that did not work
https://docs.gradle.org/current/userguide/declaring_dependencies.html
configurations {
default.extendsFrom implementation
}
build file 'C:\dev\repo\connector-custom\build.gradle': 66: unexpected token: default # line 66, column 5.
default.extendsFrom implementation
I did a quick test and removing my subproject did work so I know the problem is there but no idea why.
It means somehow I must pass to my subproject the default configuration of the main project?

Liferay 7.2 No value has been specified for property 'apiDir'

I created a module project using servicebuild template (gradebook-api, gradebook-service), but i have an error after an export package com.liferay.training.gradebook.validator in api below
Bundle-Name: gradebook-api
Bundle-SymbolicName: com.liferay.training.gradebook.api
Bundle-Version: 1.0.0
Export-Package: \
com.liferay.training.gradebook.exception,\
com.liferay.training.gradebook.model,\
com.liferay.training.gradebook.service,\
com.liferay.training.gradebook.service.persistence,\
com.liferay.training.gradebook.validator
-check: EXPORTS
-includeresource: META-INF/service.xml=../gradebook-service/service.xml
i have a problem in the BuildService gradle task that's says :
Some problems were found with the configuration of task
':modules:gradebook:gradebook-api:buildService' (type
'BuildServiceTask').
File 'C:\Liferay\ide-workspace\training-workspace\modules\gradebook\gradebook-api\service.xml'
specified for property 'inputFile' does not exist.
No value has been specified for property 'apiDir'.
And this is a how I add the api module as dependency in service.
dependencies {
compileOnly group: "com.liferay", name: "com.liferay.petra.io"
compileOnly group: "com.liferay", name: "com.liferay.petra.lang"
compileOnly group: "com.liferay", name: "com.liferay.petra.string"
compileOnly group: "com.liferay", name: "com.liferay.portal.aop.api"
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel"
compileOnly group: "org.osgi", name: "org.osgi.annotation.versioning"
compileOnly group: "org.osgi", name: "org.osgi.core"
compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations"
compileOnly group: "javax.portlet", name: "portlet-api"
compileOnly group: "javax.servlet", name: "javax.servlet-api"
compile project(":modules:gradebook:gradebook-api")
}
buildService {
apiDir = "../gradebook-api/src/main/java"
}
group = "com.liferay.training.gradebook"
I am trying to create new Liferay workspace but i got the same error (May be this is an error with Gradle)
Be sure, you run gradle task in the corresponding folder (in gradebook-service folder). I got similar error when I ran it in upper folder which contains api and service folder.
Until recently I have only worked on LR 7.0, but the last day or so have been playing with 7.3. I encountered the exact issue you describe.
The only solution I found was to copy service.xml from gradebook-service into the root of both my gradebook-api and gradebook-web modules and also add the following to the build.gradle files of both the gradebook-api and gradebook-web modules:
buildService {
apiDir = "../gradebook-api/src/main/java"
}
After that service builder ran successfully. There may be a cleaner way around it, but this worked for me.
service.xml should stay in service module only. Here, it look you need few checks.
api module dependency should be compileonly. not in compile
scope.
block order also matters in gradle file. move below block
at the top of the file.
buildService {
apiDir = "../gradebook-api/src/main/java"
}
not sure why you need -check:exports header in bnd file. As, that is not required normally.
In the Gradle task tab, go to your specific module service folder and then build it from there. Don't forget to refresh your gradle by pressing Cntrl+F5.
This Worked for me.
I had the same situation and just added the buildService{...} in build.gradle in the api module.
buildService {
apiDir = "../gradebook-api/src/main/java"
}
After: BUILD SUCCESSFUL

How to setup Elasticsearch for your tests using build.gradle

So, I have been trying to write JUnit tests for my APIs which interact with Elasticsearch. This is like Integration test where I need to setup Elasticsearch before I can run my code.
For all the tests, I need to create a test task which will do following:
Download the zip from
compile group: 'org.elasticsearch.distribution.zip', name: 'elasticsearch', version: '6.1.1', ext: 'pom'
Run elasticsearch executable present in /bin of unzipped file. When running this executable, take as argument elasticsearch.yml file in the command.
Once the all the tests are run, stop the elasticsearch executable, and clean the zipped folder.
Things that I can improve is that if zip file is already present in my gradle cache, then don't download it again and again.
Thanks
Add following dependency to your build.gradle file
configurations {
elasticDist
}
...
dependencies {
elasticDist group: 'org.elasticsearch.distribution.zip', name: 'elasticsearch', version: '6.1.2', ext: 'zip'
}
Add tasks to unzip and cleanup, setup dependencies
task unzip(type: Copy) {
// to download distribution
dependsOn configurations.elasticDist
from { // use of closure defers evaluation until execution time
configurations.elasticDist.collect { zipTree(it) }
}
into file("${buildDir}/your/destination")
}
task cleanElastic(type:Delete) {
delete file("${buildDir}/your/destination")
}
test.dependsOn('unzip')
test.finalizedBy('cleanElastic')
Using your test framework of choice, configure setUp and tearDown to start and stop elastic respectively.

How to compile different flavors while building a fat jar using Gradle

I want to include the Java Bindings for V8 ("J2V8") in a Java project. The reasons are that (i) the V8 JavaScript engine is much faster then the JavaScript engine shipped with the JRE and (ii) the library I am using is available in JavaScript only and a port to Java is much effort.
The issue is that J2V8 is compiled for different platforms: linux 64bit, macos 64bit, windows 64 bit, windows 32 bit.
I now want to generate different JARs, containing all the dependencies (fat jars):
jabref-linux_x86_64.jar
jabref-macosx_x86_64.jar
jabref-windows_x86_32.jar
jabref-windows_x86_64.jar
jabref-all.jar - the platform indipendent JAR without v8 engine
I am currently creating fat jars using the shadow plugin.
Note that the project is not an Android project. There, with the Android plugin, it seems to be straight-forward to do that.
The first idea is to introduce configurations and configuration-specific dependencies:
configurations {
linux_x86_64Compile.extendsFrom compile
macosx_x86_64Compile.extendsFrom compile
windows_x86_32Compile.extendsFrom compile
windows_x86_64Compile.extendsFrom compile
}
dependencies {
compile configuration: 'linux_x86_64', group: 'com.eclipsesource.j2v8', name: 'j2v8_linux_x86_x64', version: '4.6.0'
compile configuration: 'macosx_x86_64', group: 'com.eclipsesource.j2v8', name: 'j2v8_macosx_x86_x64', version: '4.6.0'
compile configuration: 'windows_x86_32', group: 'com.eclipsesource.j2v8', name: 'j2v8_win32_x86', version: '4.6.0'
compile configuration: 'windows_x86_64', group: 'com.eclipsesource.j2v8', name: 'j2v8_win32_x86_x64', version: '4.6.0'
...
}
But now I'm stuck. In pseudocode, I'd like to do:
task releaseSingleJar(dependsOn: "shadowJar", name) {
doLast {
copy {
from("$buildDir/libs/JabRef-${project.version}-fat.jar")
into("$buildDir/releases/")
rename { String fileName ->
fileName.replace('-fat', '-$name')
}
}
}
}
task releaseJars() {
forEach name in "linux_x86_64", "macosx_x86_64", "windows_x86_32", "windows_x86_64", "all":
if (name != "all") activate configuration $name
releaseSingleJar($name)
shadowJar is from the shadow plugin.
Background information
Video showing the difference of speed in our setting: https://github.com/JabRef/jabref/pull/2250#issuecomment-264824598
Current state of the integration in JabRef: https://github.com/JabRef/jabref/pull/3180
Related questions
The question Using Gradle to manage Java web app with flavors like Android has a similar title, but asks for source directories, whereas I ask for dependencies. Further, I want to generate a fat JAR and there a plain JAR seems to be enough. However, it might be that the solution is similar. A hint was to use the gradle-java-flavours plugin with the main source being JavaFlavoursExtension.groovy.
Following questions are similar to this one. However, the setting is related to Android apps and not to plain Java apps.
How to define different dependencies for different product flavors
Gradle: add dependency for a specific flavour of the library
Change dependency through a task in gradle
Use different resources for different application flavors using gradle
You might be interested in my gradle-java-flavours plugin which creates source sets, configurations and compile, jar and test tasks for each flavour in a java only project.
eg:
import com.github.jengelman.gradle.plugins.shadow.tasks.*
plugins {
id 'com.lazan.javaflavours' version '1.2'
id 'com.github.johnrengelman.shadow' version '1.2.4'
}
def flavours = ['linux_x86_64', 'macosx_x86_64', ...]
javaFlavours {
flavours.each {
flavour it
}
}
dependencies {
linux_x86_64Compile 'aaa:aaa:1.0'
linux_x86_64Runtime 'bbb:bbb:1.0'
macosx_x86_64TestCompile 'ccc:ccc:3.0'
}
flavours.each { String flavour ->
SourceSet flavourSourceSet = sourceSets.getByName(flavour)
Configuration flavourRuntime = configurations.getByName("${flavour}Runtime")
JavaCompile flavourCompileTask = tasks.getByName("compile${flavour.capitalize()}Java")
Task shadowJarTask = tasks.create(name: "${flavour}ShadowJar", type: ShadowJar) {
classifier = "${flavour}-all"
dependsOn flavourCompileTask
// todo configure (likely based on Configuration and SourceSet)
}
artifacts {
archives shadowJarTask
}
// wire the task into the DAG
assemble.dependsOn shadowJarTask
}

How can i have a property in the compile group gradle file

Hi I want to use a gradle property in my gradle build file like this:
compile group: 'org.hibernate', name: 'hibernate-core', version: '${hibernateVersion}'
and i have in the gradle.properties this entry
hibernateVersion:5.2.6.Final
But each time it gives me error:
Could not create an instance of type org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency_Decorated
Try:
compile "org.hibernate:hibernate-core:$hibernateVersion"

Resources