Heart containing Coding Chica Java 101

Hello World! – A Java Application from a Simple Maven Project Archetype

I like to use Maven to build my Java applications, so let’s start there.

Parent Folder Structure

Create the folder structure where your new Java / Maven project will be housed and change directory to move into that parent folder.

TIP: References Quick List

Additional Info:

# Run the following in a terminal / command prompt
# Go to the desired directory - In this example, we are starting with the home directory for the current user.
# Mac / Linux - 
cd ~
# Windows
cd %HOMEPATH%

# Create a new directory (if you have not already done so
mkdir developmentProjects

# Move into the directory you just created
cd developmentProjects

Create a new Simple Maven Project

Create a new Simple Maven Project. A Maven archetype is a template which can be used to create a new Java project.

# Create a new sub-directory for the new Java project with a Maven pom.xml file to control the build.
mvn archetype:generate -DarchetypeGroupId="org.apache.maven.archetypes" -DarchetypeArtifactId="maven-archetype-quickstart" -DarchetypeVersion="1.4"

# This will generate a new simple Maven project using interactive mode.  
# Fill in the prompts as described below. 
Prompt ForSuggested ValueComments
groupId<yourLastNameHere>.<yourFirstNameHere>Often, this will be the reverse of the company’s website.
For example, a site of mysite.com might use a groupId = com.mysite

Used in artifacts generated / uploaded by this build, if any, in the future.
artifactIdjava101This is a very simple description of the project being created.

Used in artifacts generated / uploaded by this build, if any, in the future.

This will also be the sub-folder name generated to house the project.
version0.1-SNAPSHOTIn Maven, versioning is used to distinguish different artifact versions as you make changes to an existing project.

It uses format:
<MajorVersion>.<MinorVersion>-SNAPSHOT

The value of 0.1-SNAPSHOT indicates that we are still developing the first release.

When you are ready to release an official version, you could change to 1.0-SNAPSHOT.
package<yourLastNameHere>.<yourFirstNameHere>.java101.helloThis will be the nested folder structure generated within the src/main/java folder, except dots (.) are used to separate parent / child folders.

At the end of the process, you should see output like:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  54.084 s
[INFO] Finished at: 2023-06-16T13:18:36-05:00
[INFO] ------------------------------------------------------------------------

Your First Maven Build

A sub-folder matching the artifactId value from above should be generated. Change into that new sub-directory and perform your first Maven build.

cd java101

# You don't actually need the clean here, but providing in case this same command is reused for later.  
# The clean deletes the java101/target directory, where the build occurs.
# The install command performs the build and installs the generated artifact into your local Maven repository cache.
mvn clean install

You should see output similar to the following:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< codingchica:java101 >-------------------------
[INFO] Building java101 0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
...
[INFO] --- clean:3.1.0:clean (default-clean) @ java101 ---
...
[INFO] --- resources:3.0.2:resources (default-resources) @ java101 ---
...
[INFO] --- compiler:3.8.0:compile (default-compile) @ java101 ---
...
[INFO] --- resources:3.0.2:testResources (default-testResources) @ java101 ---
...
[INFO] --- compiler:3.8.0:testCompile (default-testCompile) @ java101 ---
...
[INFO] --- surefire:2.22.1:test (default-test) @ java101 ---
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running codingchica.java101.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in codingchica.java101.AppTest
...
[INFO] --- jar:3.0.2:jar (default-jar) @ java101 ---
...
[INFO] --- install:2.5.2:install (default-install) @ java101 ---
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.225 s
[INFO] Finished at: 2023-06-16T13:30:14-05:00
[INFO] ------------------------------------------------------------------------

Open the new Java / Maven project in your IDE

  1. Open IntelliJ application
  2. Open the new java101 project via File menu -> Open -> select the new java101 project -> Click OK

Change to the desired Java version

We will need to update the Java version in a couple of places.

Update Maven’s JDK

Assuming you downloaded Java 17 in prior local setup steps, Modify the pom.xml to point to that version.

<!-- Update the existing values in the properties section of the pom.xml -->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>

If a m2 popup appears, you can click it to re

Update IntelliJ’s JDK

File Menu -> Project Structure

  1. Platform Settings -> SDKs -> + -> Select your desired JDK.
  2. Project Settings -> Project -> SDK -> Select the desired JDK.

Make Sure Java is on your path

Run the following command from inside of your java101 project’s directory to make sure Java is available on the path:

java -version

# You should see output similar to the following (without the # prefix)
# although the exact details will vary.
#
# java version "17.0.6" 2023-01-17 LTS
# Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
# Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing) 

Run the new Hello World application

# Run a command like:
# java --class-path target/<artifactId>-<version>.jar <package>.App
# For example:
java -cp target/java101-0.1-SNAPSHOT.jar codingchica.java101.App    

You should see the output of:

Hello World!

Party! You just built and ran your very own Java project.

pom.xml Review

The pom.xml is what allows you to configure the Maven build. It is in the root of a given project. This is a simple, single module Maven project, so there is only one pom.xml. However, you can have child modules/projects.

As the file extension (.xml) suggests, the pom.xml file is an XML-based data file.

Archetype-quickstart variables

Data provided earlier during the maven-archetype-quickstart execution will show up here:

  <groupId>codingchica</groupId>
  <artifactId>java101</artifactId>
  <version>0.1-SNAPSHOT</version>

  <name>java101</name>

Properties Section

The properties section defines variables you can use elsewhere within the build (or its sub-modules, if any).

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

Dependencies Section

The dependencies section lists all of the artifacts your build itself is dependent upon. Those dependencies may also have their own dependencies that need to be downloaded. Those are deemed transitive dependencies and will also be downloaded during the Maven build, if defined in a way that Maven can understand.

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

You will see additional artifacts downloaded during the build, those would be:

  • Maven plugins – the code performing the build,
  • Dependencies for the Maven plugins

Build Section

Plugin Management/Plugins

This section defines which Maven plugins will perform the build steps. While Maven defines default values, the maven-archetype-quickstart pins the version of those plugins, so that your build’s behavior won’t change if a new version of a plugin is published.

Hello World! – A Java Application from a Simple Maven Project Archetype

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.