Maven compile plug-in, configure Tomcat plug-in in Maven, local Tomcat, jetty plug-in to run web projects, use of provided, inheritance in Maven (parent-child project), aggregation project in Maven-day02

1. Maven compilation plugin

  • You can change the jdk version of the project compilation compile through the pom.xml configuration plugin
    Insert picture description here
<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)
    Insert picture description here
  • 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.
    Insert picture description here
  • 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
    Insert picture description here

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

Insert picture description here
Insert picture description here
Insert picture description here

tomcat7:run

  • You need to configure the tomcat7 plugin in pom.xml first, and then configure tomcat7 in the run configuration: run
    Insert picture description here
  • 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>

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Local tomcat running

  • Configure the maven plugin cargo:start, install before running, otherwise an error will be reported!
    Insert picture description here
  • 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>

Insert picture description here

  • You can also directly configure the local Tomcat
    Insert picture description here
    Insert picture description here

4. Configure jetty plugin to run web project in Maven

Insert picture description here

  • 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>

Insert picture description here
Insert picture description here

5. Use provided to prevent dependency conflicts

  • Create a new servlet
    Insert picture description here
  • Add configuration in web.xml
    Insert picture description here
  • Make a simple jump
    Insert picture description here
    Insert picture description here
  • 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
    Insert picture description here
    Insert picture description here
  • Check that the war package does not enter the packaging
    Insert picture description here
  • If not configured, the default scope is compile and will enter the packaging
    Insert picture description here
    Insert picture description here

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
    Insert picture description here
  • Inheritance is to eliminate duplication and can extract many identical configurations. For example: grouptId, version, etc.
    Insert picture description here
  • 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
    Insert picture description here
  • Subprojects can be directly inherited and used, without the need to write dependencies like the previous dependency transfer
    Insert picture description here
    Insert picture description here

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
    Insert picture description here

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)

Insert picture description here

1. Write a model module to store entity classes, etc.

Insert picture description here
Insert picture description here

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
    Insert picture description here
    Insert picture description here

3. Write a service module specifically for business services

Insert picture description here
Insert picture description here

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
    Insert picture description here
    Insert picture description here

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)
    Insert picture description here
  • There are corresponding packaged jars in model, dao, and service
    Insert picture description here
  • Multiple back-end development can use a set of model, dao, service
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43414199/article/details/109095892