二、配置Maven(pom.xml)
接下来就是重头戏了,pom.xml是一个maven项目的重点配置,一个项目的所有配置都可以由这个文件来描述,文件中的所有配置都有默认值,也就是说所有的配置都是可选配置,但是为了把构件发布到中央仓库,我们必须配置一些关键信息,否则再发布时是不会通过了。
在工程的pom.xml文件中,引入Sonatype官方的一个通用配置oss-parent,这样做的好处是很多pom.xml的发布配置不需要自己配置了:
<parent> <groupId>org.sonatype.oss</groupId> <artifactId>oss-parent</artifactId> <version>7</version> </parent>
并增加Licenses、SCM、Developers信息:
<licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> </license> </licenses> <developers> <developer> <name>Lux Sun</name> <email>28554482@qq.com</email> <organization>Lux Sun</organization> <url>https://github.com/LuxSun</url> </developer> </developers> <scm> <url>https://github.com/LuxSun/requestjson</url> <connection>https://github.com/LuxSun/requestjson.git</connection> </scm>
修改maven配置文件setting.xml,在servers中增加server配置。
<servers> <server> <id>sonatype-nexus-snapshots</id> <username>Sonatype 账号</username> <password>Sonatype 密码</password> </server> <server> <id>sonatype-nexus-staging</id> <username>Sonatype 账号</username> <password>Sonatype 密码</password> </server> </servers>
根据官方指南,这里需要4个插件,
maven-source-plugin 用来生成Source Jar文件
maven-javadoc-plugin 用来生成 javadoc 文档
maven-gpg-plugin 用来对工程文件进行自动签名
nexus-staging-maven-plugin 用来将工程发布到中央仓库
另外注意生成javadoc文档时需要指定关闭doclint,不然可能因为使用了不规范的javadoc注解而导致失败,完整配置如下。
完整版
<?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> <groupId>com.luxsuen</groupId> <artifactId>requestjson</artifactId> <version>1.0.1</version> <packaging>jar</packaging> <name>requestjson</name> <description>RequestJson</description> <url>https://github.com/LuxSun/requestjson</url> <properties> <spring.version>4.3.2.RELEASE</spring.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties> <licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> </license> </licenses> <developers> <developer> <name>Lux Sun</name> <email>28554482@qq.com</email> <organization>Lux Sun</organization> <url>https://github.com/LuxSun</url> </developer> </developers> <scm> <url>https://github.com/LuxSun/requestjson</url> <connection>https://github.com/LuxSun/requestjson.git</connection> </scm> <distributionManagement> <snapshotRepository> <id>ossrh</id> <name>oss Snapshots Repository</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> <repository> <id>ossrh</id> <name>oss Staging Repository</name> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <id>default-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <excludes> <exclude>**/spring/</exclude> <exclude>**/com/luxsuen/requestjson/entity/</exclude> <exclude>**/com/luxsuen/requestjson/web/</exclude> <exclude>**/META-INF/*.kotlin_module</exclude> </excludes> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>release</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> <configuration> <excludes> <exclude>**/spring/</exclude> <exclude>**/com/luxsuen/requestjson/entity/</exclude> <exclude>**/com/luxsuen/requestjson/web/</exclude> <exclude>**/META-INF/*.kotlin_module</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.9.1</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <excludePackageNames>com.luxsuen.requestjson.entity:com.luxsuen.requestjson.web</excludePackageNames> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>1.6.7</version> <extensions>true</extensions> <configuration> <serverId>ossrh</serverId> <nexusUrl>https://oss.sonatype.org/</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>
注意1:以上 pom.xml 必须包括:name、description、url、licenses、developers、scm 等基本信息,此外,使用了 Maven 的 profile 功能,只有在 release 的时候,创建源码包、创建文档包、使用 GPG 进行数字签名。此外,snapshotRepository 与 repository 中的 id 一定要与 setting.xml 中 server 的 id 保持一致。
注意2:这里在 nexus-staging-maven-plugin 插件里开启了自动 Release。也可以关掉,然后登录构件仓库 https://oss.sonatype.org 手动去 close 然后 relseae。