git发布到中央仓库

GPG

Gpg生成公私钥,密钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$ gpg --gen-key
gpg (GnuPG) 1.4.22; Copyright (C) 2015 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) Y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"

Real name: lichengcai(suimi)<isuimi@aliyun.com>
Invalid character in name
Real name: lichengcai
Email address: isuimi@aliyun.com
Comment: suimi
You selected this USER-ID:
    "lichengcai (suimi) <isuimi@aliyun.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? 0
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
....+++++
...+++++
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.....+++++
.......+++++
gpg: /c/Users/suimi/.gnupg/trustdb.gpg: trustdb created
gpg: key 1AFEE421 marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/1AFEE421 2019-04-16
      Key fingerprint = 3CE9 7E3C E01E E78E AFD8  0A32 0CF0 9882 1AFE E421
uid                  lichengcai (suimi) <isuimi@aliyun.com>
sub   2048R/F82E2AB5 2019-04-16
1
2
3
4
5
6
7
$ gpg --list-keys --keyid-format short
/c/Users/suimi/.gnupg/pubring.gpg
---------------------------------
pub   2048R/1AFEE421 2019-04-16
uid                  lichengcai (suimi) <isuimi@aliyun.com>
sub   2048R/F82E2AB5 2019-04-16

发布key

1
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 1AFEE421

查询发布是否成功

1
2
3
4
5
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 1AFEE421
gpg: requesting key 1AFEE421 from hkp server pool.sks-keyservers.net
gpg: key 1AFEE421: "lichengcai (suimi) <isuimi@aliyun.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

Maven配置

pom配置,release时需要javadoc,source以及gpg签名文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <tagNameFormat>v@{project.version}</tagNameFormat>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                    <useReleaseProfile>false</useReleaseProfile>
                    <!-- 执行oss-release profile-->
                    <releaseProfiles>oss-release</releaseProfiles>
                    <goals>deploy</goals>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.3</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <properties>
                        <property>
                            <name>usedefaultlisteners</name>
                            <value>false</value>
                        </property>
                        <property>
                            <name>listener</name>
                            <value>org.uncommons.reportng.HTMLReporter</value>
                        </property>
                    </properties>
                    <skip>false</skip>
                    <testFailureIgnore>true</testFailureIgnore>
                    <argLine>${surefireArgLine}</argLine>
                    <workingDirectory>target/</workingDirectory>
                    <!-- <forkMode>always</forkMode> -->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.8</version>
                <configuration>
                    <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                    <skip>${skipTests}</skip>
                    <output>file</output>
                    <append>true</append>
                </configuration>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>oss-release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>3.0.0</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.9.1</version>
                        <configuration>
                            <additionalparam>-Xdoclint:none</additionalparam>
                        </configuration>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

maven settings配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<settings>
    <servers>
        <server>
            <id>ossrh</id>
            <username>oss-user</username>
            <password>oss-pwd</password>
        </server>
    </servers>
    <profiles>
        <profile>
          <id>ossrh</id>
          <activation>
            <activeByDefault>true</activeByDefault>
          </activation>
          <properties>
            <!--
            <gpg.executable>gpg2</gpg.executable> -->
            <gpg.passphrase>the_pass_phrase</gpg.passphrase>
          </properties>
        </profile>
      </profiles>
</settings>

执行过程

1
2
mvn  -Darguments="-DskipTests"  -DautoVersionSubmodules=true release:prepare
mvn  -Darguments="-DskipTests gpg.passphrase=pwd"  release:perform  

验证

发布后需要在sonatype.org验证,验证成功后2小时后就可在中央仓库查询到

Travis Deploy配置

配置user和pwd

在项目根目录建/bin/settings.xml,同时在travis配置SONATYPE_USERNAMESONATYPE_PASSWORD环境变量,并赋值

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
		<server>
			<id>ossrh</id>
            <username>${env.SONATYPE_USERNAME}</username>
            <password>${env.SONATYPE_PASSWORD}</password>
		</server>
    </servers>
</settings>

travis.yml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
language: java

branches:
  only:
    - master
    - /^release(-|_).*$/
    - /^dev(-|_).*$/
    - /^v(\d+.){1,2}\d+.*$/
    - /^feature(-|_).*$/

jdk:
  - oraclejdk8

cache:
  directories:
    - $HOME/.m2
    - $HOME/.sonar
addons:
  sonarcloud:
    organization: "aurorasic-github"

install: mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V

script: skip

after_success:
  - bin/deploy-snapshot.sh
  - mvn jacoco:report  sonar:sonar

自定义shell

可以通过自定义shell,完成特定需要的任务

1
2
3
4
5
6
7
8
9
10
#!/bin/bash

if [[ $TRAVIS_BRANCH == master ]] || \
   [[ $TRAVIS_BRANCH == release* ]] || \
   [[ $TRAVIS_BRANCH == dev* ]] ;
then
  echo -e "Publishing maven snapshot...\n"
  mvn -s bin/settings.xml deploy -DskipTests -Dmaven.install.skip=true -B
  echo -e "Published maven snapshot"
fi

shell脚本需要授权

1
git update-index --chmod +x bin/deploy-snapshot.sh

参考资料