Given that I have recently learned the development of a distributed project, I will talk about the actual development of using IntelliJ IDEA to create multi-module projects based on Maven. There may be inappropriate aspects, but the project can be run through. Please put forward all the shortcomings and discuss them together.
First, let’s show the project directory of the final project:
A brief introduction to the directory structure:
common-parent is the parent project of all projects, mainly used to manage the jar packages and their versions used by all projects.
common-utils is a public tool-class project, inheriting the parent project, and it will be labeled as a jar package for use by other projects.
taotao-manager is a project that inherits from our parent project.
taotao-manager-pojo is a submodule of our own project, which depends on taotao-manager and is divided into jar packages.
taotao-manager-mapper is a submodule of our own project, and it depends on taotao-manager-pojo and is divided into jar packages.
taotao-manager-service is a submodule of our own project, and it depends on taotao-manager-mapper and is made into a jar package.
taotao-manager-web is a submodule of our own project, and it depends on taotao-manager-service and is combined into a war package.
The following are the specific operations:
1. First open IntelliJ IDEA and follow the steps below:
Or open IDEA for the first time,
2. Go to the creation directory and select Empty project, next
3. Go to the New Project page, fill in the project name, select Project location, and click Finish
Wait for IDEA to load and proceed to the next step
4. Select File>New>Module and enter the New Module page:
5. On the New Module page, follow the steps below:
6. Fill in the organization name and project name, as well as version number
7. Configure the local maven directory and maven repository configuration files
8. The last step in creating the parent project, name the Module, select the working directory, and select Complete
Wait for the generation of the pom file, please note that the packaging method is to be modified as pom. Modify the pom file as follows: (Since the pom file is long, only part is given, and all are obtained, please pay attention to github)
<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>cn.william</groupId> <artifactId>common-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging><!--Modify the packaging method--> <name>common-parent</name> <url>http://maven.apache.org</url> <!--Central definition of dependency version number--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.12</junit.version> ...(omitted) </properties> <dependencyManagement> <dependencies> <!-- Time Operation Component--> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> ...(omitted) </dependencyManagement> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- Resource File Copy Plugin--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java compilation plugin--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build></project> First, the public tool class is inherited from the parent project common-parent.
One thing to note is that in order for the project to appear as a submodule (similar to development in eclipse), there is a step to note in creating common-utils:
Pay special attention to the next step, this step will affect the directory structure.
Also note that the package method of the project is jar, which is different from the parent project.
<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"> <parent> <artifactId>common-parent</artifactId> <groupId>cn.william</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../common-parent/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>common-utils</artifactId> <packaging>jar</packaging><!-- to jar package--> <name>common-utils</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Dependencies of jar package--> <dependencies> <!-- Time Operation Component--> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency> ...(omitted) </dependencies></project>
Create a development project taotao-manager. The creation steps are the same as above. It also inherits common-parent and modifys its pom file. The details are as follows:
<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"> <parent> <artifactId>common-parent</artifactId> <groupId>cn.william</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../common-parent/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>taotao-manager</artifactId> <packaging>pom</packaging> <name>taotao-manager</name> <url>http://maven.apache.org</url> <modules> <module>taotao-manager-pojo</module> <module>taotao-manager-mapper</module> <module>taotao-manager-service</module> <module>taotao-manager-web</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Dependency Management--> <dependency> <groupId>cn.william</groupId> <artifactId>common-utils</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <!-- Configuring Plugins--> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8080</port> <path>/</path> </configuration> </plugin> </plugins> </build></project>
Create submodules such as taotao-manager-pojo, mapper, service, web, etc., as follows:
Right-click taotao-manager, select New, Module,
Note in this step that unlike when creating common-utils, it is to add its submodules to the taotao-manager directory:
The relevant configuration of maven, keep the default:
Just pay attention to the directory where the submodule is located:
Create other submodules under taotao-manager
When we mentioned above, we create the taotao-manager-pojo submodule. The other three submodules are created similarly to them, with a little difference. When creating the taotao-manager-web submodule, the type of the maven project is selected as follows:
Modify the pom file of the submodule
The packaged methods of submodules pojo, mapper, and service are all jars, and only web submodules are packaged as war packages.
The pojo submodule, pom.xml is as follows:
<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"> <parent> <artifactId>taotao-manager</artifactId> <groupId>cn.william</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>taotao-manager-pojo</artifactId> <packaging>jar</packaging> <name>taotao-manager-pojo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties></project>
The mapper submodule depends on the pojo submodule. The pom.xml details are as follows:
<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"> <parent> <artifactId>taotao-manager</artifactId> <groupId>cn.william</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>taotao-manager-mapper</artifactId> <packaging>jar</packaging> <name>taotao-manager-mapper</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- Dependency and pojo submodule--> <dependency> <groupId>cn.william</groupId> <artifactId>taotao-manager-pojo</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Connection Pooling--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> </dependencies></project>
The service submodule depends on the mapper submodule, and the pom.xml details are as follows:
<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"> <parent> <artifactId>taotao-manager</artifactId> <groupId>cn.william</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>taotao-manager-service</artifactId> <packaging>jar</packaging> <name>taotao-manager-service</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- Dependencies and mapper submodules--> <dependency> <groupId>cn.william</groupId> <artifactId>taotao-manager-mapper</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> ... (omitted) </dependencies></project>
The web submodule depends on the service submodule, the pom.xml details are as follows:
<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/maven-v4_0_0.xsd"> <parent> <artifactId>taotao-manager</artifactId> <groupId>cn.william</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>taotao-manager-web</artifactId> <packaging>war</packaging><!--Part into war package--> <name>taotao-manager-web Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- Dependency on service --> <dependency> <groupId>cn.william</groupId> <artifactId>taotao-manager-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> ...(omitted) </dependencies> <build> <finalName>taotao-manager-web</finalName> </build></project>
Create index.jsp
At this point, the project creation has been completed. We can create index.jsp in the webapp directory of taotao-manager-web. The details are as follows:
Using tomcat plugin
We use maven's tomcat plugin to run the project. Before this, make sure to configure the tomcat plugin in the taotao-manager's pom file:
...(Omitted) <build> <!-- Configuration Plugin--> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8080</port> <path>/</path> </configuration> </plugin> </plugins></build>... (Omitted)
Configure Maven Tomcat Plugin to run web projects
(1) Add new configuration
(2) Click Run, or click the debug next to it
(3) Error in issue
An error occurred, which may be that common-parent and common-utils were not installed in the local repository. The error occurred is that the common-utils jar package cannot be found, so we need to install it.
Waiting to install the jar package to the local repository, it is a little slow, maybe, be patient, wait. . .
After the installation is completed, run again and the following information appears to prove that the operation is successful:
Access through the browser:
At this point, our project environment has been successfully built, and the next step is the actual project development.
statement:
① Regarding the omitted part in the above code, it is because the content is too long, so it is omitted. If you want to view the completed project, please check my github, address:
https://github.com/williamHappy/tao-shopping-mall
② Please acknowledge any shortcomings above. After all, my technology is limited and I am also in the learning stage. I can also give reasonable opinions and improve them together.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.