A small library end to end
This is the last article of the course and the only one that is a whole project from its first file to its last command: a small library with three methods, four tests, a jar and an install. Every step of it is a step the earlier articles explain; the point of the page is to see them in one folder, in the order a project does them.
What the library does
wordcount counts the words and the lines of a text and finds the longest word. Three static methods and no state, which is enough for a body of code that has a real rule to test and no framework in the way.
The pom
The coordinates, the two properties the course has used since the properties article and the one test dependency, declared with test scope from the dependency article:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>wordcount</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
<junit.version>5.14.3</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The three methods
src/main/java/com/example/wordcount/WordCount.java
package com.example.wordcount;
import java.util.ArrayList;
import java.util.List;
public class WordCount {
public static List<String> words(String text) {
List<String> words = new ArrayList<>();
for (String word : text.split("\\s+")) {
if (!word.isEmpty()) {
words.add(word);
}
}
return words;
}
public static int lines(String text) {
if (text.isEmpty()) {
return 0;
}
return text.split("\n", -1).length;
}
public static String longest(String text) {
String longest = "";
for (String word : words(text)) {
if (word.length() > longest.length()) {
longest = word;
}
}
return longest;
}
}
text.split("\\s+") is one backslash for the regular expression and one for the Java string; the page shows the two the file has. lines uses split with a limit of -1, because the default drops the empty strings at the end and "a\nb\n" would count two lines instead of three.
The tests
src/test/java/com/example/wordcount/WordCountTest.java
package com.example.wordcount;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class WordCountTest {
@Test
void countsTheWordsOfALine() {
assertEquals(3, WordCount.words("one two three").size());
}
@Test
void ignoresTheExtraSpaces() {
assertEquals(2, WordCount.words(" one two ").size());
}
@Test
void countsTheLines() {
assertEquals(3, WordCount.lines("a\nb\nc"));
assertEquals(0, WordCount.lines(""));
}
@Test
void findsTheLongestWord() {
assertEquals("three", WordCount.longest("one two three"));
}
}
Four tests for three methods, because the second one is a rule of the first: the extra spaces are the case a split on a single space gets wrong. The text block of the language is not used, although Java 21 has it: a string with a real newline in it is easier to read in a page than a block with an opening triple quote.
mvn test, package, install
mvn -B test > test.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" test.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:wordcount >------------------------
[INFO] Building wordcount 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.wordcount.WordCountTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: <TIME> s -- in com.example.wordcount.WordCountTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
Four tests, four goals before surefire, one report. package runs the same and adds the jar:
mvn -B package > package.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" package.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:wordcount >------------------------
[INFO] Building wordcount 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO]
[INFO]
[INFO] --- jar:3.5.0:jar (default-jar) @ wordcount ---
[INFO] Building jar: <project>/target/wordcount-1.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
The last step is install, which is what makes the library usable by another project on this machine:
mvn -B install > install.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" install.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:wordcount >------------------------
[INFO] Building wordcount 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO]
[INFO] --- install:3.1.4:install (default-install) @ wordcount ---
[INFO] Installing <project>/pom.xml to <HOME>/.m2/repository/com/example/wordcount/1.0.0/wordcount-1.0.0.pom
[INFO] Installing <project>/target/wordcount-1.0.0.jar to <HOME>/.m2/repository/com/example/wordcount/1.0.0/wordcount-1.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
The jar and the program
jar tf target/wordcount-1.0.0.jar | sort
META-INF/
META-INF/MANIFEST.MF
META-INF/maven/
META-INF/maven/com.example/
META-INF/maven/com.example/wordcount/
META-INF/maven/com.example/wordcount/pom.properties
META-INF/maven/com.example/wordcount/pom.xml
com/
com/example/
com/example/wordcount/
com/example/wordcount/WordCount.class
Eleven entries: the two classes, the package folders, the manifest and the four META-INF/maven entries. The jar is run with the class name, because the manifest names no main class; the package article adds that line, and this project does not need it because it is a library.
A program that uses the library is one more class in the same project, and it runs from the same jar:
src/main/java/com/example/wordcount/Main.java
package com.example.wordcount;
public class Main {
public static void main(String[] args) {
String text = "one two three\nfour five\n";
System.out.println("words: " + WordCount.words(text).size());
System.out.println("lines: " + WordCount.lines(text));
System.out.println("longest: " + WordCount.longest(text));
}
}
mvn -B -q package; echo "status: $?"; java -cp target/wordcount-1.0.0.jar com.example.wordcount.Main
status: 0
words: 5
lines: 3
longest: three
Five words, three lines (the last one empty, which is the case the test covers) and three as the longest. The program runs from the jar and not from the classes folder, so the jar is proved to hold what the program needs; the test dependency is not in it, as the scope article says it should not be.
The course in one page
Everything this folder taught, in the order a project meets it:
- A pom, four coordinates and a layout are the whole of a project.
- Phases run in order, from compile through test to package and install, and the switches of the command line change which of them run.
- Dependencies come with their own dependencies, their versions are decided by nearest wins and settled by dependencyManagement, and every file of them is in the local repository.
- Plugins do the work, the compiler and resources ones included, and surefire reports on the tests.
- A parent pom shares the versions of a reactor, its modules can be used by another project, a snapshot is the version of a branch and a profile the version of an environment.
- Around the build: settings and mirrors, the wrapper and a fat jar, CI and a Docker image, the IDE and the errors.
The hub lists every page of the course with one line each, and the Java hub and the JUnit hub have the rest of the language the builds compile.
Article author: Arthur Isaev