Skip to content

Port build to Gradle#2

Open
marshallpierce wants to merge 1 commit intoblpercha:masterfrom
marshallpierce:mp/gradle
Open

Port build to Gradle#2
marshallpierce wants to merge 1 commit intoblpercha:masterfrom
marshallpierce:mp/gradle

Conversation

@marshallpierce
Copy link
Copy Markdown

  • Include support for building Kotlin w/ kotlinter code format checking
  • Include a starting point for a Clikt-based CLI (see https://ajalt.github.io/clikt/)

- Include support for building Kotlin w/ kotlinter code format checking
- Include a starting point for a Clikt-based CLI (see https://ajalt.github.io/clikt/)
Comment thread build.gradle.kts
@@ -0,0 +1,42 @@
plugins {
kotlin("jvm") version "1.4.0"
id("org.jmailen.kotlinter") version "3.0.2"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fail the build if your kotlin isn't formatted right, and you can use this to auto-fix it:

./gradlew formatKotlin

Comment thread build.gradle.kts
plugins {
kotlin("jvm") version "1.4.0"
id("org.jmailen.kotlinter") version "3.0.2"
id("com.github.johnrengelman.shadow") version "6.0.0"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

produces "fat jars" that have dependencies bundled in. This is a sort of regrettable way to package software, but it's probably the simplest for your users.

Comment thread build.gradle.kts
}

application {
mainClassName = "com.foo.unmls.UnmlsGraphTool"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is used by the shadow plugin when generating the "shaded" aka fat jar to know which class to invoke by default

Comment thread build.gradle.kts
@@ -0,0 +1,42 @@
plugins {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is actually itself kotlin (in script flavor, hence .kts instead of .kt)

@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these and the other gradle files are auto-generated

init {
subcommands(
Foo(),
Bar()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subcommands are easy

}

private class Foo : CliktCommand() {
private val widgetSize: Int by option().int().required()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will produce a --widget-size CLI param automatically (can customize the name, but by default it goes off of the property name)

private val widgetSize: Int by option().int().required()

override fun run() {
println("Size $widgetSize widget")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string interpolation

Comment thread README.md
The `umls-to-graph` code uses Gradle to handle dependencies (see the `build.gradle.kts` file for details). Run the code from Eclipse or IntelliJ, or compile a jar:

```
./gradlew assemble
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the default task that's set up for "do whatever tasks are necessary to produce the build artifacts that you might publish or otherwise use directly". The key task running under the hood is the shadowJar task, provided by the shadow plugin.


object UnmlsGraphTool {
@JvmStatic
fun main(args: Array<String>) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For iterative development you might set up an IDE "run configuration" that runs this class with your chosen parameters. Or, if you're CLI focused, you might:

./gradlew run --args='foo --widget-size 1'

Or if you don't want gradle in the way, but just want to work directly with the jar:

./gradlew shadowJar
java -jar build/libs/umls-to-graph-all.jar foo --widget-size=1

Or to get fancier...

In one terminal, run gradle in "watch mode". Whenever input files change, it will redo the necessary build steps to generate the shadow jar constantly, so it will keep running until you kill it. Also note that we are abbreviating shadowJar as shJ, and gradle is ok with that since shJ is an unambiguous abbreviation. c would not be, since it could be clean or check, but ch (for check, which runs tests) would be ok.

./gradlew shJ --continuous

In another:

java -jar build/libs/umls-to-graph-all.jar foo --widget-size=1

Comment thread build.gradle.kts
@@ -0,0 +1,42 @@
plugins {
kotlin("jvm") version "1.4.0"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the kotlin plugin applies the java plugin, so no need to do that separately. (Java and Kotlin source will both be compiled)

Comment thread build.gradle.kts
kotlin("jvm") version "1.4.0"
id("org.jmailen.kotlinter") version "3.0.2"
id("com.github.johnrengelman.shadow") version "6.0.0"
application
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configures tasks to produce output appropriate for an application, rather than a library (which we actually are disabling below since we want a shaded fat jar), and the run task to run the main() method of the class specified below

Comment thread build.gradle.kts
}

repositories {
jcenter()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where to get jars from

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant