Today's content
- 1. Maven compilation plugin
- 2. Create webMaven project
- 3. Configure tomcat plugin to run web project in Maven
- 4. Configure jetty plugin to run web project in Maven
- 5. Use provided to prevent dependency conflicts
- 6. Inheritance in Maven (parent-child project)
- 7. Aggregate project in Maven
-
- 0. Parent project (the following modules are all submodules of the parent project)
- 1. Write a model module to store entity classes, etc.
- 2. Write a dao module specifically for operating the database
- 3. Write a service module specifically for business services
- 4. Write a background web module to simulate a background page development
- 5. Package and install to local warehouse
1. Maven compilation plugin
- You can change the jdk version of the project compilation compile through the pom.xml configuration plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
2. Create webMaven project
- Choose to use a template to create (the subsequent steps are consistent with ordinary javaMaven projects)
- It is found that the webMaven project created with the template lacks folders such as test tests, resources resources, etc., you can add it if necessary, and then mark it as the corresponding folder.
- IntelliJ idea is very powerful. For webMaven projects created using templates, you will be prompted when the folders are filled under src and main, just click
3. Configure tomcat plugin to run web project in Maven
tomcat:run
- Configure a tomcat:run directly in the running configuration, the default is tomcat 6.0
- Tomcat6 is not compatible with jdk1.8, just change it to 1.7
tomcat7:run
- You need to configure the tomcat7 plugin in pom.xml first, and then configure tomcat7 in the run configuration: run
- Configuration in pom.xml
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8081</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
</configuration>
</plugin>
Local tomcat running
- Configure the maven plugin cargo:start, install before running, otherwise an error will be reported!
- Configuration in pom.xml
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.4</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<home>D:\apache-tomcat-7.0.79</home>
</container>
<configuration>
<type>existing</type>
<home>D:\apache-tomcat-7.0.79</home>
<properties>
<cargo.servlet.port>9234</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
- You can also directly configure the local Tomcat
4. Configure jetty plugin to run web project in Maven
- Configuration in pom.xml
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>${basedir}/target/Maven_javaWeb</webApp>
<webAppConfig>
<!--访问路径 根路径访问-->
<contextPath>/</contextPath>
</webAppConfig>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8888</port>
<headerBufferSize>16192</headerBufferSize>
</connector>
</connectors>
</configuration>
</plugin>
5. Use provided to prevent dependency conflicts
- Create a new servlet
- Add configuration in web.xml
- Make a simple jump
- You need to configure servlet-api and other gav in pom.xml. If you don’t configure servlet, you will get an error, but you will get an error when you configure it at runtime (provided in the Tomcat container), so you need to configure provided to only make it valid at compile time, and don’t enter at the end. Bale
- Check that the war package does not enter the packaging
- If not configured, the default scope is compile and will enter the packaging
6. Inheritance in Maven (parent-child project)
- First use the template to quickly create a parent project MavenParent
- Then create the sub-project MavenChild module in the project and specify the parent project
- Inheritance is to eliminate duplication and can extract many identical configurations. For example: grouptId, version, etc.
- Configure the parent 's GAV, and then add dependencies in the parent project, and the subproject can directly inherit and use
- For example, configure junit in the parent project
- Subprojects can be directly inherited and used, without the need to write dependencies like the previous dependency transfer
Version number extraction in parent project
- When there are more and more jar packages defined in the parent project, it may become more and more troublesome to find, so the version number can be extracted into an attribute for centralized management.
- Configure a properties tag in the pom.xml of the parent project, and the tags inside can be customized
7. Aggregate project in Maven
- Aggregation is generally a project divided into multiple modules for development, each module is an independent project, but if all modules must be aggregated together at runtime to be a complete project, you can use the Maven aggregation project.
- For example, e-commerce projects include commodity modules, order modules, user modules, etc. You can create separate projects for different modules, and finally aggregate the different modules together when packaging.
- For example, the presentation layer, business layer, and persistence layer in the same project can also be layered to create different projects, and finally packaged and run, and then aggregated together.
- The aggregation project is similar to the parent-child project above, and is directly modified on the parent-child project
0. Parent project (the following modules are all submodules of the parent project)
1. Write a model module to store entity classes, etc.
2. Write a dao module specifically for operating the database
- Write it like this now. Dao is generally not used in the future, it is better to use mybatis mapper
3. Write a service module specifically for business services
4. Write a background web module to simulate a background page development
- The front are all common modules, here is the web module, you can use the previous modules at will
5. Package and install to local warehouse
- Install directly to the parent project (it will be packaged and installed locally according to the order of the modules in the parent project)
- There are corresponding packaged jars in model, dao, and service
- Multiple back-end development can use a set of model, dao, service