A finishing project
Every piece of this course meets in one project: a small library with tests, an application plugin that runs it, a jar with a manifest, and a command for each stage. It is the project to copy when the first build of your own starts.
The project
settings.gradle
rootProject.name = 'wordcount'
build.gradle
plugins {
id 'java'
id 'application'
}
group = 'com.example'
version = '1.0.0'
repositories {
mavenLocal()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.14.3')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
tasks.withType(JavaCompile).configureEach {
options.compilerArgs += ['-Xlint:all']
}
tasks.named('test') {
useJUnitPlatform()
testLogging {
events 'passed', 'failed'
}
}
application {
mainClass = 'com.example.App'
}
jar {
manifest {
attributes 'Main-Class': 'com.example.App'
}
}
src/main/java/com/example/WordCount.java
package com.example;
public final class WordCount {
public static int words(String text) {
return text.isBlank() ? 0 : text.trim().split("\\s+").length;
}
public static int lines(String text) {
return text.isEmpty() ? 0 : text.split("\n", -1).length;
}
public static String longest(String text) {
String best = "";
for (String word : text.split("\\s+")) {
if (word.length() > best.length()) {
best = word;
}
}
return best;
}
}
src/main/java/com/example/App.java
package com.example;
public final class App {
public static void main(String[] args) {
String text = "A build tool turns sources into an artefact";
System.out.println("words: " + WordCount.words(text));
System.out.println("lines: " + WordCount.lines(text));
System.out.println("longest: " + WordCount.longest(text));
}
}
src/test/java/com/example/WordCountTest.java
package com.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class WordCountTest {
@Test
void countsWords() {
assertEquals(3, WordCount.words("one two three"));
}
@Test
void countsNothingInBlankText() {
assertEquals(0, WordCount.words(" "));
}
@Test
void findsTheLongestWord() {
assertEquals("artefact", WordCount.longest("a build tool turns sources into an artefact"));
}
}
gradle test
gradle test --console=plain --offline --no-daemon
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
WordCountTest > countsWords() PASSED
WordCountTest > countsNothingInBlankText() PASSED
WordCountTest > findsTheLongestWord() PASSED
BUILD SUCCESSFUL in 18s
3 actionable tasks: 3 executed
gradle run
gradle run --console=plain --offline --no-daemon
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :run
words: 8
lines: 1
longest: artefact
BUILD SUCCESSFUL in 12s
2 actionable tasks: 1 executed, 1 up-to-date
The archive
gradle build --console=plain --offline --no-daemon
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar
> Task :startScripts
> Task :distTar
> Task :distZip
> Task :assemble
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test UP-TO-DATE
> Task :check UP-TO-DATE
> Task :build
BUILD SUCCESSFUL in 16s
7 actionable tasks: 4 executed, 3 up-to-date
java -jar build/libs/wordcount-1.0.0.jar
words: 8
lines: 1
longest: artefact
The four commands of a project: test it, run it, build it, ship the jar. Everything else in this course is a detail of one of them, and the hub has the list of chapters if one of them needs a second reading.
Related
- JUnit 5 with useJUnitPlatform(): the test setup this project uses.
- The jar and its manifest: the attribute that makes java -jar work.
- A finishing project in Maven: the same library, built by the other tool.
Article author: Arthur Isaev