I realize when I use IntelliJ IDEA CE and make a Gradle-Java project.
When I run a Class.main(), say Hoge.main(),
the run window shows like
0:15:03 PM: Executing task 'Hoge.main()'...
and in gradle tasks list
:Hoge.main()
like this (the bottom line).
How can I do this in a terminal (not using IDEA)? If possible, it may be like this? (I know about https://docs.gradle.org/current/userguide/application_plugin.html, which is not flexible for my purpose)
gradle run Java Hoge
Is this only in IDEA?
I saw some ideas using the application plugin but I could not find very simple way like above...
That's what the gradle wrapper is for. If you have the wrapper (in [ProjectDir]/gradle/wrapper folder), then you can use Gradle without an IDE.
A) In Windows, execute the gradlew.bat script;
B) In Unix, execute the gradlew script.
In your case, you would type in the terminal following:
gradlew run or ./gradlew run.
P.S.
If you want to have several "run" tasks, you need to create them like so:
task runHoge(type: JavaExec,group: 'application'){
classpath(sourceSets.main.runtimeClasspath)
// set the main class name here
setMain('package.Hoge')
}
Related
I'm currently building a Android application, and I would like to do some processing after the apk is built.
I've already succeeded to launch a gradle task after the build:
tasks.whenTaskAdded {
task ->
if(task.name == 'assembleRelease'){
task.finalizedBy postApkProcess
}
}
task postApkProcess{
doLast {
println 'OK'
}
}
But I struggle to launch a method inside that task. What I would like to do is to call something like new MyClass().postBuild() inside the class (or if it is not possible, run the main method of a Java class), but I don't find a way to do it.
I've tried to build a task task postProcess(type: JavaExec), but the line apply plugin: 'java'conflicts with the Android plugin.
Is there a way to do it ?
You could use commandLine to run the main method in MyClass which could call postBuild:
commandLine 'java' '<pathToSrc>/<mypackage>.MyClass'
Be careful to set the package name and source location properly. The path should point to the location (minus package directory tree) of the MyClass.class file, not MyClass.java. If your class is not compiled or it changes often, you could call javac to compile it each time before you run it (optionally using a shell script / batch file) to bundle both commands and simplify the gradle call and help you test the setup directly.
If your class uses code from libraries ensure you add them to the classpath:
'-classpath' 'mylib1.jar:mylib2.jar'
If you use java in the build process you might also have a look at scar
I need to tune the JVM memory by help of Gradle. I've writen settings in the gradle.properties file:
org.gradle.jvmargs=-XX:MaxMetaspaceSize=60M
but it didn't work. Then I've done it manually in an IDEA (Run>Edit Configurations...) and wrote the settings for the needed class (in VM options: field) and it works. Can we do the same by help of Gradle?
I've tried to do:
org.gradle.jvmargs=-XX:MaxMetaspaceSize=60M test1.Test
but I got the error:
Error: Could not find or load main class test1.Test
When you run your application via IDEa, it runs the application using java ... command and gradle is not involed at all, so whatever you put in build.gradle doesn't matter.
If you want to run your app via gradle, consider using application plugin, which allows you to set jvm args. If you use Spring Boot use can use bootRun task and specify params in jvmArgs property.
As a part of a TDD workflow, I want to be able to check if my Java codebase compiles, but not if the tests pass.
Currently, if I run gradle build it runs the compile tasks (for source and tests) and then also executes the test task (and returns a non-zero exit code since the tests fail).
So I find that I have to run gradle build -x test to exclude the test task, and get a successful zero exit code.
What do I add to my build.gradle to define a new task, say compile that is an alias for build x test?
So far I have this, but it doesn't seem like dependsOn takes any arguments to customize the build task I want to execute:
task compile {
dependsOn build
}
I've been reading the docs here, I see different kinds of dependency chaining mechanisms, but not to disable/exclude a particular task. How does the -x flag even work then? I assumed there would be a way to control it programmatically too.
Thanks to Bjørn Vester's answer and reading the docs, I have implemented my task as follows:
task compile {
dependsOn classes
dependsOn testClasses
}
There are lots of different tasks you can run individually. For instance:
gradle classes: Will compile your "main" code.
gradle testClasses: Will compile your "main" code as well as test code.
gradle jar: Will compile your "main" code and assemble it into a jar.
None of the above will run your unit tests. On the other hand, the build task depends on all of the above, as well as the test task and more.
In general, if you like to run a particular set of tasks, you do that by defining a new task and then make dependencies to those other tasks you like to run with it. You tried that already, but instead of build you should have used something like compileJava or classes whatever other tasks you need. But always check if there isn't one already that satisfies your needs, like there are in this case. You can read about what tasks are available in Java projects in the documentation for the Gradle java plugin.
I am a gmake user transitioning to Gradle. I have a multi-project structure, where one sub-project is a Java project and the other a home-brewed language. The home-brewed language does not use any Gradle plugins. Now I want to add a task that runs a Java program to generate XML when any of my home-brewed source files have been modified. In make, I would just declare a dependency on inputFile.mine or *.mine next to the target name, but I could not easily find how to do this basic thing with Gradle. Currently, I force the task to always execute using the potentially ugly work-around below. I want to replace this with some dependsOn *.mine . The Gradle user guide has a whole chapter dedicated to explaining different ways of specifying files, but I did not see how to declare a dependency.
task generateXML(type: Exec) {
generateXML.getOutputs().upToDateWhen({false}) // Force it to execute always
executable("java.exe")
args("-jar", "resources/generateXml.jar", "src/inputFile.mine")
}
Thanks for helping a newbie out.
You can define task inputs and outputs in Gradle.
For example:
task generateXML(type: Exec) {
inputs.file ("src/inputFile.mine")
executable("java.exe")
args("-jar", "resources/generateXml.jar", "src/inputFile.mine")
}
See https://docs.gradle.org/current/userguide/more_about_tasks.html and https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskInputs.html for more information.
Side note: When you run your build with -i, Gradle will tell you what has happened during the up-to-date check.
I might be doing something wrong, cuz I'm using buildr for not so long, so all comments are welcome.
My project structure is:
define :proj do
define :web do
task :run do
# runs the web part of the project in a jetty
end
end
end
now if I want to start my project I have to type
buildr proj:web:run
I'd like to type simply
buildr run
instead. How do I achieve that?
At the top level of your buildfile (i.e., outside of any defines), add
task :run => 'proj:web:run'
This defines a task named run whose sole prerequisite is the proj:web:run task.
You can also make the task a 'local task',
Project.local_task 'run'
which means that whenever you are inside the web directory, typing buildr run will look for a locally-scoped that of that name.
Note that Buildr 1.4.3 added a standard run task so you typically wouldn't need to make run a local task; see http://buildr.apache.org/more_stuff.html#run for details.