I am trying to build a gradle project that has following task:
task avro {
doLast {
...
javaexec {
classpath configurations.config
setMain "class";
args = ["--input", inputDir,
"--output", srcDir]
jvmArgs "-Xmx128m"
}
}
}
The build fails with
Caused by: java.io.IOException: error=7, Argument list too long
We have a plugin to create pathing jar that exposes a method (new to gradle, so please excuse mistake in terminologies).
pathingJar.enableFor(taskName)
However, I am not able to apply the pathingJar for this task as it says
enableFor() is applicable for argument types: (org.gradle.api.DefaultTask_Decorated) values: [task 'avro']
Related
in our project we are working with
springBoot 2.2.11
groovy 2.5.6
Our build.gradle looks like the following:
...
plugins {
id 'groovy'
id 'java'
id 'idea'
}
configurations {
testCompile.extendsFrom compile
testRuntime.extendsFrom runtime
...
}
...
sourceSets {
test {
java { srcDirs = ['src/test/java'] }
groovy {srcDirs = ['src/test/groovy'] }
}
...
}
...
springBoot {
buildInfo()
}
...
When we run gradlew test --info we get the following output:
...
> Task :bootBuildInfo
Caching disabled for task ':bootBuildInfo' because:
Build cache is disabled
Task ':bootBuildInfo' is not up-to-date because:
Value of input property 'properties.time' has changed for task ':bootBuildInfo'
...
> Task :compileJava UP-TO-DATE
...
Skipping task ':compileJava' as it is up-to-date.
...
> Task :compileTestGroovy
Caching disabled for task ':compileTestGroovy' because:
Build cache is disabled
Task ':compileTestGroovy' is not up-to-date because:
Input property 'astTransformationClasspath' file C:\projects\test-project\build\resources\main\META-INF\build-info.properties has changed.
The input changes require a full rebuild for incremental task ':compileTestGroovy'.
...
The build-info.properties file which is generated by spring-boots buildInfo step contains a build.time property which is updated on every execution.
The Java Compiler excludes this file apperently as the up-to-date check for :compileJava is returning true. However, the compileGroovy tasks includes this file into his sourceSet which is why it returns false on the up-to-date check.
I already tried the exclude option as follows with no success.
sourceSets {
test {
java { srcDirs = ['src/test/java'] }
groovy {
srcDirs = ['src/test/groovy']
excludes = [file("${buildDir}/resources/main/META-INF/build-info.properties")]
}
}
}
Any ideas on how to fix this? Idealy I would like to exclude the file build\resources\main\META-INF\build-info.properties from the up-to-date-check (or even the input sourceSet of compileGroovy).
You need to make spring-boot.properties generated with no time part(what makes springBoot task out of dated what in turn leads compileJava outdated)
springBoot {
buildInfo {
properties {
// to make compileJava up to date
// https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#integrating-with-actuator.build-info
time = null
}
}
}
I want to enlarge my java heap size.
using intellij on mac.
I have tried:
task BL_generate_dummy(type: JavaExec) {
JavaExec.setMaxHeapSize("4096")
classpath sourceSets.main.runtimeClasspath
main = "com.m.runners.BaselineGeneratorRunner"
}
and got this error:
* What went wrong:
A problem occurred evaluating root project 'RoutingRegression'.
> No signature of method: static org.gradle.api.tasks.JavaExec.setMaxHeapSize() is applicable for argument types: (java.lang.String) values: [4096]
Possible solutions: setMaxHeapSize(java.lang.String), getMaxHeapSize(), setMinHeapSize(java.lang.String), getMinHeapSize()
and also
JavaExec.setMaxHeapSize(4096)
JavaExec.maxHeapSize = 4096
what is the right syntax?
In general if I want to set a Java flag (not to main, but to running process arg). How do i set it in a gradle task?
task BL_generate_dummy(type: JavaExec) {
add '-Xmx4096m -Xms4096m' to the java flags
Think the JavaExec.set is superfluous.
Do
task BL_generate_dummy(type: JavaExec) {
maxHeapSize = "4096"
classpath sourceSets.main.runtimeClasspath
main = "com.m.runners.BaselineGeneratorRunner"
}
Btw in the current Gradle version (7.x) this will yield some deprecation warning . With Kotlin, change the mainClass from
main = "com.m.runners.BaselineGeneratorRunner"
to
mainClass.set("com.m.runners.BaselineGeneratorRunner")
I try to pass arguments from command line to gradle.
My build.gradle is:
task execute(type:JavaExec) {
main = mainClass
classpath = sourceSets.main.output
}
When I do:
gradle -PmainClass=Hello execute
I get this:
FAILURE: Build failed with an exception.
* Where:
Build file '/home/example2/build.gradle' line: 7
* What went wrong:
A problem occurred evaluating root project 'example2'.
> Could not find property 'sourceSets' on task ':execute'.
My question is that what should I provide for main? name of any java file package? any specific path? What should actually be sourceSet?
Your minimal build.gradle file should look like this:
apply plugin: 'java'
task execute(type:JavaExec) {
main = mainClass
classpath = sourceSets.main.output
}
And you need a java class located in src/main/java/Hello.java like this:
public class Hello {
public static void main(String[] a) {}
}
I have to pass two arguments to my main method. My build script is
// Apply the java plugin to add support for Java
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'maven central' for resolving your dependencies.
mavenCentral()
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'com.example:example-core:1.7.6'
}
task main(type: JavaExec, dependsOn: classes) {
description = 'This task will start the main class of the example project'
group = 'Example'
main = 'com.example.core.Example'
classpath = sourceSets.main.runtimeClasspath
}
If I try:
gradlew main doc.json text.txt
Then an error occured.
org.gradle.execution.TaskSelectionException: Task 'doc.json' not found in root project
How can I pass arguments to my main method command line easily?
task run(type: JavaExec) {
main = "pkg.MainClass"
classpath = sourceSets.main.runtimeClasspath
args = ["arg1", "arg2"]
}
You should use use -P as listed in the Gradle command line documentation.
For example, the following will work:
gradlew main -Parg1=doc.json --project-prop arg2=text.txt
And you access them in your Gradle script like this:
println "$arg1 $arg2"
task run1(type: JavaExec) {
main = "pkg.mainclass"
classpath = sourceSets.main.runtimeClasspath
args = ["$arg1","$arg2",...]
}
//I have named as run1 it can be any task name
While invoking the gradle script:
c:\> gradle run1 -Parg1="test123" -Parg2="sss"
I need to execute a java class which has a main method in it before compiling the code. This is what I have tried so far:
task runSimple(type: JavaExec) {
main = 'jjrom.ObjectGen'
classpath = sourceSets.main.runtimeClasspath
File prop1 = file(propFilePath)
args '-sqlserver', '-force', prop1.path
println "I'm done executing."
}
compileJava {
dependsOn runSimple
}
When I execute this script with the command "gradle compileJava" , I get this error message:
I'm done executing.
FAILURE: Build failed with an exception.
What went wrong: Circular dependency between the following task: :classes --- :compileJava --- :runSimple --- :classes (*)
If you need to execute this class before compiling the code, you can't give it classpath = sourceSets.main.runtimeClasspath. The latter includes the compiled code, and so Gradle automatically infers runSimple.dependsOn compileJava, which together with your compileJava.dependsOn runSimple gives a cyclic task dependency. (To be precise, Gradle infers runSimple.dependsOn classes, which in turn depends on compileJava.)
If you need to run JavaExec only with dependecies classpath, just change classpath variable to something like:
classpath = configurations.compile
Or if you are interested in very specific classpath, you could add custom configuration like this:
configurations {
customClasspath
}
dependencies {
customClasspath files('path/to/your.jar')
}
task runSimple(type: JavaExec) {
main = 'jjrom.ObjectGen'
classpath = configurations.customClasspath
File prop1 = file(propFilePath)
args '-sqlserver', '-force', prop1.path
println "I'm done executing."
}
compileJava {
dependsOn runSimple
}