📜 ⬆️ ⬇️

Apache Maven - web application

If you already know what Maven is and want to build a simple modular web application (if not, you can read the topic about it and the basics ). The topic of this topic is how to configure pom.xml, add a separate module to the project, connect plugins, deploy the application on the Apache Tomcat server.

Intro


So, take for example a simple web application. It consists of modules: model (Entity and Dao classes), service (services), web (servlets). The project and each module has its own pom.xml file. As an IDE, eclipse will serve, but this is also true for NetBeans, IDEA. It is assumed that the maven plugin, Tomcat server, has already been added to the IDE.
Our steps:

Creating a Maven Project


In our IDE, we create the Maven Project, the IDE itself will ask you to specify items:
choose skip archetype selection, Group Id = com.mycompany, Artifact Id = myproject.
We connect modules:
<groupId>com.mycompany</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <modules> <module>model</module> <module>service</module> <module>web-servlet</module> </modules> 

By default, maven connects JRE version 1.4, we need 1.6, we connect the plugin:
 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> 

A module may have dependencies on other libraries, it is good practice to specify the version of the dependency not in the pom.xml of the module, but on the project, in dependencyManagment:
 <dependencyManagement> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1-b01</version> </dependency> </dependencies> </dependencyManagement> 

Creating a Maven Module


In the IDE, right-click on the project, create the Maven Module. In the pom.xml of this module we indicate the project:
 <packaging>war</packaging> <parent> <groupId>com.mycompany</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> 

In dependencies, you can also specify our modules (in this case, service). I repeat, if in the project pom.xml the version of the library is indicated, the version can be (and better) not indicated in the pom.xml of the module:
 <dependencies> <dependency> <groupId>com.mycompany</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> 

We also need a plugin for the application to be deployed on the server by a command in the Maven-> build IDE. I note that for Tomcat 6 and 7, the url lines will be different:
 <build> <finalName>project</finalName> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <url>http://localhost:8080/manager/html</url><!--  Tomcat 7 !--> <!--<url>http://localhost:8080/manager</url>!--><!--  Tomcat 6 !--> <server>tomcatServer</server> <path>/project</path> </configuration> </plugin> </plugins> </build> 

Configuring Maven, Tomcat


The Maven plugin should know login: Tomcat password to connect to the Tomcat manager. Open (if not, create) the file in the home directory /.m2/settings.xml, then in the IDE-> Maven-> User Settings-> Update Settings . Actually settings.xml:
 <settings> <servers> <server> <id>tomcatServer</id> <username>admin</username> <password>password</password> </server> </servers> </settings> 

In the Tomcat onf / tomcat-users.xml directory, add the rights to the same manager:
 <tomcat-users> <role rolename="manager"/> <role rolename="manager-gui"/> <role rolename="admin"/> <user username="admin" password="password" roles="admin,manager,manager-gui"/> </tomcat-users> 

It is worth noting that the IDE can run Tomcat with settings from the workspace or from the Tomcat directory. We chose the second option, so in the IDE itself we set the parameters of the Server- Server Location-> Use Tomcat installation .

Deploy to server


We start our Tomcat server from IDE. -> Run As-> Maven build life cycle for the module: -> Run As-> Maven build . In line Goals add tomcat: redeploy, run. So, the module crashed on localhost: 8080 / project /. All dance and sing. In Eclipse, it is very convenient to do another deployment on Shift+Atl+X, M

Useful links:
maven.apache.org/guides
tomcat-maven-plugin

')

Source: https://habr.com/ru/post/148841/


All Articles