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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!-- TODO --> <version>2.3.12.RELEASE</version> </parent> <!-- TODO --> <groupId>com.github.dreamroute</groupId> <!-- TODO --> <artifactId>xxx</artifactId> <version>${revision}</version> <packaging>pom</packaging> <properties> <!-- TODO --> <!-- 版本信息 --> <revision>1.0-RELEASE</revision> <!-- TODO --> <!-- 默认仓库地址 --> <repository.nexus.url>https://maven.aliyun.com/repository/public</repository.nexus.url> <repository.oss.url>https://maven.aliyun.com/repository/public</repository.oss.url> <dist.nexus.release>http://nexus.yzw.cn/repository/maven-releases</dist.nexus.release> <dist.nexus.snapshot>http://nexus.yzw.cn/repository/maven-snapshots</dist.nexus.snapshot> <dist.oss.release>https://oss.sonatype.org/content/repositories/snapshots</dist.oss.release> <dist.oss.snapshot>https://oss.sonatype.org/content/repositories/snapshots</dist.oss.snapshot> <!-- 编码相关 --> <java.version>1.8</java.version> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.compilerVersion>${java.version}</maven.compiler.compilerVersion> <project.build.sourceEncoding>${maven.compiler.encoding}</project.build.sourceEncoding> <project.reporting.outputEncoding>${project.build.sourceEncoding}</project.reporting.outputEncoding> <!-- 第三方工具 --> <guava.version>31.0.1-jre</guava.version> <hutool-all.version>5.7.13</hutool-all.version> <arthas-spring-boot-starter.version>3.7.2</arthas-spring-boot-starter.version> <!-- TODO 下方这4个是否需要--> <common-util.version>1.2.2</common-util.version> <mybatis-pro.version>1.5.9</mybatis-pro.version> <springfox-boot-starter.version>3.0.0</springfox-boot-starter.version> <knife4j-spring-boot-starter.version>3.0.3</knife4j-spring-boot-starter.version> <!-- plugin --> <maven-gpg-plugin.version>1.6</maven-gpg-plugin.version> <maven-deploy-plugin.version>3.0.0</maven-deploy-plugin.version> <flatten-maven-plugin.version>1.6.0</flatten-maven-plugin.version> <maven-source-plugin.version>3.2.1</maven-source-plugin.version> <maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version> <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version> <lombok-maven-plugin.version>1.18.16.0</lombok-maven-plugin.version> <nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version> </properties> <dependencyManagement> <dependencies> <!-- 基础依赖start --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool-all.version}</version> </dependency> <!-- 基础依赖end --> <!-- TODO 下方这4个是否需要 --> <dependency> <groupId>com.github.dreamroute</groupId> <artifactId>common-util</artifactId> <version>${common-util.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>${springfox-boot-starter.version}</version> </dependency> <dependency> <groupId>com.github.dreamroute</groupId> <artifactId>mybatis-pro-base</artifactId> <version>${mybatis-pro.version}</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>com.taobao.arthas</groupId> <artifactId>arthas-spring-boot-starter</artifactId> <version>${arthas-spring-boot-starter.version}</version> </dependency> </dependencies> </dependencyManagement> <profiles> <profile> <id>nexus</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>nexus</id> <url>${repository.nexus.url}</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <url>${repository.nexus.url}</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </pluginRepository> </pluginRepositories> <distributionManagement> <repository> <id>nexus</id> <url>${dist.nexus.release}</url> </repository> <snapshotRepository> <id>nexus</id> <url>${dist.nexus.snapshot}</url> </snapshotRepository> </distributionManagement> </profile> <profile> <id>oss</id> <repositories> <repository> <id>nexus</id> <url>${repository.oss.url}</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <url>${repository.oss.url}</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </pluginRepository> </pluginRepositories> <distributionManagement> <repository> <id>oss</id> <url>${dist.oss.release}</url> </repository> <snapshotRepository> <id>oss</id> <url>${dist.oss.snapshot}</url> </snapshotRepository> </distributionManagement> </profile> </profiles> <build> <plugins> <!-- 编译 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> </plugin> <!-- 单元测试:默认关闭 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> <configuration> <argLine>-Dfile.encoding=UTF-8</argLine> <skipTests>true</skipTests> </configuration> </plugin> <!-- pom文件 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>flatten-maven-plugin</artifactId> <version>${flatten-maven-plugin.version}</version> <configuration> <updatePomFile>true</updatePomFile> <flattenMode>oss</flattenMode> </configuration> <executions> <execution> <id>flatten</id> <phase>process-resources</phase> <goals> <goal>flatten</goal> </goals> </execution> <execution> <id>flatten.clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>clean-flattened-pom-backup</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> <configuration> <filesets> <fileset> <directory>${project.basedir}</directory> <includes> <include>.flattened-pom.xml.bak</include> </includes> </fileset> </filesets> </configuration> </execution> </executions> </plugin> <!-- 默认关闭lombok明细打包 --> <plugin> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>${lombok-maven-plugin.version}</version> <configuration> <skip>true</skip> </configuration> </plugin> <!-- 默认关闭部署插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>${maven-deploy-plugin.version}</version> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> <modules> </modules> </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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <!-- TODO --> <groupId>com.github.dreamroute</groupId> <!-- TODO --> <artifactId>xxx</artifactId> <version>${revision}</version> </parent> <!-- start --> <!-- TODO --> <artifactId>xxx-spring-boot-starter</artifactId> <!-- TODO --> <name>${project.parent.artifactId}</name> <!-- TODO --> <description>xxx</description> <!-- TODO --> <url>https://github.com/Dreamroute/xxx</url> <scm> <url>${project.url}</url> </scm> <licenses> <license> <name>MIT</name> <url>https://opensource.org/licenses/mit-license.php</url> </license> </licenses> <developers> <developer> <name>w.dehai</name> <email>342252328@qq.com</email> </developer> </developers> <!-- end --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> </dependencies> <profiles> <profile> <id>nexus</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>${maven-deploy-plugin.version}</version> <configuration> <skip>false</skip> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>oss</id> <build> <plugins> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>${nexus-staging-maven-plugin.version}</version> <extensions>true</extensions> <configuration> <serverId>ossrh</serverId> <nexusUrl>https://oss.sonatype.org</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin> </plugins> </build> </profile> </profiles> <build> <plugins> <!-- 源码打包start --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>${maven-source-plugin.version}</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!-- 源码打包end --> <!-- javadoc --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>${maven-javadoc-plugin.version}</version> <configuration> <encoding>UTF-8</encoding> <charset>UTF-8</charset> <docencoding>UTF-8</docencoding> </configuration> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> <configuration> <additionalOptions>-Xdoclint:none</additionalOptions> <encoding>UTF-8</encoding> </configuration> </execution> </executions> </plugin> <!-- gpg签名 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>${maven-gpg-plugin.version}</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
最佳实践
关于配置settings.xml和pom.xml的最佳实践
* pom优先于settings
* 原则上我们都使用pom来进行配置,避免因为每个开发者本地的settings不同而有所差异,`<profiles>`配置在pom中而不是settiings中
* 公司管理非公共jar的方式有两种:
* 一种是不区分环境
* 一种是区分环境,比如dev,test,stg,prd等
* 对于不分区环境的,我们一个mirror全部从公共私服(镜像)拉取即可,这样所有请求都走公司私服,速度比较快,这种是应对新建项目未配置pom时候,则正常情况下所有的项目均应该配置pom,比如
<mirror> <id>nexus</id> <name>公司私服</name> <url>http://nexus.xxx.xxx/repository/maven-public</url> <mirrorOf>*</mirrorOf> </mirror>
* 对于分环境的,我们将各种环境的的仓库走各个环境的仓库,其他的都走公共私服,如下所示。这样的好处是:
* 当某个项目并没配置profiles时候走的是公共仓库
* 当某个项目配置了profiles时候走的是对应的仓库,当新建项目时候由于pom未配置也会走私服,比较快
* 当有新的环境增加时在下方的mirrorOf中增加`!xxx`即可
<mirror> <id>nexus</id> <name>公司私服</name> <url>http://nexus.xxx.xxx/repository/maven-public</url> <mirrorOf>*,!xxx-dev,!xxx-qa,!xxx-pre,!xxx-stg!,xxx-prd</mirrorOf> </mirror>
* 对于profile的配置建议(只需要发布到私服):
<!-- 环境信息,mvn clean source:jar deploy -P ${env},也可通过idea的mvn插件勾选对应环境进行deploy --> <!-- 这里的id就是上面${env}替换的值 --> <!-- <id>,<profileActive>设置成相同的值 --> <!-- distributionManagement.snapshotRepository和distributionManagement.repository和repositories.repositorie.id的id设置成mirror中排除的名称,这样一来拉去jar包从指定的${id}(也就是${env})中拉去,deploy时候也是deploy到指定的${id}(也就是${env})中去 --> <!-- 如果使用flatten插件需要全局禁用deploy插件,并且在需要deploy的模块中增加<profile>启用deploy插件,对于不需要deploy的模块不需要配置<distributionManagement> --> <!-- <repositories>放在父pom的profile中,因为需要从不同环境拉去jar包 --> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>dev</profileActive> </properties> <repositories> <repository> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> <id>xxx-dev</id> <url>http://nexus.xxx.xxx/repository/xxx-dev-public/</url> </repository> </repositories> </profile> </profiles> <!-- <distributionManagement>放在需要deploy的子模块中,由于父pom全局禁用了deploy,所以这里还需要开启 --> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>dev</profileActive> </properties> <distributionManagement> <snapshotRepository> <id>xxx-dev</id> <name>xxx-dev</name> <url>http://nexus.xxx.xxx/repository/xxx-dev/</url> <uniqueVersion>false</uniqueVersion> </snapshotRepository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>false</skip> </configuration> </plugin> </plugins> </build> </profiles>
* 对于profile的配置建议(既要发布到私服又要发布到中央库的项目),使用flattern插件的情况:
* maven-deploy-plugin插件全局禁用deploy
* 将repositories配置在父pom中,实现所有子模块继承(由于都从私服拉去,repositories无需配置profile,只需要将distributionManagement配置在profile中即可)
* 由于私服和公共仓库的
* 将distributionManagement分别设置在子模块中,便于deploy,全部如下:
<!-- repositories放在父pom中,并且无需放在profile中,因为都是从私服下载 --> <repositories> <repository> <id>nexus</id> <url>http://nexus.xxx.xxx/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <!-- profiles分别放在需要deploy的子模块中 --> <profiles> <profile> <id>nexus</id> <activation> <activeByDefault>true</activeByDefault> </activation> <distributionManagement> <repository> <id>nexus</id> <url>http://nexus.xxx.xxx/repository/maven-releases/</url> </repository> <snapshotRepository> <id>nexus</id> <url>http://nexus.xxx.xxx/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>false</skip> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>oss</id> <distributionManagement> <repository> <id>oss</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </repository> <snapshotRepository> <id>oss</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>1.6.8</version> <extensions>true</extensions> <configuration> <serverId>ossrh</serverId> <nexusUrl>https://oss.sonatype.org</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin> </plugins> </build> </profile> </profiles>
1.6.13