In our Groovy scripts we can use the @Grab
annotation. With this annotation we define dependencies for our script and they will be automatically downloaded and added to the class path when we run our script. When we use IntelliJ IDEA we can use a nice intention that is part of the IntelliJ IDEA Groovy support to download the dependencies from our IDE editor. IDEA downloads the dependencies and add it to the project module dependencies. This is useful, because this will also adds code completion for the classes in the dependency to our editor.
Category Archives: IDE
Run one or Exclude one test with Maven
From time to time you only want to run one test, one test method, one class or one package from the command-line.
Or on the contrary: you want to exclude / ignore one specific test or group of tests during the build cycle.
Excluding tests from the build cycle by the command line usually occurs when the following scenarios meet:
- A test requires significant amount of resources (time, memory, disk space, etc.)
- The run needs to be independent from the IDE (reenact the Continuous Integration / Continuous Delivery pipeline) as some IDEs load test-dependencies on the compile-time class-path.
- You have no or limited ability to change the code-base
Continue reading
Gradle Goodness: Create Shortcut Key To Refresh Gradle Projects In IntellIJ IDEA
We can open a Gradle project in IntelliJ IDEA and get support for Gradle inside IntelliJ. Sometimes we need to refresh the project in IntelliJ IDEA, for example when we add a new dependency or plugin in our Gradle build file. We need to refresh the Gradle project so IntelliJ IDEA can work with the changes. The Gradle tool window has an icon to Refresh all Gradle projects. But this means a mouse action and we want to have a shortcut key so we can leave our hands on the keyboard.
The action Refresh all Gradle projects is actually the action Refresh all external projects. We can add keyboard shortcut key via Preferences | Keymap. We use the search box to search for Refresh all external projects
.
We can right click on the found action and select Add Keyboard Shortcut to define a new shortcut key:
Now we simply use the shortcut to refresh our Gradle project when we have made a change in the Gradle build file that IntelliJ IDEA should know about.
Besides have the action in the Gradle tool window we can also add it to the main toolbar. We right click on the main toolbar and select the option Customize Menus and Toolbars…. We can add the action Refresh all external projects here to the toolbar:
Written with Gradle 3.4.1 and IntelliJ IDEA 2016.3.4.
Gradle Goodness: Passing Environment Variable Via Delegate Run Action In IntelliJ IDEA
IntelliJ IDEA 2016.3 introduced the option to delegate the run action to Gradle. This means when we have a run Configuration for our Java or Groovy classes we can use the Run action and IDEA will use Gradle to run the application. Actually IntelliJ IDEA creates a new task of type JavaExec
dynamically for our specific run configuration with the main
property set to the class we want to run.
In the Edit Configuration dialog window we can set the command line argument and Java system properties. These are passed on to the dynamically created JavaExec
task and are accessible from within the class that runs. The environment variables that can be set in the Edit Configuration dialog windows are not passed to the JavaExec
task configuration. But we can do it ourselves in the build script file of our project. We look for the dynamically created task and use the environment
method to add a environment variable that can be access in the Java or Groovy class that is executed.
We start our example with a simple Groovy class that can be executed using JavaExec
. We simply print out the given input arguments, all Java system properties that start with sampleApp
and finally all environment variables that start with SAMPLE_APP
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// File: src/main/groovy/mrhaki/gradle/SampleApp.groovy package mrhaki.gradle class SampleApp { static void main(final String[] args) { final app = new SampleApp() app.printArgs(args) app.printSystemProperties() app.printEnv() } void printArgs(final String[] args) { printSection 'Command line arguments', args } void printSystemProperties() { printSection 'System properties', System.properties.findAll(findNameStartsWith('sampleApp')) } void printEnv() { printSection 'environment', System.getenv().findAll(findNameStartsWith('SAMPLE_APP')) } private void printSection(final String title, final values) { println "--- $title ---" println values } private Closure findNameStartsWith(final String search) { { name, value -> name.startsWith(search) } } } |
We create a new Run/Debug Configuration for our SampleApp
class:
When we click on OK to save the configuration we are ready to use the Run ‘SampleApp’ action. When we look at the output we get the following result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
10:54:29: Executing external task 'run SampleApp'... :compileJava UP-TO-DATE :compileGroovy :processResources UP-TO-DATE :classes :run SampleApp --- Command line arguments --- [Execute via IDEA delegate to Gradle] --- System properties --- [sampleApp:Gradle Galore] --- environment --- [:] BUILD SUCCESSFUL Total time: 3.539 secs 10:54:33: External task execution finished 'run SampleApp'. |
Notice the Gradle tasks that are invoked and the dynamically created run SampleApp
task. We see our Java system property is passed on, together with the program arguments. The environment variable is not passed on. We must add some extra configuration to the dynamically created task run Sample
in our build.gradle
file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// File: build.gradle ... setEnvVarRunConfiguration 'SampleApp', 'SAMPLE_APP', 'Gradle environment variable' /** * Add environment variable to JavaExec task * configuration created by IntelliJ IDEA * from a Run/Debug configuration. * * @param configuration Name of IntelliJ IDEA Run/Debug configuration * @param envName Name of environment variable * @param envValue Value for environment variable */ def setEnvVarRunConfiguration( final String configuration, final String envName, final String envValue) { // Find task by type JavaExec and matching // task name for configuration name. final javaExecRunConfiguration = { task -> task instanceof JavaExec && task.name == "run $configuration" } tasks.matching(javaExecRunConfiguration).all { // Add environment variable to JavaExec // task configuration. environment envName, envValue } } ... |
Now we re-run our application and we get the following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
11:29:19: Executing external task 'run SampleApp'... :compileJava UP-TO-DATE :compileGroovy UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :run SampleApp --- Command line arguments --- [Execute via IDEA delegate to Gradle] --- System properties --- [sampleApp:Gradle Galore] --- environment --- [SAMPLE_APP:Gradle environment variable] BUILD SUCCESSFUL Total time: 0.599 secs 11:29:19: External task execution finished 'run SampleApp'. |
Written with IntelliJ IDEA 2016.3 and Gradle 3.2.1.
IntelliJ IDEA: 2016.2 font ligatures
The new version IntelliJ IDEA 2016.2 is out! It includes a new feature to support font ligatures. It looks very promising, so I downloaded and installed the font FiraCode. But I noticed rough edges in the font.
Groovy Goodness: IntelliJ IDEA Formatting Of Closure Chains
When we write code with a lot of method chaining that involves closures and the use the Reformat code feature of IntelliJ IDEA things might get screwed up. Luckily we can changes some Groovy formatting rules in IntelliJ IDEA to prevent the reformatting.
IntelliJ IDEA: How Productive Was Your Day?
If we want to see how productive we are with IntelliJ IDEA we must open Help | Productivity Guide. IntelliJ IDEA opens a dialog window with a very detailed overview of smart stuff we have been using. And how much typing we have saved.
Written with IntelliJ IDEA 2016.1.
Gradle Goodness: Add Spring Facet To IntelliJ IDEA Module
To create IntelliJ IDEA project files with Gradle we first need to apply the idea
plugin. We can then further customise the created files. In this blog post we will add a Spring facet to the generated module file. By adding the Spring facet IntelliJ IDEA can automatically search for Spring configuration files. We can then use the Spring view to see which beans are configured in our project.
Gradle Goodness: Set VCS For IntelliJ IDEA In Build File
When we use the IDEA plugin in Gradle we can generate IntelliJ IDEA project files. We can customise the generated files in different ways. One of them is using a simple DSL to configure certain parts of the project file. With the DSL it is easy to set the version control system (VCS) used in our project.
In the next example build file we customise the generated IDEA project file and set Git as the version control system. The property is still incubating, but we can use it to have a proper configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// File: build.gradle apply plugin: 'groovy' apply plugin: 'idea' idea { project { // Set the version control system // to Git for this project. // All values IntelliJ IDEA supports // can be used. E.g. Subversion, Mercurial. vcs = 'Git' } } |
Written with Gradle 2.12 and IntelliJ IDEA 15.
Gradle Goodness: Configure IntelliJ IDEA To Use Gradle As Testrunner
When we run tests in IntelliJ IDEA the code is compiled by IntelliJ IDEA and the JUnit test runner is used. We get a nice graphical overview of the tasks that are executed and their results. If we use Gradle as the build tool for our project we can tell IntelliJ IDEA to always use Gradle for running tests.