how to alias a task in buildr - java

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.

Related

How do I make an alias for a Gradle task?

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.

Are there any ways to run an arbitrary main method via Gradle?

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')
}

Adding custom command to gradle script

I'm using the shadowJar plugin for Gradle (4.2.1) to build the so-called fatJar or uberJar. This works as expected but I would like to to add some actions after the building is done. More precisely, I'd like to make the resulting file executable (in Unix terms, i.e. chmod +x) and then copy it to a certain directory. I've googled a lot and I know that both task are possible. I'd like to know whether I should write a script (task) that runs shadowJar and then does what I want, or I should change the shadowJar itself to embed the operations I need.
I think a good criteria to decide about such situation is to ask yourself if the new features are really part of the responsibility of the shoadowJar. If the answer is no, then it would be better to (as you mentioned) have another task that runs on top of that. By doing so you can reuse the shadowJar in much more different scenarios by combining it to other tasks. If you define the new one to be dependent on the shadowJar, then you would be sure that you can still call shadowJar task individually while calling the new task would always trigger the shadowJar. Your new task would be something like this:
task afterShadowJar (dependesOn: 'shadowJar') {
// manipulate file permissions, etc.
}

File dependency Gradle

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.

How to explode a war file in buildr

I'm new to buildr, so I might be missing something obvious.
According to this page, it should be fairly simple:
Download explode.rb
At the top of your buildfile, add the following:
require 'explode.rb'
Define explode task on your web application:
package(:war).explode :target => "jetty/webapps/myApplication"
Run the task
buildr myApp:explode
However, but I can't get it to work. If I just go through the steps in that article, I get the following error:
RuntimeError : Don't know how to build task 'myApp:explode'
I tried all sorts of combinations, but nothing worked.
Turns out the instructions were 100% correct. My problem was that myApp was a sub-project, and I needed the fully qualified name when running the task, i.e.:
buildr myProject:myApp:explode
I was confused because just running buildr myApp worked fine.

Categories

Resources