Exercise 1.1: Fixing the Maven Build Warning
1 Fixing the Maven Build Warning
Welcome to your first programming exercise! The goal of this task is to address a common warning you might encounter with Maven projects, ensuring your project builds consistently across different computers.
A Word on Reproducibility
A reproducible build is a build process that produces the exact same output every time, regardless of the environment it’s run in. This is a fundamental concept in software engineering, as it ensures that what works on your computer will also work for other developers and on a production server.
1.1 The Problem
When you run mvn compile in your terminal, you might see a warning message that looks like this:
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!This warning tells us that Maven is using your computer’s default character encoding to handle project files. While this works on your machine, it could cause problems if another developer tries to build the project on a different system with a different default encoding. The goal is to make our project build in the exact same way for everyone, which is known as a reproducible build.
1.2 The Solution
The best practice for modern Java projects is to explicitly set the character encoding to UTF-8. This is a universal standard that guarantees consistency.
To fix this, we need to explicitly tell Maven which character encoding to use for the entire project. The standard choice for modern Java development is UTF-8.
Open pom.xml
First, open the pom.xml file in the root of your project directory. This is the heart of any Maven project, as it contains all the configuration details.
Add the project.build.sourceEncoding property
Add or edit the <properties> section directly after the <version> tag in your pom.xml. Copy and paste the following <project.build.sourceEncoding/> property, making sure to replace the existing <project.build.sourceEncoding/> property if one is already present.
<project xmlns="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)"
xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"
xsi:schemaLocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0) [http://maven.apache.org/xsd/maven-4.0.0.xsd](http://maven.apache.org/xsd/maven-4.0.0.xsd)">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.helloworld</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- Add this property to set the source encoding for the project. -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.0-M1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>Verify the Fix
Save the pom.xml file. Then, return to your terminal and run the following command:
mvn clean install
The clean command will remove all previously compiled files, ensuring you’re running a fresh build. The install command will recompile your project and install the new .jar file into your local Maven repository.
After this, you should see that the warning message is no longer present in the build output. You have successfully made your build platform-independent!
The mvn clean install command places a project’s compiled .jar file into your local Maven repository for use as a dependency in other projects. On a Linux or macOS system, this repository is located you your user’s home directory.
ls -l ~/.m2/repository/com/example/helloworld/hello-world/1.0-SNAPSHOT/hello-world-1.0-SNAPSHOT.jarOn Windows, you can use PowerShell, which maps the ls command as an alias to Get-ChildItem, and also includes the ~ alias to your home directory.
ls ~/.m2/repository/com/example/helloworld/hello-world/1.0-SNAPSHOT/hello-world-1.0-SNAPSHOT.jarNotice that the installed artifact uses the <version/> string from the pom.xml file.