maven多模块的配置和注意细节

1.项目父模块pom.xml

1.packaging 为pom
2.可以继承其他pom,如项目用了spring-boot-starter-parent
3.modules下定义子模块的artifactId标识
<?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>
    <packaging>pom</packaging>
    <modules>
        <module>HaoohoCommon</module>
        <module>HaoohoTasks</module>
        <module>HaoohoDataCenterApp</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.haooho</groupId>
    <artifactId>datacenter</artifactId>
    <version>1.0-dev</version>
    

1.子模块HaoohoCommon pom.xml

1.common 是其他模块依赖
2.packaging 为jar
3.spring-boot-maven-plugin插件,创建spring可依赖的jar包
<?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">
    <parent>
        <artifactId>datacenter</artifactId>
        <groupId>org.haooho</groupId>
        <version>1.0-dev</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>HaoohoCommon</artifactId>
    <packaging>jar</packaging>
    
        <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>
</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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<artifactId>datacenter</artifactId>
		<groupId>org.cyt</groupId>
		<version>1.0-dev</version>
	</parent>
	<artifactId>CYTasks</artifactId>
	<packaging>jar</packaging>


	<dependencies>
		<dependency>
			<groupId>org.cyt</groupId>
			<artifactId>CYTCommon</artifactId>
			<version>1.0-dev</version>
		</dependency>
	</dependencies>


	<dependencyManagement>

	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<classifier>exec</classifier>
				</configuration>
			</plugin>
		</plugins>
	</build>

</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">
    <parent>
        <artifactId>datacenter</artifactId>
        <groupId>org.cyt</groupId>
        <version>1.0-dev</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>CYTDataCenterApp</artifactId>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.cyt</groupId>
            <artifactId>CYTasks</artifactId>
            <version>1.0-dev</version>
        </dependency>
        <dependency>
            <groupId>org.cyt</groupId>
            <artifactId>CYTCommon</artifactId>
            <version>1.0-dev</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencyManagement>

    </dependencyManagement>

</project>
package org.cyt.app;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@ComponentScan(basePackages = { "org.cyt.task" ,"org.cyt.common" }
        , excludeFilters = {
        @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.cyt.common.extConf.*")
}
)
@Slf4j
public class DataCenterApplication extends SpringBootServletInitializer {

       public static void main(String[] args) {
              SpringApplication.run(DataCenterApplication.class, args);
       }
}
<profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 发布 -->
            <id>lib</id>
            <properties>
                <profiles.active>lib</profiles.active>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>3.1.1</version>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <excludeTransitive>false</excludeTransitive> <!-- 表示是否不包含间接依赖的包 -->
                            <stripVersion>false</stripVersion> <!-- 去除版本信息 -->
                        </configuration>
                        <executions>
                            <execution>
                                <id>copy-dependencies</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <!-- 拷贝项目依赖包到lib/目录下 -->
                                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                    <excludeTransitive>false</excludeTransitive>
                                    <stripVersion>false</stripVersion>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <archive>
                                <manifest>
                                    <addClasspath>true</addClasspath>
                                    <classpathPrefix>lib/</classpathPrefix>
                                    <mainClass>org.cyt.app.DataCenterApplication</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
cd data-center\CYTDataCenterApp\src\main\resources


application.yml

    server:
      port: 8037
      address: 0.0.0.0
    spring:
      application:
        name: stream-prediction
      profiles:
        active: dev


线上: application-prod.yml
测试: application-dev.yml


java -jar ./CYTDataCenterApp-1.0-dev.jar  --spring.config.location=/data/ext/work/imagesearch/tasks/configs/ --spring.profiles.include=ext-imgdtb0a --spring.profiles.active=prod 
Share