[JAVA] Introduce Maven to Tomcat project


We've never used a build tool in our project, so we've summarized Maven for implementation. This article is designed to give you the information you need to understand yourself and deploy it to your project members. Also, since Maven3 is used, the description is based on this assumption.

What is Maven

Maven is a project management tool that can control the execution of Java program builds (manage jar libraries, compile, test, WAR and JAR generation) and generate reports of results. You can also extend the plugin to add functionality to your builds and reports. Maven can control detailed instructions by describing the process you want to execute in pom.xml (Project Object Model). For example, you can generate a WAR by specifying the version of the jar library to be used and environment-dependent settings such as the DB connection destination.

pom.xml I will summarize only the outline. Please see Maven-POM Reference for details. groupId / artifactId / version is a required setting value.

element Description
groupId A unique ID for your organization or project. In some cases, it is defined according to the package structure of the project.
artifactId Name that identifies the project
version Project version
packaging The state of the project deliverables. You can specify war, jar, etc.
dependencies Library referenced by the project
parent Specifies the parent POM to reference.
dependencyManagement Defines the template for the dependent library.
modules Defines the modules managed by the multi-project.
properties The value declared in this tag is in pom${X}You can refer to it at. It is used when you want to define a value that is generally referenced in the project, such as JDK Version and Encording.
build Defines the plugin to be executed at build time.
reporting Define the plugin to be executed at the time of site.
repositories Defines a repository to search other than remotely.
pluginRepositories Define a Plugin-only repository that you want to search other than remotely.
profiles Define the setting information for each execution environment of maven. For example, you can switch the environment between production, staging, and development.


The version control of the project specified by the version tag generally follows Semantic Versioning. That is, it is represented by "X.Y.Z (major minor patch) -identifier (alpha / beta / m / rc / SNAPSHOT, etc.)". When creating a new Maven project in Eclipse or IntelliJ IDEA, the default version is 0.0.1-SNAPSHOT.

Dependency resolution

Dependency resolution is an important feature in Maven and manages the libraries (external jars) that your project depends on. By describing the dependency [dependency] in pom.xml, the required library is automatically downloaded from the Maven repository. This feature eliminates the need to download the required libraries yourself from the site and add them to your build path or commit to a version control system. You can also eliminate environment setting conflicts caused by different classpaths depending on the development environment.

Maven repository

The Maven repository is where the libraries are managed. Maven will download the required libraries from the repository when you run the build. Project deliverables can also be uploaded to the repository for use by other projects.

Central repository

This is the official Maven repository published on the Internet. Versions of various libraries are available here, and you can download the required libraries locally. Central Repository

Remote repository

Manage the library as you would a central repository. You can also build your own remote repository using Sonatype NEXUS. For example, the following repositories are remote repositories.

Local repository

A repository built in the development environment. When Maven searches for a library, it first searches the local repository, and if it does not exist, it searches for the central repository or remote repository and downloads it. The local repository is located in ~ / .m2 / repository.

Scope If you specify scope, you can specify the status of defining the library in classpath. If not specified, it will be compile.

Scope Description
compile Define it in the classpath in all situations.
provided Equivalent to compile, but not included in the artifact. Specify this when the library is prepared mainly on the environment side where the artifacts such as JDK and Servlet API are deployed.
runtime A library that is required when the artifact is executed and is included in the artifact.
test Test A library that needs to be compiled and executed when the code is executed. This includes JUnit and Mockito. It is not included in the deliverables.
system Equivalent to provided, but the library is not a repositorysystemPathRefers to the path specified in.

Run maven


The process executed by Maven is called "goal" for each function. The "goal" is provided by the plugin and runs under mvn plugin name: Goal name. A plugin has multiple goals.


What you want to do in Maven is called a "phase". Multiple "goals" are linked to a phase, and if you execute the phase you want to perform with mvn phase name, the" goals "linked to the" phase "will be executed in order. The order in which the phases are executed is determined by the life cycle described later.

Build life cycle

Maven builds perform a set "phase" in sequence according to the life cycle. There are three life cycles defined

default cycle

It's literally a standard life cycle. Since there are 23 Phases in total, only the main Phases will be explained here. (Phase not described will be the pre-processing of the next Phase and the post-processing of the previous Phase.) "Artifact" refers to war, jar, etc.

No. Phase Overview
1 validate Verify the legitimacy of the project
7 compile Compiling the project
15 test Run unit tests
17 package Creation of deliverables (packaging)
21 verify Verification of deliverables
22 install Place artifacts in local repository
23 deploy Place artifacts in remote repository

For example, running mvn package will perform 17 phases from validate to package and generate artifacts. Running up to mvn deploy will run all 23 phases from validate to deploy.

clean cycle

The clean cycle is a life cycle for deleting temporary files generated by a build. Temporary files are locally generated class and resource files such as compile and test. (In a normal Maven project, the file output to the target directory)

It's a good idea to always run clean before running the build in the default cycle, as the temporary files left over when rebuilding may be reused. Example mvn clean package

No. Phase Overview
1 pre-clean Preprocessing for temporary file deletion
2 clean Delete temporary files
3 post-clean Post-processing of temporary file deletion

site cycle

The site lifecycle generates documentation for your project. For example, use the plugin described later to generate javadoc, static analysis result reports such as checkstyles and FindBugs, test results and coverage reports.

If you specify the site at the same time as the build and execute it, the build result document will be generated. Example'mvn install site'

No. Phase Overview
1 pre-site Preprocessing for document generation
2 site Delete document
3 post-site Post-processing of document creation
3 site-deploy Place the document on the specified web server


Plugins are Maven features written in Java. The plugin also provides goals associated with the phases of the life cycle. In addition to this, various plugins are open to the public, and you can also create your own plugins. Please check the HP of each plugin as the usage is different for each plugin.


It mainly defines plugins that you want to run in the default life cycle.


site Defines a plugin to output a run-time report. For example, you can check the static analysis results such as checkstyle and findbugs, and you can browse the javadoc of the project code.

Multi-module project

In Maven, one project can be managed by dividing it into "modules" for each role and service. For example, core module, plugin module, common function module, etc. For such a project configuration, you can define the module configuration and common settings in pom.xml of the parent module that manages the entire project and inherit it in pom.xml of each module.

Multi-module project configuration example

In pom.xml, set the following in the parent module of the multi-module project.

In the child module, specify the groupId / artifactId / version of the parent module in the parent tag and inherit it.

Introduction example to development project

Take the process of migrating a project under development by a group to a Maven project as an example.

In Eclipse, you can convert an existing project to a Maven project by Right-click the project> Configuration> Convert to Maven project, but since this is the first Maven project creation, I decided to copy the module to the project created from scratch.


Maven installation

See Install Maven for more information. Uncompress the compressed file and put the bin folder in the environment variable PATH. Open a command prompt and run mvn --version to see the version information.

Next, create a Maven working folder .m2. The working folder is generally created in the user directory ~ / .m2. In addition, create the following directories and files under the working folder. ~ / .m2 / repository This is the directory that will be the local repository. ~ / .m2 / setting.xml This is a user setting file.

proxy settings

If you are accessing the Internet via a proxy, you need to set the proxy. Describe as follows in ~ / .m2 / setting.xml.


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      <username>Authentication account</username>
      <password>Authentication password</password>
      <host>IP address of proxy server</host>
      <port>proxy server port number</port>
      <username>Authentication account</username>
      <password>Authentication password</password>
      <host>IP address of proxy server</host>
      <port>proxy server port number</port>

Eclipse settings

  1. Select Window> Preferences> Maven> User Settings.
  2. Specify ~ / .m2 / setting.xml in User Setting.
  3. For Local Repository, specify ~ / .m2 / repository.

Creating a Maven project

This time we will convert an existing Tomcat project to a Maven project. It is managed in the following multi-module configuration.

project ├─ main module ├─ sub module1 ├─ sub module2 └─ sub module3

Creating a parent pom

Create a parent pom that makes up the multi-module.

  1. Select File> New> Maven project.
  2. In the New Maven project, check "create a simple project (skip archetype selection)" and press Next.
  3. In the Configure project Artifact, type the following, then press Finish.

groupId ・ ・ ・ ・ Unique ID for the project (jp.co.company name.product name/com.product name etc.) artifactId ・ ・ ・ Project identification name version ・ ・ ・ Version (First, the default 0.0.1-SNAPSHOT is fine.) package ・ ・ ・ ** pom **

Once created, you will have a src folder and pom.xml. This time, src is unnecessary, so delete it.

Specifying the Jave version used by Maven for compilation

Java5 (1.5) is specified by default, so open the property in pom.xml and specify the version. By specifying it as the parent pom, it will be reflected in the sub module as well. If you want to change the version with sub module, specify property in pom.xml of that module as well.

pom.xml[JDK version]


Creating a sub module

Create a sub module under the parent project. The sub module created this time will be the jar library referenced in the web module described later.

  1. Right-click on the parent project> Maven> New Maven Module Project.
  2. In the New Maven project, uncheck "create a simple project (skip archetype selection)".
  3. Enter the name of the module in Module Name, confirm that the parent project is specified in Parent Project, and press Next.
  4. Under Select an Archetype, select Artifact ID: maven-archetype-quickstart and press Next.
  5. Confirm and enter the following in Artifact of Configure project, and press Finish.

groupId ・ ・ ・ ・ Same as the parent project artifactId ・ ・ ・ module name version ・ ・ ・ Same as the parent project package ・ ・ ・ ** jar **

A sub module is created with the following folder structure. sub module ├src/main/java └src/test/java

Creating a main module (Web module)

Create a web application module that will be the main module this time.

  1. Right-click on the parent project> Maven> New Maven Module Project.
  2. In the New Maven project, uncheck "create a simple project (skip archetype selection)".
  3. Enter the name of the module in Module Name, confirm that the parent project is specified in Parent Project, and press Next.
  4. Under Select an Archetype, select Artifact ID: maven-archetype-webapp and press Next.
  5. Confirm and enter the following in Artifact of Configure project, and press Finish.

groupId ・ ・ ・ ・ Same as the parent project artifactId ・ ・ ・ module name version ・ ・ ・ Same as the parent project package ・ ・ ・ ** war **

A web module is created with the following folder structure. web module ├src/main/resources └src / main / webapp (Root folder of web application. META-INF and WEB-INF exist under this.)

In maven-archetype-webapp, java source folder and test folder are not created, so create them.

web module ├src/main/java ├src/main/resources ├src/main/webapp └src/test/java

Dependency resolution

Add dependency to pom.xml for each module. Also, in Eclipse, you can use the dedicated Maven POM Editor to input in a visually easy-to-understand manner.

Open pom.xml in Maven POM Editor and open the Dependencies tab. Click Add for Dependencies on the left to open the Select Dependency window. Specify the Group ID / Artifact ID / Version / Scope of the library you want to add and press OK. If you don't know, you can search by Enter ... in the middle.

If you want to add the added library to Dependency Management of the parent Pom and refer to it in common with other Sub modules, press Managed. Select the library you want to add to the parent pom from the left, select the parent Pom on the right and press OK. This will add the library to the parent Pom's Dependency Management, but if you want to use it with another Sub Module, open that pom.xml and add the library.

Specifying a plug-in

Execute the following plugin to output the report with mvn site. If you build the report using Jenkins, you can also output it to the build result.

A setting example can be found at [here](http://qiita.com/kazokmr/items/971ec5cc8e3a07f2429a#pomxml example). (This is my article.)

Environment-dependent settings

pom.xml has a profile tag. For example, it is used when you want to change a certain setting value according to the situation. I think that there is a DB connection destination as a method that is often used. For example, define the connection destination in the resourceProperty file


To build for 3 environments of development environment (dev), verification environment (staging), and production environment (prod), set as follows in profile.


      <jdbc.username>Development environment username</jdbc.username>
      <jdbc.password>Development environment password</jdbc.password>
      <jdbc.username>Verification environment username</jdbc.username>
      <jdbc.password>Verification environment password</jdbc.password>
      <jdbc.username>Production username</jdbc.username>
      <jdbc.password>Production password</jdbc.password>

ʻActiveByDefault = trueis the default value, so refer to it when building without specifying anything or when the web application is started on the IDE. If you want to switch to the verification environment or production environment, specify the id of profile withmvn -P staging package, man -P prod packageand-P` option.

The environment-dependent configuration file is managed by a dedicated module, and by separating it from the Web application module, the war file can be reused.

This is a way to switch some of the properties, but profile also allows you to switch the property file itself. For example, suppose you manage the following directories for property files by environment.

src/main/resources/··· Development environment (default) resource file directory
resources/staging/... Directory of resource files for verification environment
resources/prod/··· Production environment resource file directory

The pom.xml when replacing the files under src / main / resources / for each environment is as follows.



In the build tag, the path of the resources directory is defined in $ {resources.directory}, and the directory path for each environment is defined in profile. This will create an environment-independent war that can be reused in another environment.

In addition, I think that it is better to manage the environment setting file with a dedicated module instead of the web application module, go out as a reference library, and do not include it in war.

The Terasoluna guidelines also explain how to build for environment-dependent files. Terasoluna --3.5. Build Development Project

~~ This project also has 3 DB connection destinations, development environment / verification environment / production environment, so I wanted to use profile, but the configuration file of the connection destination was not under the resource folder and under the resource folder. Due to the large number of files in, it was difficult to manage all of them, so I first migrated without using profile. ~~ I tried it in this article.

How to refer to the OJDBC driver

The OJDBC driver (oracle.jar) is not managed in the Maven repository, so you need to include it in your Maven project by doing one of the following: (In this project, Oracle.jar is set to runtime, and on the production server, the jar is placed in the lib of Tomcat.)

1. Register with dependency in system scope

  1. Download the required oracle.jar from the official Oracle website.
  2. Place oracle.jar anywhere in your project. (Lib folder, etc.)
  3. Add to dependency in pom.xml. (Write groupId, artifactId, version as usual.)
  4. Set the scope of dependency to SYSTEM and specify the PATH to oracle.jar placed in SYSTEMPATH.

2. Put oracle.jar in your local repository

  1. Download the required oracle.jar from the official Oracle website.
  2. Place oracle.jar in your local repository ~ / .m2 / repository.
  3. Add to dependency in pom.xml. (Write groupId, artifactId, version as usual)

3. Make it available from Oracle's official Maven repository.

How to use the official Oracle Maven repository

This method itself is unconfirmed and unverified. However, it seems that it is necessary to set the OTN license in each development environment and it is necessary to execute the mvn command for the first time in Eclipse.

Run build

  1. Right-click on the project or module you want to build and select Run As> Maven build.
  2. The Maven setting screen will be displayed. Enter the Goal or Phase you want to execute in Goals. Example clean compile`` clean install site etc.
  3. The execution result of Maven is displayed on the console. If it is Success, it is complete. If there is an error, eliminate the cause.
  4. If you were running install, the war and jar files will be uploaded to your local repository.

Server settings

Although it has nothing to do with Maven, when running a web application in Eclipse, the Tomcat plug-in was installed so far, but this time I will explain how to run the Tomcat application using the WTP plug-in. (It is assumed that the WTP plug-in is installed in Eclipse)

  1. Open the Server view and right click> New> Server.
  2. Under Define a New Server, select Apache> Tomcat vX.X Server. (X.X is the version of Tomcat you want to use)
  3. When the Tomcat Server settings screen is displayed, specify the local Tomcat directory in the Tomcat installation directory, or install Tomcat from the Download and Install button.
  4. Similarly for JRE, select local or install and press Finish.
  5. Return to the Sever view, right-click on the added Tomcat vX.X Server> and select Add and Remove.
  6. Available Web modules will be displayed, select them and press Add to finish.
  7. After adding the web module, right-click on Tomcat vX.X Server> Start to start Tomcat.

Reference site

Apache Maven Project Wikipedia Maven Basics How to get started with Maven3 Summary of how to write POM for Maven2 and Maven3 ~ Overview ~ Maven build life cycle I'll explain "What is a build tool?" To a newcomer who has only run a Java app from Eclipse ... and then to CI 6. Profile | TECHSCORE

Recommended Posts

Introduce Maven to Tomcat project
Introduce Kotlin to your existing Java Maven Project
How to make a Maven project
Create Maven Project
Added slf4J + logback to Eclipse Maven project
[Eclipse / Tomcat] Servlet + JSP in Maven webapp project
Port tomcat to MAMP
Create Maven Web Project
Deploy the Spring Boot project to Tomcat on XAMPP
Deploy Vapor Project to Heroku
How to introduce Basic authentication
Try to introduce OpenCV to Android application
SCP transfer War file to Tomcat
CI for Maven project on AppVeyor
CI for Maven project at CircleCI
Create a tomcat project using Eclipse
Until you introduce fonts to Rails
[STS] Error due to project overlap
To switch JVM for each project
Addicted to project imports from GitHub
Procedure to publish to Maven Central Repository
How to introduce jQuery in Rails 6
Create a Maven project with a command