BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Apache Maven 3 : Web Application - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




mvn archetype:generate

Continuing from Apache Maven 3 - Dependencies.

In this chapter, we'll create a new web application which provides us *.war file and additional dependencies.

While we're running mvn archetype:generate, we'll choose
764: remote -> org.codehaus.mojo.archetypes:webapp-j2ee14 (J2EE 1.4 web application archetype)
as our archetype.

k@laptop:~/java/apache-maven-3.2.5/bin/myapp$ mvn archetype:generate
...
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 525: 764
Choose org.codehaus.mojo.archetypes:webapp-j2ee14 version: 
1: 1.0
2: 1.0.1
3: 1.1
4: 1.2
5: 1.3
Choose a number: 5: 
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-j2ee14/1.3/webapp-j2ee14-1.3.jar
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-j2ee14/1.3/webapp-j2ee14-1.3.jar (2 KB at 10.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-j2ee14/1.3/webapp-j2ee14-1.3.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-j2ee14/1.3/webapp-j2ee14-1.3.pom (911 B at 9.1 KB/sec)
Define value for property 'groupId': : com.k.bogotobogo
Define value for property 'artifactId': : mywebapp
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  com.k.bogotobogo: : 
Confirm properties configuration:
groupId: com.k.bogotobogo
artifactId: mywebapp
version: 1.0-SNAPSHOT
package: com.k.bogotobogo
 Y: : 
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: webapp-j2ee14:1.3
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.k.bogotobogo
[INFO] Parameter: artifactId, Value: mywebapp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.k.bogotobogo
[INFO] Parameter: packageInPathFormat, Value: com/k/bogotobogo
[INFO] Parameter: package, Value: com.k.bogotobogo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.k.bogotobogo
[INFO] Parameter: artifactId, Value: mywebapp
[INFO] project created from Archetype in dir: /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 59.347s
[INFO] Finished at: Sun Feb 01 14:07:12 PST 2015
[INFO] Final Memory: 18M/163M
[INFO] ------------------------------------------------------------------------

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/mywebapp$ ls
pom.xml  src

mywebapp_dirs.png





pom.xml

Here is our new pom.xml which Maven has generated for us:

<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.k.bogotobogo</groupId>
  <artifactId>mywebapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>mywebapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.4</source>
          <target>1.4</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Three things are worth mentioning:

  1. We have war package instead of jar package since it's a web application.
    <packaging>war</packaging>
    
  2. We have servlet and jsp dependencies.
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.4</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.1</version>
          <scope>provided</scope>
        </dependency>
    
  3. The compiler in Maven is actually a plugin. We set the behavior, especially, the configuration:
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
              <source>1.4</source>
              <target>1.4</target>
            </configuration>
          </plugin>
    




mvn compile

When we compile it, Maven will download all packages that our project is dependent on:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/mywebapp$ mvn compile
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mywebapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.pom
Downloaded: http://repo.maven.apache.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.pom (156 B at 0.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.jar
Downloaded: http://repo.maven.apache.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.jar (96 KB at 165.9 KB/sec)
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ mywebapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ mywebapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.477s
[INFO] Finished at: Sun Feb 01 15:15:00 PST 2015
[INFO] Final Memory: 11M/97M
[INFO] ------------------------------------------------------------------------
k@laptop:~/java/apache-maven-3.2.5/bin/myapp/mywebapp$ 




mvn package

mvn package:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/mywebapp$ mvn package
...
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ mywebapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [mywebapp] in [/home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/target/mywebapp-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/src/main/webapp]
[INFO] Webapp assembled in [98 msecs]
[INFO] Building war: /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/target/mywebapp-1.0-SNAPSHOT.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.686s
[INFO] Finished at: Sun Feb 01 15:16:40 PST 2015
[INFO] Final Memory: 12M/161M
[INFO] ------------------------------------------------------------------------




generated files

Let's look at what's been generated so far:

generated_files.png
  1. We see our package file mywebapp-1.0-SNAPSHOT.war in target folder.
  2. When we look into the src directory, we have nothing under src/main/java but we have index.jsp under the src/main/webapp folder:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1>
        </body>
    </html>
    
    The web.xml should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <display-name>mywebapp</display-name>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
            </welcome-file-list>
        </web-app>
    








Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Java Tutorials



Java Tutorial Home

Basics - Compiling and Launching

Inner Classes

Constructor

Enums

Static & Finally

Default and Protected

Polymorphism

Exception Handling

Exception Handling II

String Class

Threads

Threads II - State Transition

Threads III - Synchronization

Object Class

File I/O

Serialization

ArrayList

Autoboxing

Java Graphics Interface I - Basics

Java Graphics Interface II - Labels, Text Fields, Layouts

Java Graphics Interface III - paintComponent

TCP Sockets Server/Client

Scala - Functional Java Programming

Apache CXF install

Tomcat 7 Ubuntu 14 Install on Amazon EC2 instance

What is Apache Maven?

Maven life cycle

Eclipse Maven 3 plugin on Ubuntu 14.04

Apache Maven 3 - Setting up and creating a project

Apache Maven 3 - Compile, build, and install a Maven project

Apache Maven 3 - Dependencies

Apache Maven 3 - Web Application

Apache Maven 3 - Plugins (compiler)

Apache Maven 3 - Plugins (Jetty)

Eclipse CDT / JNI (Java Native Interface) / MinGW



Spring Framework

Hello World App with Spring 4 & Maven 3 - Part I




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Spring Boot



Spring Boot : Hello world with Mavan 3

Spring Boot : Hello world with Gradle 2

Spring Boot (Gradle 2) : Hello world with Authentication

Spring Boot : Deploying War file to Tomcat 8's webapps

How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy

Maven : mvn command cheat sheet

Spring-Boot REST API with CORS App Maven war file deploy to Tomcat

Spring-Boot / Spring Security with AngularJS - Part I (Introduction)

Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular)

Spring-Boot / Spring Security with AngularJS : Part III (Form-based Authentication)





JUnit & Maven Tutorial



JUnit 4 Introduction (Hello World)

JUnit 4 Test with Eclipse Luna (Hello World)

JUnit 4 Test with Maven (Hello World)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master