Idea operation Maven advanced chapter:
- After the above study, we have roughly understood: Maven Basics
What is Maven - why use it - Foundation Construction: - Maven can be said to be an indispensable tool for Java development:
ok!, Next, learn more about Maven's advanced articles!
Modular construction engineering [application]
Maven inheritance [dependency] and aggregation
What is aggregation?
- Project development is usually divided into groups and modules
- Each module is developed to complete a separate function.
- If you want to run the whole project, you need to aggregate each module to run together;
- For example, dao, service and web will eventually run as an independent war
Aggregation: you can create a parent module on the dao service web module: aggregate projects through the parent module. You only need to compile and package the parent project to run!
What is inheritance?
- It is very similar to Java: if a subclass inherits the extends parent class, it has the characteristics of the parent class;
- Maven inheritance:
In order to eliminate duplication, if dao, service and web are separated to create independent projects, the cost of each project will be reduced;
pom.xml will have many of the same dependent Jar packages~
These duplicate configurations can be extracted from the POM of the parent project Define a unified management declaration public Jar in XML. - The parent module must be packaged as pom, otherwise the project cannot be built. The child module is configured to indicate which parent module it inherits from:
- Inheritance and aggregation are usually used together.
Maven aggregate Demo case:
Database:
Parent project BBS parent
- Creating an ordinary Maven project and deleting the src parent project does not write code, but does an aggregation management function
- Add the required Maven modules:
- dao data layer
- entity layer
- service business logic layer
- For the web presentation layer, the above are all ordinary Maven projects, and here you need to specify projects as web Maven templates (refer to the previous article ~)
Parent project POM xml
<?xml version="1.0" encoding="UTF-8"?> <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> <!-- Group name version --> <groupId>org.example</groupId> <artifactId>bbs-parent</artifactId> <version>1.0-SNAPSHOT</version> <!-- Packaging method of parent project! --> <!-- We've seen packaging jar and war Yes, but the packaging method of aggregation module must be pom,Otherwise, the build cannot be completed. --> <packaging>pom</packaging> <!-- Child project of parent project integration! --> <modules> <module>bbs-dao</module> <module>bbs-service</module> <module>bbs-entity</module> <module>bbs-web</module> <!--<module>..Aggregated subproject..</module>--> </modules> <!-- General configuration.. --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.version>4.2.4.RELEASE</spring.version> </properties> <!-- SSM Required for the project Jar,Declared in parent project,Subprojects can also be used.... --> <dependencies> <!-- spring-Namespace:apl --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-beans: Bean of Apl --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> <!-- Log dependency --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.6.1</version> </dependency> <!-- JSON rely on --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency> </dependencies> </project>
be careful:
- <spring. version>4.2.4. RELEASE</spring. Version > spring version 4.0 and above are used by JDK 1.8!!
- 3. What version is jdk1 7!! Cry to death, find this mistake for a long time!!
BBS entity module
Entity layer: define pojo required by the project
- This is particularly simple Define a package and declare a class: just tap the entity attribute against the database!
- It should be noted that the POM of the subproject xml
Entity pom.xml
Basically, no configuration is required, but some sub projects aggregate the configuration of the parent project:
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Subproject,Point to parent project dependency configuration! --> <parent> <artifactId>bbs-parent</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <!-- Subproject:Project name --> <artifactId>bbs-entity</artifactId> </project>
bbs-dao
Persistence layer: dealing with the database, responsible for reading and writing operations
- The code will not be interpreted one by one here. Normal SSM query full table operation! If you need to know about SSM, you can click here!
- SortMapper.Java interface
- SortMapper. XML SQL mapping file
- mybatis-config.xml mybatis core configuration file
- applicationContext-mybatis.xml Spring core configuration file
Dao pom.xml
A child project can not only inherit the public dependencies of the parent project, but also define its own unique dependencies
Dao module depends on Entity module, and all dependencies to be introduced into other subprojects
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Subproject,Point to parent project dependency configuration! --> <parent> <artifactId>bbs-parent</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <!-- Subproject:Project name --> <artifactId>bbs-dao</artifactId> <!-- Unique dependency of subproject definition! --> <dependencies> <!-- mybatis:integrate SpringJar --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <!-- c3p0 Data source dependency --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> <type>jar</type> </dependency> <!-- mybatis rely on --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!-- mysql drive --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!-- Add entity module coordinates --> <dependency> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> <artifactId>bbs-entity</artifactId> </dependency> </dependencies> </project>
Spring core configuration file: ApplicationContext mybatis xml
C3P0 data source definition: you may have friends. If the data source is different, you don't have to find it alone
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Spring:Comments under scan package --> <context:component-scan base-package="com.wsm.mapper"></context:component-scan> <!-- Data source configuration C3P0 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <!-- Connect driver library user password.. --> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://Localhost: 3306 / BBS "> < / property > <! -- don't forget to specify the database! -- > <property name="user" value="root"></property> <property name="password" value="ok"></property> </bean> <!-- Connection factory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wsm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
bbs-servc
Business logic layer: process page requests and make encapsulated responses redi cache, etc Operation; This time, we simply call Dao method l
- Declare the interface normally and call Dao through the interface
- Because it was developed in modules Dao - Service is equivalent to two separate Maven projects. Modules interact through dependencies
Service pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Subproject,Point to parent project dependency configuration! --> <parent> <artifactId>bbs-parent</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <!-- Subproject:Project name --> <artifactId>bbs-service</artifactId> <!-- introduce dao rely on: service Introduce—— dao Introduce—— entity--> <dependencies> <dependency> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> <artifactId>bbs-dao</artifactId> </dependency> </dependencies> </project>
bbs-web
View layer: mainly composed of Controller and JSP page! Responsible for displaying the effect!
- General code of SSM:
- Controller spring MVC configuration! web.xml jsp page pom.xml
be careful:
- Maven web project is packaged in war package. Tomcat can only run war Package ~ while other modules are Jar package (default). Maven's parent project is packaged in pom type!
- This demonstration is about SSM project: Demo download
Entity - Dao - Service - Web: all are aggregated through Maven dependency;
Upload to the local warehouse before using install: find it in the local warehouse through dependency~ Of course, you can upload to the local warehouse through the parent project with one click~
maven private server setup:
- Formal development, different project teams develop different projects.
ssm_dao project is developed and released to private server. ssm_service Download dao from private server can't be in the company. Run around with u disk and find someone to copy and rely on
Maven warehouse built by the company:
Different people upload their developed modules to the private server. Of course, the required modules can also be downloaded from the private server
~The concept of private server has been explained in the last article~
Build private server environment
Download nexus
- Nexus is the Maven warehouse manager
- The maven warehouse can be built through nexus. At the same time, nexus also provides powerful warehouse management functions, component search functions, etc.
- Download Nexus at: http://www.sonatype.org/nexus/archived/
Install nexus
Unzip nexus-2.12.0-01-bundle Zip. In this tutorial, unzip it to disk D and enter the bin directory:
Execute nexus Bat install if not, run it by the administrator
Uninstall nexus
cmd enter the bin directory of nexus and execute: nexus Bat uninstall view window service list nexus has been deleted.
Start nexus
Method 1
cmd enter the bin directory and execute nexus bat start
Method 2
Start nexus service directly
visit: http://localhost:8081/nexus/ Default port 8081
View the nexus configuration file conf / nexus properties
Common configuration:
# Jetty section application-port=8081 # Access port configuration of nexus application-host=0.0.0.0 # nexus host listening configuration (no need to modify) nexus-webapp=${bundleBasedir}/nexus # nexus project directory nexus-webapp-context-path=/nexus # web access path of nexus # Nexus section nexus-work=${bundleBasedir}/../sonatype-work/nexus # nexus warehouse directory runtime=${bundleBasedir}/nexus/WEB-INF # nexus running program directory
Nexus built-in account admin password admin123
Warehouse type
Warehouse classification
- Public Repositories:
The warehouse group aggregates the warehouse whose Policy is Release and provides external services through an address; Download and upload will be saved here· - 3rd party:
A jar package used to deploy third-party releases that cannot be obtained from a public repository - Apache Snapshots
The snapshot version jar package used to proxy Apache Apache Apache Maven warehouse is a proxy warehouse that remotely downloads Apache Apache Maven's dependent jar - Central
The warehouse agent is Maven central warehouse, and its Policy is Release. Therefore, it will only download and cache the Release version jar package in the central warehouse, which is the same as Apache - Codehaus Snapshots
Snapshot version jar package used to proxy CodeHaus Maven repository - Release
The user deploys the jar package of the release version within the organization - Snapshots
The jar package used to deploy the snapshot (test) version within the organization
nexus has four types of warehouses
- group: Warehouse group
It is used to merge multiple hosted/proxy warehouses. Usually, we configure our own maven connection warehouse group. - Hosted: hosted
Host warehouse: deploy your own jar s to this type of warehouse, including releases and snapshot s:
Releases internal release warehouse
Snapshots company internal test version warehouse - Proxy: proxy
Proxy warehouse is a public warehouse used to proxy remote
For example, in the maven central warehouse, the user connects to the private server, and the private server automatically goes to the central warehouse to download jar packages or plug-ins. - virtual: virtual
jar or plug-in compatible with Maven 1 version
Warehouse Format
There are two types of Maven 1 and Maven 2. The following warehouse classification only introduces Maven 2
Introduction to warehouse Policy
Release: release version
Snapshots: snapshot version
The nexus warehouse is in the sonatype work directory by default:
Central: agent warehouse, agent central warehouse
Publish project to private server
ok, after understanding the private server and building the private server, you can upload the code module to the private server and download it!!
- In the collaborative development of multiple teams in an enterprise, some common components and development modules are usually published to private servers for use by other teams or module developers.
- This example assumes that multiple teams develop SSM separately_ dao,ssm_service,ssm_web
A team developed in ssm_dao will SSM_ Publish Dao to private server for ssm_service team use
Upload / download: of course, first up in the drop-down
upload
Step 1: configure the user and password connected to the private server;
- You need to configure maven environment on the client, that is, the computer to upload the module project, and specify the warehouse location to upload! Modify settings xml
Upload the setting of device Maven xml
Maven installation directory: configure < / servers > under < servers > tab in apache-maven-3.5.4-bin\apache-maven-3.5.4\conf directory
<!-- releases Connect release project warehouse --> <server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <!-- snapshots Connect test version project warehouse --> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server> <!-- More can be configured as appropriate... -->
Step 2: to upload the module configuration item POM xml
- Configure the address of the private server warehouse and upload the host warehouse of the company's private server
- Determine which host warehouse to upload according to the version number of the project
If the version is release, upload it to the release warehouse of the private server
If the version is snapshot, upload it to the snapshot warehouse of the private server
You can create modules in POM View settings in XML:
Find any module and find that its configuration version is: < version > 1.0-SNAPSHOT < / version > the default is the SNAPSHOT test version!
pom.xml
<distributionManagement> <!-- Non test version upload location configuration --> <repository> <!-- there id It's local Maven: Setting.xml Configured id --> <id>releases</id> <url> http://localhost:8081/nexus/content/repositories/releases/ </url> </repository> <!-- Test version upload location: --> <snapshotRepository> <id>snapshots</id> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>
Because my upload device and private server device are the same computer, all of which are actually developed by localhost and changed according to the company's port!
And this position is:
test
Print the project dao project into a jar package and publish it to the private server:
- Start nexus first
- Ensure that there are modules in the local warehouse, pack the package and issue the local warehouse of install in advance, and execute the Maven deploy upload command for the dao project:
You can see the console. The prompt of uploading appears: you can view the module just uploaded on the connection Of course, local warehouses also exist! Upload succeeded! - According to the project POM The version definition in XML determines which repository to publish to < version > 1.0-snapshot < / version >
- If version is defined as snapshot, check the snapshot warehouse of nexus after deploy
- If version is defined as release, the project will be published to the release warehouse of nexus, and the project will be published to the snapshot warehouse:
download
Meven setting to download the device Configure warehouse in XML
Configuration under < profiles > < / profiles > tab:
<profile> <!--profile of id--> <id>dev</id> <repositories> <repository> <!--Warehouse id,repositories Multiple warehouses can be configured to ensure id No repetition--> <id>nexus</id> <!--Warehouse address, i.e nexus Address of warehouse group:go public Look inside--> <url>http://localhost:8081/nexus/content/groups/public/</url> <!--Download releases component--> <releases> <enabled>true</enabled> </releases> <!--Download snapshots component--> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <!-- Plug in warehouse, maven The operation of depends on the plug-in, and you also need to download the plug-in from the private server --> <pluginRepository> <!-- Of plug-in warehouse id Duplication is not allowed. If the configuration of the rear side is repeated, the front side will be overwritten --> <id>public</id> <name>Public Repositories</name> <url>http://localhost:8081/nexus/content/groups/public/</url> </pluginRepository> </pluginRepositories> </profile>
Configuration under < Settings > < / Settings > node: enable warehouse configuration
<!-- Activate and download warehouse configuration! --> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles>
Test:
In order to better see the effect, it is recommended to delete the local Dao module, delete the Jar of the local warehouse, directly compile the Service module, and then start looking for Dao modules according to dependencies
- If you can't find it, go to the private server to download it. If you don't have it, you will go to the warehouse. How can you not have the private server just uploaded
- After downloading, the local warehouse will automatically store one, and the next call will directly go to the local warehouse to find it!!
- You can see the download ID below!
OK, you'll have a general understanding of Maven's study here and basically meet the development needs! Three couplets are recommended~
It's a new year. I wish you a happy New Year! Happy New Year
I have time to update a few more chapters (″  ̄  ̄) people (″  ̄  ̄ ″) these two days