Build a Java App with Maven On FreeBSD

In this article, we will create a simple web application to manage Java web projects. You can create a java web application using the maven-archetype-webapp plugin. We can create using IDE tools as well as command line console. In the end, we will create a Spring MVC web application, displaying the current date on a JSP page.


1. FreeBSD Prerequisites

OS: FreeBSD 13.2 or later
Hostname: ns7
IP Server: 192.168.5.2
Java Version: openjdk version "17.0.9" 2023-10-17
Mvaen version: Apache Maven 3.9.6


2. Create a Java Project With Maven

As a first step, we will create a Java project, namely a Web App, using the Maven template. Run the command below to create a web project from the Maven template maven-archetype. We will place the Java Web App project in the "/var" folder
root@ns7:~ # cd /var
root@ns7:/var # mvn archetype:generate -DgroupId=site.unixwinbsd.blogapp -DartifactId=blogapp_project -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
root@ns7:/var # cd blogapp_project
The above command will create the Generated Web Project directory structure.



The web.xml, index.jsp and pom.xml files are project specific files generated by the Maven command line, these files you can change as needed.

Please open the pom.xml file. delete the script and replace it with the script below.

<?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>site.unixwinbsd.blogapp</groupId>
    <artifactId>blogapp_project</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>java-web-project Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <!-- https://maven.apache.org/general.html#encoding-warning -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.1.0.RELEASE</spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- logging , spring 5 no more bridge, thanks spring-jcl -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- junit 5, unit test -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>

        <!-- unit test -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <!-- for web servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Some containers like Tomcat don't have jstl library -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>java-web-project</finalName>
        <plugins>
            <!-- http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.12.v20180830</version>
            </plugin>

            <!-- Default is too old, update to latest to run the latest Spring 5 + jUnit 5 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>

            <!-- Default 2.2 is too old, update to latest -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>

        </plugins>
    </build>

</project>

In your Maven project create a Java folder, with the following command.
root@ns7:~ # cd /var/blogapp_project/src/main
root@ns7:/var/blogapp_project/src/main # mkdir -p java/site/unixwinbsd/blogapp
root@ns7:/var/blogapp_project/src/main # cd java/site/unixwinbsd/blogapp
root@ns7:/var/blogapp_project/src/main/java/site/unixwinbsd/blogapp # mkdir -p config
root@ns7:/var/blogapp_project/src/main/java/site/unixwinbsd/blogapp # mkdir -p controller
Create a Web App file in the Maven project we are creating:
  1. /var/blogapp_project/src/main/java/site/unixwinbsd/blogapp/WebInitializer.java
  2. /var/blogapp_project/src/main/java/site/unixwinbsd/blogapp/config/SpringConfig.java
  3. /var/blogapp_project/src/main/java/site/unixwinbsd/blogapp/controller/WelcomeController.java
root@ns7:/var/blogapp_project/src/main/java/site/unixwinbsd/blogapp # touch /var/blogapp_project/src/main/java/site/unixwinbsd/blogapp/WebInitializer.java
root@ns7:/var/blogapp_project/src/main/java/site/unixwinbsd/blogapp # touch /var/blogapp_project/src/main/java/site/unixwinbsd/blogapp/config/SpringConfig.java
root@ns7:/var/blogapp_project/src/main/java/site/unixwinbsd/blogapp # touch /var/blogapp_project/src/main/java/site/unixwinbsd/blogapp/controller/WelcomeController.java
Then create a "views" folder.
root@ns7:/var/blogapp_project/src/main/java/site/unixwinbsd/blogapp # cd /var/blogapp_project/src/main/webapp/WEB-INF
root@ns7:/var/blogapp_project/src/main/webapp/WEB-INF # mkdir -p views
Delete the "web.xml" file and move the "index.jsp" file.
root@ns7:/var/blogapp_project/src/main/webapp/WEB-INF # rm web.xml
root@ns7:/var/blogapp_project/src/main/webapp/WEB-INF # cd /var/blogapp_project/src/main/webapp
root@ns7:/var/blogapp_project/src/main/webapp # mv index.jsp WEB-INF/views
Create a Log file to the console.
root@ns7:/var/blogapp_project/src/main/webapp # cd /var/blogapp_project/src/main/resources
root@ns7:/var/blogapp_project/src/main/resources # touch logback.xml
After that we create a "test" folder.
root@ns7:/var/blogapp_project/src/main/resources # cd /var/blogapp_project/src
root@ns7:/var/blogapp_project/src # mkdir -p test/java/site/unixwinbsd/blogapp
The next step, create a "TestWelcome.java" file.
root@ns7:/var/blogapp_project/src # cd test/java/site/unixwinbsd/blogapp
root@ns7:/var/blogapp_project/src/test/java/site/unixwinbsd/blogapp # touch TestWelcome.java
You can download all the script contents from this file on Github:


After you download the script from Github, insert the script into each appropriate file. The final job is to compile the Maven project, with the commands mvn compile, mvn package and mvn install
root@ns7:/var/blogapp_project/src/test/java/site/unixwinbsd/blogapp # cd /var/blogapp_project
root@ns7:/var/blogapp_project # mvn compile && mvn package && mvn install
Run your Maven project.
root@ns7:/var/blogapp_project # mvn jetty:run
To see the results, access via http://localhost:8080/ via Google web browser.

In this article, we have learned how to create a simple maven based web application with FreeBSD server and we have run this web application in a Servlet container called jetty.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post