How to set system property using gradle? - gradle

I was wondering if it's possible to set a system property, for a Java application, using Gradle?
I tried using gradle.properties file and defining a property as
systemProp.name = my name
but then when I try to get that property from a Java application using
System.getProperty("name")
that property is not found.
build.gradle and gradle.properties are in root folder of the project.
This is what my build.gradle looks like:
apply plugin: 'war'
apply plugin: 'appengine'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.8.6'
}
}
appengine {
httpPort = 8081
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '3.0'
compile group: 'com.google.inject.extensions', name: 'guice-servlet', version: '3.0'
compile group: 'javax.servlet', name: 'servlet-api', version: '2.5'
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '4.2.0.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.27'
compile 'com.google.protobuf:protobuf-java:2.5.0'
compile 'com.google.appengine:appengine-api-1.0-sdk:1.8.6'
compile 'com.google.template:soy:2012-12-21'
compile 'org.json:json:20090211'
}
And this is what my build.properties look like:
systemProp.firstName=Marko
systemProp.lastName=Vuksanovic
This is part of an AppEngine application and I run it using the following command:
gradle appengineRun

This is how I do it, setting props for Selenide test framework:
test {
systemProperty "browser", "chrome"
systemProperty "webdriver.chrome.driver", "$projectDir/drivers/chromedriver"
}

I just stumbled on this for use with the gradle application plugin. Add to the run task. Works great:
run {
systemProperties['MYPROP'] = '/my/prop/value'
}

This is what worked for me (using Kotlin DSL):
tasks.withType<JavaExec> {
systemProperty("key", "value")
}

The system property set in gradle.properties will be available only in JVM where Gradle is running.
From gradle-appengine-plugin documentation:
appengineRun: Starts a local development server running your project
code.
jvmFlags: The JVM flags to pass on to the local development server.
If you need your system properties to be available in app engine, which is separate JVM, you should use jvmFlags property.
Explicitly:
appengine {
jvmFlags = ['-DfirstName=Marko', '-DlastName=Vuksanovic']
}
With gradle.properties:
appengine {
jvmFlags = ['-DfirstName=$firstName', '-DlastName=$lastName']
}

Related

Groovy compilation fails: Unable to load class 'org.grails.io.support.Resource'

I'm developing a JavaFX application written in Groovy and using Gradle. When I started up my application in IntelliJ recently, it seemingly from out of the blue started failing to compile with the error:
Unable to load class 'org.grails.io.support.Resource'.
This is an unexpected error. Please file a bug containing the idea.log file.
This is from running the "run" gradle task.
This is my build file:
plugins {
id 'groovy'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
group 'redacted'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven { url "https://m2proxy.atlassian.com/repository/public" } // Atlassian repository
maven { url "https://maven.atlassian.com/content/repositories/atlassian-public/"} // Atlassian public
maven { url "https://download.java.net/maven/2/"}
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:3.0.5'
// Logback log-annotation
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.6'
// https://mvnrepository.com/artifact/org.controlsfx/controlsfx
implementation group: 'org.controlsfx', name: 'controlsfx', version: '11.1.0'
testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
// ---- GPars concurrency lib ----
// https://mvnrepository.com/artifact/org.codehaus.gpars/gpars
implementation group: 'org.codehaus.gpars', name: 'gpars', version: '1.2.1'
// ---- Jira Client ----
// https://mvnrepository.com/artifact/com.atlassian.jira/jira-rest-java-client-app
implementation group: 'com.atlassian.jira', name: 'jira-rest-java-client-app', version: '5.2.0'
// https://mvnrepository.com/artifact/com.atlassian.jira/jira-rest-java-client-api
implementation group: 'com.atlassian.jira', name: 'jira-rest-java-client-api', version: '5.2.0'
// ---- Misc ----
// https://mvnrepository.com/artifact/io.github.cdimascio/java-dotenv
implementation group: 'io.github.cdimascio', name: 'java-dotenv', version: '5.2.2'
// https://mvnrepository.com/artifact/org.jsoup/jsoup
implementation group: 'org.jsoup', name: 'jsoup', version: '1.14.3'
// ---- REST ----
// https://mvnrepository.com/artifact/org.springframework/spring-web
implementation group: 'org.springframework', name: 'spring-web', version: '3.0.2.RELEASE'
// https://mvnrepository.com/artifact/org.grails/grails-datastore-rest-client
implementation group: 'org.grails', name: 'grails-datastore-rest-client', version: '6.1.9.RELEASE'
// https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
implementation group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
// https://mvnrepository.com/artifact/net.dongliu/requests
implementation group: 'net.dongliu', name: 'requests', version: '5.0.8'
// https://mvnrepository.com/artifact/io.jsondb/jsondb-core
implementation group: 'io.jsondb', name: 'jsondb-core', version: '1.0.115-j11'
}
test {
useJUnitPlatform()
}
javafx {
modules = ['javafx.controls', 'javafx.fxml']
version = '11.0.2'
}
mainClassName = 'redacted'
Has anyone seen this before? The things I've tried:
Invalidating caches in IntelliJ
Clean + Rebuild
Rollback code to a point I know compiled.
Update gradlew to use gradle 7.4
UPDATE
I seem to have found the culprit. My company has recently changed stuff related to how we build our projects, which requires using a init.gradle file in the .gradle/ folder, which contains some standard repository definitions. This is the content (with company repositories redacted):
allprojects {
apply plugin: 'java'
apply plugin: 'maven-publish'
buildscript {
repositories {
maven {
url "https://<redacted>"
credentials {
username mavenUser
password mavenPassword
}
}
}
}
repositories {
mavenLocal()
maven {
url "https://<redacted>"
credentials {
username mavenUser
password mavenPassword
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
credentials {
username mavenUser
password mavenPassword
}
url "https://<redacted>"
}
}
}
}
Removing it fixed the problem for the project in question, but will obviously not work for all the other work related projects. So the question remains...how does this cause such an error?
UPDATE 2
I have not exactly found the source of the error, but it seems to have something to do with the repositories being declared in a wrong way. To fix this, one can call clear() in the project build.gradle file at the top of the repositories definition, effectively ignoring what's declared in init.gradle. So, just update the repositories {} clause to:
repositories {
clear() // This is needed in order for init.gradle to not cause errors
mavenCentral()
maven { url "https://m2proxy.atlassian.com/repository/public" } // Atlassian repository
maven { url "https://maven.atlassian.com/content/repositories/atlassian-public/"} // Atlassian public
maven { url "https://download.java.net/maven/2/"}
}
I have not exactly found the source of the error, but it seems to have something to do with the repositories being declared in a wrong way. To fix this, one can call clear() in the project build.gradle file at the top of the repositories definition, effectively ignoring what's declared in init.gradle. So, just update the repositories closure to:
repositories {
clear() // This is needed in order for init.gradle to not cause errors
mavenCentral()
maven { url "https://m2proxy.atlassian.com/repository/public" } // Atlassian repository
maven { url "https://maven.atlassian.com/content/repositories/atlassian-public/"} // Atlassian public
maven { url "https://download.java.net/maven/2/"}
}

How to enable Allure history using Gradle and Junit5?

I know I could copy the history directory from allure-reports to a generated allure-results, and then do allure generate to get the history to show up, but I am looking for a way to achieve this with the built-in functionality in allure-gradle.
Currently, I run ./gradlew clean test, then ./gradlew allureReport, and this gives me a fresh html report with no history or reruns. I have noticed that the test task deletes the entire allure-reports directory and then re-makes and re-populates it.
Here is my build.gradle:
buildscript {
repositories {
mavenLocal()
jcenter()
}
}
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
id 'java'
id 'eclipse'
// Allure reporter
id 'io.qameta.allure' version '2.8.1'
}
repositories {
mavenCentral()
jcenter()
}
def allureVersion = "2.13.1"
allure {
autoconfigure = true
version = allureVersion
useJUnit5 {
version = '2.0-BETA10'
}
downloadLink = "https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.13.1/allure-commandline-2.13.1.zip"
}
test {
useJUnitPlatform() {
includeEngines 'junit-jupiter'
}
ignoreFailures = true
}
dependencies {
compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.0'
compile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.0'
compile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.6.0'
// Needed as a workaround for JUnit 5 not failing tests that stopped due to an error message. The fix is in eclipse 4.10
compile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '5.6.0'
compile group: 'io.qameta.allure', name: 'allure-junit5', version: '2.13.1'
compile group: 'io.qameta.allure', name: 'allure-java-commons', version: '2.13.1'
// https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
}
I have also tried specifying the results and reports directories to be outside of the build directory, like this:
allure {
autoconfigure = true
version = allureVersion
resultsDir = file('../../allure-results')
reportDir = file('../../allure-report')
...
}
This allows the allure-results folder to keep the previous report, which show up as retries (not quite right), but history still doesn't work.
I have solved this problem by adding at the running test script these lines( I have some tests scuites)
call gradlew test -PsuiteFile=YOUR_TEST_SUITE.xml
mkdir allure-results\history
xcopy build\allure-results\* allure-results\history
allure serve allure-results\history
So I have added folder allure-results\history to keep all results

Gradle not imported dependency with ext:pom

I want import library org.geotools.
Added raw compile group: 'org.geotools', name: 'geotools', version: '16.1'
or compile group: 'org.geotools', name: 'geotools', version: '16.1', ext: 'pom'
and repo:
repositories {
mavenLocal()
maven { url 'http://repo.boundlessgeo.com/main' }
maven { url "http://download.osgeo.org/webdav/geotools/" }
maven { url "http://download.java.net/maven/2" }
maven { url "http://repo.opengeo.org" }
mavenCentral()
}
BUILD SUCCESSFUL in 47s
but in external libraries it not found.
I not a gradle user but don't you need to add some actual dependencies?
For GeoTools I usually start with at least some of these:
gt-main
gt-metadata
gt-referencing
and a datasource or 2.

Gradle Plugin Execution Order

I have written this gradle file
group 'com.abhi'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'org.flywaydb.flyway'
sourceCompatibility = 1.8
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.flywaydb:flyway-gradle-plugin:3.2.1'
classpath 'org.jooq:jooq-codegen:3.7.1'
classpath 'com.h2database:h2:1.4.177'
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jooq', name: 'jooq', version: '3.7.1'
compile group: 'org.jooq', name: 'jooq-meta', version: '3.7.1'
compile group: 'org.jooq', name: 'jooq-codegen', version: '3.7.1'
runtime group: 'com.h2database', name: 'h2', version: '1.4.177'
}
flyway {
url = 'jdbc:h2:file:target/foobar'
user = 'sa'
}
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
.configuration('xmlns': 'http://www.jooq.org/xsd/jooq-codegen-3.7.0.xsd') {
jdbc() {
driver('org.h2.Driver')
url('dbc:h2:file:target/foobar')
user('sa')
password('')
}
generator() {
database() {
}
generate() {
}
target() {
packageName('com.abhi.jooq.models')
directory('src/main/java')
}
}
}
// Run the code generator
// ----------------------
org.jooq.util.GenerationTool.generate(
javax.xml.bind.JAXB.unmarshal(new StringReader(writer.toString()), org.jooq.util.jaxb.Configuration.class)
)
when I say gradle compile it throws an exception
Could not load schemata
java.lang.NullPointerException
at org.jooq.impl.MetaImpl.meta(MetaImpl.java:120)
at org.jooq.impl.MetaImpl.getCatalogs(MetaImpl.java:143)
at org.jooq.impl.MetaImpl.getSchemas(MetaImpl.java:168)
at org.jooq.util.jdbc.JDBCDatabase.getSchemasFromMeta(JDBCDatabase.java:135)
at org.jooq.util.jdbc.JDBCDatabase.getSchemata0(JDBCDatabase.java:124)
at org.jooq.util.AbstractDatabase.getSchemata(AbstractDatabase.java:279)
I think the problem is that the code at the bottom of the script executes much before the "flyway" plugin.
Is there a way I can ensure that the code below is executed only after the flyway plugin has executed?
Yes, if you suppose, the problem is in execution order, then you can modify it, just like with any other tasks. Take a look at the documentation of the flyway plugin. According to it, this plugin adds some extra tasks to your build script, for thos one tasks are: flywayMigrate, flywayClean etc. You can make any of your tasks depending (with dependsOn option) on the tasks form this plugin and make them running just after the plugin's job is done.

Spring-Boot html in /templates don't render from command line

I have read several online posts about this issue, but for some reason none of those helped me solve this. I looked at the spring-boot examples and I don't think I am doing much different than those.
I am using Spring-boot 1.2.1, Thymeleaf, embedded tomcat without issue in IntelliJ. However, when I do a "gradle clean build", move the jar file, and an application.properties file to a deploy directory. I then start on the command with:
java -jar edm-0.1.0.jar
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/loginPage", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245)
I am letting spring-boot configure my site for the most part. I am using the recommended directory structure:
I can hit the index page, and the REST calls seem to work just fine.
UPDATE based upon feedback:
I don't know if this is a broken jar issue, but when I extract it the structure and files appear correct. I can run "gradle bootRun" from the top of my project structure just fine. But if I deploy the jar file and run it, I get an error:
Task 'bootRun' not found in root project 'edm'
Here is my build.gradle file in case there might be an issue in it:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'maven'
idea {
project {
//if you want to set specific jdk and language level
jdkName = '1.8'
languageLevel = '1.8'
}
}
jacoco {
toolVersion = "0.7.0.201403182114"
}
project.ext {
springBootVersion = '1.2.1.RELEASE'
}
configurations {
querydslapt
}
buildscript {
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url "http://repo.spring.io/libs-milestone" }
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
}
}
jar {
baseName = 'edm'
version = '0.1.0'
}
dependencies {
querydslapt group: 'com.mysema.querydsl', name: 'querydsl-jpa', version: '2.8.0', classifier: 'apt-one-jar', transitive: false
compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
compile("org.springframework.security:spring-security-web:4.0.0.M1")
compile("org.springframework.security:spring-security-config:4.0.0.M1")
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.1.RELEASE')
compile('com.android.tools.build:gradle:1.0.1')
compile("org.hibernate:hibernate-core:4.3.4.Final")
compile("org.hibernate:hibernate-entitymanager:4.3.4.Final")
compile("org.hibernate:hibernate-validator")
compile("org.apache.velocity:velocity:1.7")
compile('javax.mail:mail:1.4.1')
compile("org.springframework:spring-context-support")
compile("com.h2database:h2:1.3.172")
compile("joda-time:joda-time:2.3")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.5.0")
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')
compile("postgresql:postgresql:9.1-901.jdbc4")
testCompile('org.spockframework:spock-core:1.0-groovy-2.0-SNAPSHOT') {
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
}
testCompile('org.spockframework:spock-spring:1.0-groovy-2.0-SNAPSHOT') {
exclude group: 'org.spockframework', module: 'spock-core'
exclude group: 'org.spockframework', module: 'spring-beans'
exclude group: 'org.spockframework', module: 'spring-test'
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
}
testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
testCompile("junit:junit")
// for the anontation processor
querydslapt group: 'com.mysema.querydsl', name: 'querydsl-jpa', version: '2.8.0', classifier: 'apt-one-jar', transitive: false
// for compiling
compile("com.mysema.querydsl:querydsl-jpa:3.3.3")
compile("com.mysema.querydsl:querydsl-apt:3.3.3")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.0'
}
project.configurations {
providedRuntime
}
project.bootRepackage {
enabled = true
}
I also tried adding some thymeleaf configuration (even though I am not explicitly using Thymeleaf) to my application.properties file:
liquibase.changeLog=classpath:/db/changelog/db.changelog-master.xml
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template-location=true
So I guess my question is, why does running the "java -jar" not let me view the webpages?
It turns out that my REST controllers were proceeded by "/" so login page became "//loginPage" and that was causing the problem. I found the answer here but it took me a while to realize that was the issue. Hopefully this answer will help out someone in the future as it as a bear to figure out.

Resources