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.
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 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.
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.
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
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.
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 repositorysystemPath Refers to the path specified in. |
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.
Maven builds perform a set "phase" in sequence according to the life cycle. There are three life cycles defined
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.
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 |
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.
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.
In pom.xml, set the following in the parent module of the multi-module project.
pom
for packaging.In the child module, specify the groupId / artifactId / version of the parent module in the parent tag and inherit it.
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.
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.
If you are accessing the Internet via a proxy, you need to set the proxy.
Describe as follows in ~ / .m2 / setting.xml
.
setting.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<proxies>
<proxy>
<id>http_proxy</id>
<active>true</active>
<protocol>http</protocol>
<username>Authentication account</username>
<password>Authentication password</password>
<host>IP address of proxy server</host>
<port>proxy server port number</port>
</proxy>
<proxy>
<id>https_proxy</id>
<active>true</active>
<protocol>https</protocol>
<username>Authentication account</username>
<password>Authentication password</password>
<host>IP address of proxy server</host>
<port>proxy server port number</port>
</proxy>
</proxies>
</settings>
~ / .m2 / setting.xml
in User Setting.~ / .m2 / repository
.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
Create a parent pom that makes up the multi-module.
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.
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]
<properties>
<java-version>1.8</java-version>
<maven.compiler.source>${java-version}</maven.compiler.source>
<maven.compiler.target>${java-version}</maven.compiler.target>
</properties>
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.
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
Create a web application module that will be the main module this time.
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
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.
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.)
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
jdbc.url=jdbc:oracle://xxxxxxxx:1521/ora
jdbc.username=username
jdbc.password=password
To build for 3 environments of development environment (dev), verification environment (staging), and production environment (prod), set as follows in profile.
pom.xml
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<jdbc.url>jdbc:oracle://xxxxxxxx:1521/oradev</jdbc.url>
<jdbc.username>Development environment username</jdbc.username>
<jdbc.password>Development environment password</jdbc.password>
</properties>
</profile>
<profile>
<id>staging</id>
<properties>
<jdbc.url>jdbc:oracle://xxxxxxxx:1521/orastaging</jdbc.url>
<jdbc.username>Verification environment username</jdbc.username>
<jdbc.password>Verification environment password</jdbc.password>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<jdbc.url>jdbc:oracle://xxxxxxxx:1521/oraprod</jdbc.url>
<jdbc.username>Production username</jdbc.username>
<jdbc.password>Production password</jdbc.password>
</properties>
</profile>
</profiles>
ʻ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 with
mvn -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.
pom.xml
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<resources.directory>${basedir}/src/main/resources</resources.directory>
</properties>
</profile>
<profile>
<id>staging</id>
<properties>
<resources.directory>${basedir}/resources/staging</resources.directory>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<resources.directory>${basedir}/resources/prod</resources.directory>
</properties>
</profile>
</profiles>
···
<build>
<resources>
<resource>
<directory>${resources.directory}</directory>
</resource>
</resources>
</build>
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.
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.)
~ / .m2 / 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.
clean compile`` clean install site
etc.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)
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