Maven multi-project aggregation configuration

        Using Maven in a project can greatly simplify the process of development and construction, but once the product line is huge and contains many projects and modules, there will be inconsistencies in the version management between modules, which brings many difficulties to maintenance and development. .

        In general, we use the form of project -- modules to aggregate multiple related projects or modules, so that submodules can use the relevant configuration in the parent project.

 

        A father and son project configuration

        1. Create a parent project

        1) First use the development tool eclipse to create the parent project, right-click in the workspace -> select New -> select project, and find the Maven Project option in the creation window, as shown in the following figure:

 

        2) Select the project type

        It should be noted here that you need to check the two options in the above figure, otherwise you will enter the template selection interface, so that the parent project cannot be created.


        3) Fill in the project details

Fill in the corresponding project information, select pom         in the Packaging option , and then click Finish to complete the creation.

 

        4) The parent project structure created in this way is as follows:


        There will only be a valid pom file in it, and the src folder can be deleted if it has no other function.

 

        2. Create a sub-project (module project)

        1) First open the pom.xml file of the parent project (here parent.project), after opening, the eclipse plugin will display the following interface:


 

        2) Click the "Create..." button under the Modules tab, and the submodule creation interface will pop up, which is similar to the selection interface when creating the parent project. In the module details filling interface, you can select the type of module (pom, jar, war three), as shown in the following figure:

       

        3) The packaging type we choose here is jar, and the project structure is as follows:


 

        The project includes the relevant directories of our familiar Java projects.

 

        3.pom.xml file configuration

        After the parent-child project is created, the configuration information of its pom.xml needs to be modified, so that the sub-module can reuse the configured parameter information of the parent project.

        1) Configure the pom.xml of the parent project and add the <properties> and <dependencyManagement> tags
        to the pom file of the parent project .

        The role of the <properties> tag is similar to that of the properties resource file, providing the corresponding relationship between key and value key-value pairs, so that these configured public parameters can be directly output in other locations.

        An example is as follows:

<!-- Configuration parameters -->
<properties>
	<maven.compile.source>1.7</maven.compile.source>
	<maven.compile.target>1.7</maven.compile.target>
	<maven.compiler.version>2.3.2</maven.compiler.version>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<commons.encoding>UTF-8</commons.encoding>
	<jdk.path>${JAVA_HOME}/lib/tools.jar</jdk.path>
	<!-- version -->
	<major.version>1</major.version>
	<minor.version>0</minor.version>
	<!-- SNAPSHOT -->
	<version.type>-SNAPSHOT</version.type>
	<full.version>${major.version}.${minor.version}${version.type}</full.version>
	<configure.maven.version>${full.version}</configure.maven.version>
	<hibernate5.version>5.2.1.Final</hibernate5.version>
</properties>

 

        The role of <dependencyManagement>, as the name suggests, is the management of dependency dependencies. In traditional projects, we only need to configure

<dependencies>
    <dependencie>
     ...
    </dependencie>
</dependencies>

        Dependency labels can use Maven to automatically load related dependent projects. However, such configurations are often the same in multiple projects, and deviations often occur during development and maintenance. So Maven provides the <dependencyManagement> tag to manage these dependencies, which can be reused in submodules.

        An example is as follows:

<!-- Dependency-->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate5.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate5.version}</version>
		</dependency>
	</dependencies>
</dependencyManagement>

        After the configuration is complete, the complete pom file content is:

<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>com.example</groupId>
	<artifactId>parent.project</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>parent.project</name>
	<description>Maven父工程</description>
	<modules>
		<module>module.project</module>
	</modules>

	<!-- Configuration parameters -->
	<properties>
		<maven.compile.source>1.7</maven.compile.source>
		<maven.compile.target>1.7</maven.compile.target>
		<maven.compiler.version>2.3.2</maven.compiler.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<commons.encoding>UTF-8</commons.encoding>
		<jdk.path>${JAVA_HOME}/lib/tools.jar</jdk.path>
		<!-- version -->
		<major.version>1</major.version>
		<minor.version>0</minor.version>
		<!-- SNAPSHOT -->
		<version.type>-SNAPSHOT</version.type>
		<full.version>${major.version}.${minor.version}${version.type}</full.version>
		<configure.maven.version>${full.version}</configure.maven.version>
		<hibernate5.version>5.2.1.Final</hibernate5.version>
	</properties>

	<!-- Dependency-->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.hibernate</groupId>
				<artifactId>hibernate-core</artifactId>
				<version>${hibernate5.version}</version>
			</dependency>
			<dependency>
				<groupId>org.hibernate</groupId>
				<artifactId>hibernate-entitymanager</artifactId>
				<version>${hibernate5.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

 

        2) Submodule pom.xml file configuration

        In the parent project, we see that many public parameters and dependencies have been configured. Next, we will use these configurations in the pom file of the submodule.

        First configure the dependency. In the submodule, we only need to configure the dependency used like other ordinary projects, as shown below:

<dependencies>
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-core</artifactId>
	</dependency>
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-entitymanager</artifactId>
	</dependency>
</dependencies>

        It can be seen that we no longer need to configure the <version> parameter in <dependency>, because the version parameter has been specified in the parent project.

 

        Then the configuration parameters in <properties> are reused. Taking the build plugin as an example, the configuration is as follows:

<build>
	<pluginManagement>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven.compiler.version}</version>
				<configuration>
					<source>${maven.compile.source}</source>
					<target>${maven.compile.target}</target>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>
		</plugins>
	</pluginManagement>
</build>

        It can be seen that each parameter uses the value configured by <properties> in the parent project, which is both uniform in configuration and convenient for maintenance. Whether the specific value is successfully quoted can be directly checked under the Effective POM tab.

 

        Second, refer to other project configurations

        Although the project structure of one parent and multiple children can refactor the entire project more neatly, what will Maven do if the unrelated projects also need to have the same configuration?

        In fact, Maven's support at this point is not strong enough, perhaps for security or other considerations, it can only refer to the dependency projects configured by <dependencyManagement> in other projects. Therefore, the combination of configuration files cannot be done yet.

        1. Create a configure project

        The role of the configure project is to store the configuration of various dependencies. The creation process is the same as creating a parent-child project. The difference is that the submodule also selects the pom type in the packaging options, so that the entire project becomes a pure The project of the configuration file is only used to store configuration information. After the project is created, the structure is as follows:



        Each submodule is a kind of framework-dependent configuration information, which makes it easy to add and maintain.

        Take hibernate as an example, open its (configure.maven.hibernate project) pom.xml file, the content 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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.vanilla</groupId>
		<artifactId>configure.maven</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<artifactId>configure.maven.hibernate</artifactId>
	<packaging>pom</packaging>
	<name>configure.maven.hibernate</name>
	<description>hibernate配置</description>
	<!-- Version Configuration -->
	<properties>
		<hibernate5.version>5.2.1.Final</hibernate5.version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.hibernate</groupId>
				<artifactId>hibernate-core</artifactId>
				<version>${hibernate5.version}</version>
			</dependency>
			<dependency>
				<groupId>org.hibernate</groupId>
				<artifactId>hibernate-entitymanager</artifactId>
				<version>${hibernate5.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

        There is no special configuration, and it only declares the dependency and version of hibernate.

 

        2. Introduce configure configuration information

        You can directly introduce the relevant configuration in the pom.xml file of the parent project (parent.project). If you want to introduce the dependency configuration of hibernate, the configuration information is as follows:

<!-- Dependency-->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.demo</groupId>
			<artifactId>configure.maven.hibernate</artifactId>
			<version>${configure.maven.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

        In this way, the <dependencyManagement> part of the module configure.maven.hibernate is directly introduced here, and there is no need to configure the version number.

 

        3. Submodules are configured using dependencyManagement

        Using dependencies in submodules is the same as before, as follows:

<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
		</dependency>
</dependencies>

 

        Through the above configuration of Maven, we can reuse some repeated configurations well, and achieve the purpose of unified management and unified maintenance.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693858&siteId=291194637